UCLA CS 132 Compiler Construction Fall 2005

Jens Palsberg

Homework 1: LL(1) parsing

Consider the grammar
stmt ::= id()  |  stmt ; stmt  |  { stmt }  |  if (id) stmt else stmt
where stmt is the only non-terminal symbol, stmt is the start symbol, and is the list of terminal symbols. The terminal symbol id is defined using the regular expression (letter+) where letter is an ascii character in the interval a..z . The grammar generates a subset of the Java statements.

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.

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

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

java Parse < Statement