CSC 330 Lecture Notes Week 3, Part 1
Introduction to CUP-Built Parsers
Relevant reading -- Chapter 2 of the book; CUP documentation and examples.
As described in last week's notes, the objective of a parser is to
syntactically analyze an input, and produce some form of output.
The most common form of output in a compiler's parser is a parse tree.
The other important parser output is a symbol table
Both of these outputs need to be defined as data structures in whatever
language the parser is written, which in our case is Java.
Using CUP to define a parser.
CUP is a meta-translator, in the same way that JFlex is.
Given a parser specification, CUP produces a Java program that does the
parsing.
The parser specification is in the form of a standard BNF grammar, augmented
with pieces of Java code that define parsing actions.
This structure is analogous to the way JFlex works with a lexical
specification, augmented with Java action code.
In the case of CUP, the specification is defined in the more powerful BNF
notation, instead of the simpler regular expressions that make up a JFlex
specification.
Example of a simple CUP-built parser.
The first simple example we'll look at is an expression-list grammar, similar
to the example in Section 1 of the CUP reference manual.
Included with these notes are source code listings for the major files that
comprise a CUP-built parser; the files are the following:
ExprListTest.java
-- a test driver program for the expression list parser
We will do an in-depth walk-through of these programs in class.
In forthcoming lectures, we will look at a larger example, for a subset of the
Pascal language, which is comparable to the EJay parser you are going to build
for Assignment 3.