MS document

advertisement
Tanta University
Faculty of Engineering
Computers and Control Dept
Third Year
Compilers Theory
Second term 2006
Problem Set (1)
(1)
Consider the context-free grammar:
S → S S + | S S * | a
a) Show that the string aa+a* can be generated by this grammar.
b) Construct a parse tree for this string.
c) What language is generated by this grammar?
(2) What language is generated by the following grammars? Which of
these grammars are ambiguous?
a) S → 0 S 1 | 0 1
b) S → + S S | – S S | a
c) S → S ( S ) S | 
d) S → a S b S | b S a S | 
e) S → a | S + S | S S | S * | ( S )
(3) Construct unambiguous context-free grammars for each of the
following languages.
a) Arithmetic expressions in postfix notation.
b) Left-associative lists of identifiers separated by commas.
(Lists are included inside parentheses)
c) Right-associative lists of identifiers separated by commas.
(Lists are included inside parentheses)
d) Arithmetic expressions of integers and identifiers with the
four binary operators +, –, *, /.
(4) Construct a syntax-directed translation scheme that translates
arithmetic expressions from infix notation into prefix notation in
which an operator appears before its operands: e.g., –xy is the
prefix notation for x – y. Give annotated parse trees for the
inputs 9–5+2 and 9–5*2.
(5) Construct recursive-descent parsers for the grammars in problem
(2) (a), (b), and (c).
Download