Compiler Design

Homework Assignment 2 (20 points)

Due October 8, 2002

Consider a language defined by the following syntax:

- Any integer constant is an arithmetic expression
- If
*e1*and*e2*are arithmetic expressions, then so are:- -
*e1* -
*e1*+*e2* -
*e1*-*e2* -
*e1***e2* -
*e1*/*e2* -
*e1****e2* - (
*e1*)

- -
- Nothing else is an arithmetic expression

Answer the following questions:

- [3] Write a context-free grammar for the language using only one nonterminal.
- [3] Is your grammar ambiguous? Explain your answer.
- [6] Update your grammar to make a grammar that accounting for
the following requirements (you may use as many nonterminals
as you like):
- Precedence (highest to lowest): (1) integer constant, parenthesized expression, unary minus, (2) exponentiation (**), (3) subtraction and division, (4) addition and multiplication
- ALL of the operations are right

- [3] Show a right-most derivation and corresponding parse tree for 1/3-4**5--2.
- [4] Determine the first and follow sets for your grammar. Using your sets construct a parse table for a predictive parser.