UCLA CS 132 Compiler Construction

Jens Palsberg

Homework 1: LL(1) parsing

Consider the grammar
exp ::= num  |  exp op exp  |  ( exp )
op ::= +  |  -  |  *  |  /
num ::= 0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9
where {exp,op,num} is the set of non-terminal symbols, exp is the start symbol, and { 0,1,2,3,4,5,6,7,8,9,+,-.*,/,(,) } is the set of terminal symbols. The grammar generates a subset of the Java expressions of type int. The various integer operators have precedences as in Java (see the Java specification).

Rewrite the grammar into a grammar which is LL(1), and use the rewritten grammar as the basis for implementing a recursive descent parser in Java. If the input can be parsed correctly, then the parser should output the expression in postfix notation.

Submit a pdf file with the LL(1) grammar, the FIRST and FOLLOW sets for each nonterminal symbol, and the predictive parsing table. Argue that the grammar is LL(1).

Submit electronically your program. Your main file should be called Parse.java, and if Expression is a file, possibly containing a Java expression according to the above grammar, then

java Parse < Expression

outputs