CSC 330 Lecture Notes Week 3, Part 1
Introduction to CUP-Built Parsers



  1. Relevant reading -- Chapter 2 of the book; CUP documentation and examples.

  2. As described in last week's notes, the objective of a parser is to syntactically analyze an input, and produce some form of output.
    1. The most common form of output in a compiler's parser is a parse tree.
    2. The other important parser output is a symbol table
    3. Both of these outputs need to be defined as data structures in whatever language the parser is written, which in our case is Java.

  3. Using CUP to define a parser.
    1. CUP is a meta-translator, in the same way that JFlex is.
    2. Given a parser specification, CUP produces a Java program that does the parsing.
    3. The parser specification is in the form of a standard BNF grammar, augmented with pieces of Java code that define parsing actions.
      1. This structure is analogous to the way JFlex works with a lexical specification, augmented with Java action code.
      2. 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.

  4. Example of a simple CUP-built parser.
    1. 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.
    2. Included with these notes are source code listings for the major files that comprise a CUP-built parser; the files are the following:
      1. expr-list.cup -- the CUP parser specification.
      2. expr-list.jflex -- the JFlex lexer specification.
      3. TreeNode.java -- the abstract class definition of the data structure for parse tree nodes.
      4. TreeNode2.java -- a binary tree node extension of TreeNode
      5. TreeNodeList.java -- an n-ary tree node extension of TreeNode
      6. LeafNode.java -- a leaf node extension of TreeNode
      7. ExprListTest.java -- a test driver program for the expression list parser
    3. We will do an in-depth walk-through of these programs in class.
    4. 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.




index | lectures | handouts | assignments | examples | doc | solutions | bin