UCLA CS 132 Compiler Construction Fall 2004

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 on paper 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 statement according to the above grammar, then

java Parse < Expression

outputs where X is the linenumber for where the error was found.