Exercise 1.1
Question 1
- In this course, we will try to develop a compiler. I find it hard to develop a compiler, for the sake of the fact that C or C++ have complicated syntax rules and character set. So an artificial computer language with a small character set and easy syntax rules occurred to me.
Question 2
- A program written in C++
int c0 = 2;
int c1 = 5;
while (c1 != 0) {
co += 1;
c1 -= 1;
}
- Same program written in the proposed language
+2 Cell c0 = 2
> +5 Cell c1 = 5
[ Start your loops with your cell pointer on the loop counter (c1 in our case)
< +1 Add 1 to c0
> -1 Subtract 1 from c1
] End your loops with the cell pointer on the loop counter
Question 3
Syntax:
Character | Meaning |
---|---|
program start |
char array[INFINITELY_LARGE_SIZE] = {0};char *ptr=array; |
> |
++ptr |
< |
--ptr |
+1 |
*ptr += 1 |
-1 |
*ptr -= 1 |
. |
putchar(*ptr) |
, |
*ptr=getchar() |
[ |
while (*ptr) { |
] |
} |
0 - 9 |
digit |
digit ::= 0 | 1 | ... | 9
symbol ::= + | -
operation ::= symbol {digit}
Semantics:
Each character in character set means one operator except digits which need to follow a symbol. The format and style is useless.
Question 4
First I need to complete the design of my language. Then I have to build the compiler of my language.
Exercise 1.2
Question 1
(C is a machine language)
Stage 1:
Stage 2:
Exercise 2.1
Exercise 2.2
Exercise 2.3
2 parse trees for sentence abab, so the grammar is ambiguous.