CS3012: Formal Languages and Compilers

advertisement
CS3012 Formal Languages
Exercises
CS3012 Formal Languages
Exercises 9: Postfix and Syntax Trees
1. Write a Yacc grammar with actions which prints out the postfix version of an expression from
grammar G below.
1) S -> S + T
2) S -> T
3) T -> T * F
4) T -> F
5) F -> (S)
6) F -> any string of digits
Paste your answer code in here:
2. Write a Yacc grammar with actions which computes the result of an expression from grammar G
above. e.g. if (4+2)*(3+6) is input, it should print out 54.
Paste your answer code in here:
3.
Write some Lex and Yacc to use the syntax-directed definition of Example 12.10 in the notes to
parse the following expressions and construct the syntax trees:
(i) 3*b+2
(ii) (6+9)*zed
(iii) a*2*five
You will need to write C functions for each of the three functions used in the example.
Paste your answer code in here:
Download