Computer Science 5641
Compiler Design
Homework Assignment 2 (20 points)
Due October 20, 2004
Consider a language defined by the following syntax:
- Any integer constant is an expression
- If e1 and e2 are expressions, then so are:
- +++ e1
- e1 ---
- e1 +-- e2
- e1 --+ e2
- e1 ++- e2
- e1 -++ e2
- ( e1 )
- Nothing else is an expression
Answer the following questions:
- [2] Write a context-free grammar for the language using only one nonterminal.
- [1] Is your grammar ambiguous? Explain your answer.
- [3] Update your grammar to make a grammar that accounting for
the following precedence/associativity information. Operators in
order of precedence from highest to lowest are (two operators at
the same bullet have highest precedence):
- +--, --+ (right associative)
- ++-, -++ (left associative)
- --- (left associative)
- +++ (right associative)
- [1] Show a right-most derivation and corresponding parse tree
for +++ 5 --+ 3 +-- 4 ++- 6 +-- 2 ---
- [3] Determine the first and follow sets for your grammar. Using
your sets construct a parse table for a predictive parser.