Sie sind auf Seite 1von 19

CUP

Robb T.
Koether

The CUP
Parser
Generator
CUP
The CUP File Lecture 12
Parser Code
Terminals
Section 4.9, CUP Manual
Nonterminals
Precedence Rules
Productions

The Action
and Goto
Robb T. Koether
Tables

Assignment Hampden-Sydney College

Fri, Feb 20, 2009


Outline

CUP

Robb T.
Koether 1 The CUP Parser Generator
The CUP
Parser
Generator 2 The CUP File
The CUP File Parser Code
Parser Code
Terminals Terminals
Nonterminals
Precedence Rules Nonterminals
Productions
Precedence Rules
The Action
and Goto Productions
Tables

Assignment
3 The Action and Goto Tables

4 Assignment
The CUP Parser Generator

CUP

Robb T.
Koether

The CUP
The CUP parser generator creates Java source code for
Parser an LR parser that uses the LALR algorithm.
Generator
CUP = Common Useful Parsers.
The CUP File
Parser Code LALR = Look-Ahead LR.
Terminals
Nonterminals
Precedence Rules
LALR is similar to LR(1), whose tables are much larger
Productions than the LR(0) tables.
The Action
and Goto LALR tables are compact, the same size as the LR(0)
Tables
tables.
Assignment
A LALR grammar is an LR(1) grammar whose LR(1)
tables may compressed without creating conflicts.
CUP Input and Output

CUP

Robb T.
Koether

The CUP
Parser CUP reads a description of the grammar from a .cup
Generator
file.
The CUP File
Parser Code
Terminals
From that description, it produces two files:
Nonterminals
Precedence Rules
parser.java and sym.java.
Productions
The file parser.java contains Java source code for
The Action
and Goto the parser.
Tables

Assignment
Using the -parser command-line argument, we can
rename the parser class.
CUP Input and Output

CUP

Robb T.
Koether

The CUP
Parser
Generator The sym class is a class that defines the terminals of
The CUP File the grammar as integer constants.
Parser Code
Terminals It is similar to the Token class that we have been using.
Nonterminals
Precedence Rules
Productions
For example, the ‘+’ token is now sym.PLUS and it is
The Action given a numerical value.
and Goto
Tables Using the -symbols command-line argument, we can
Assignment rename the sym class.
CUP Usage

CUP

Robb T.
Koether

The CUP
Parser
Generator

The CUP File


Parser Code
Typically invocations of CUP:
Terminals
Nonterminals
Precedence Rules
$ java java_cup.Main < grammar.cup
Productions $ java java_cup.Main -parser MyParser < grammar.cup
The Action $ java java_cup.Main -symbols Token < grammar.cup
and Goto
Tables

Assignment
CUP and JLex

CUP

Robb T.
Koether

The CUP
Parser
Generator
CUP may be used with or without JLex.
The CUP File
Parser Code In Lab 6, we will see exactly how to make the two
Terminals
Nonterminals programs work together; it is easy.
Precedence Rules
Productions
It is a little more involved to interface CUP with a
The Action
and Goto hand-written lexer such as MyLexer.
Tables

Assignment
We will not take the time to do that.
CUP Input

CUP

Robb T.
Koether
Parser Code
The CUP
Parser
parser code
Generator {:
The CUP File
public static void main(String args[]) throws Exception
Parser Code {
Terminals parser myParser = new parser(new Yylex(System.in));
Nonterminals
Precedence Rules
}
Productions :}
The Action
and Goto
Tables The CUP file begins with any parser code that the
Assignment programmer wants to add to the parser class.
Type the directive parser code, and then include the
code within the delimiters {: and :}.
CUP Terminals

CUP

Robb T.
Koether
Terminals
The CUP terminal NUM, PLUS, TIMES;
Parser
Generator terminal String ID;
The CUP File
Parser Code
Terminals List the terminals of the grammar.
Nonterminals
Precedence Rules
Productions
Type terminal, followed by a list of symbolic names
The Action
for the terminals (e.g., ID, NUM, PLUS).
and Goto
Tables These are used to create the sym class.
Assignment
JLex will use these for its tokens.
Terminals may have an associated data type. For
example, ID may have the type String.
CUP Nonterminals

CUP

Robb T.
Koether

The CUP Nonterminals


Parser
Generator nonterminal func, glbl, prog;
nonterminal Integer type;
The CUP File
Parser Code
nonterminal LinkedList stmt;
Terminals
Nonterminals
Precedence Rules
Productions
List the nonterminals of the grammar.
The Action
and Goto
Type nonterminal, followed by a list of symbolic
Tables names for the nonterminals (e.g., func, type, stmt).
Assignment
Nonterminals may have an associated data type. For
example, type may have the type Integer.
CUP Precedence Rules

CUP

Robb T.
Koether
Precedence Rules
The CUP
Parser precedence right ASSIGN;
Generator precedence left PLUS, MINUS;
The CUP File
precedence left TIMES, DIVIDE;
Parser Code
Terminals
Nonterminals
Precedence Rules
For each precedence rule,
Productions
Type precedence,
The Action
and Goto
State the associativity (left, right, or nonassoc),
Tables Write the names of the terminals that represent the
Assignment operators at that precedence level.
The precedence rules are listed from lowest to highest
precedence.
CUP Productions

CUP

Robb T.
Koether Productions
The CUP stmt ::= RETURN expr:e SEMI
Parser {: RESULT = SemanticAction.ret(e); :}
Generator expr ::= lval:v ASSIGN expr:e
The CUP File {: RESULT = SemanticAction.assign(v, e); :}
Parser Code
Terminals
Nonterminals
Precedence Rules The format of a production is
Productions

The Action nonterm ::= rightside {: action :};


and Goto
Tables
where
Assignment rightside is a string of grammar symbols.
action is zero or more Java statements.
If there is more than one production for a nonterminal,
they are separated by |.
The Start Symbol

CUP

Robb T.
Koether

The CUP
Parser
Generator
The Start Symbol
The CUP File
Parser Code
start with prog;
Terminals
Nonterminals
Precedence Rules
Productions The start symbol is assumed to be the nonterminal on
The Action the left side of the first production.
and Goto
Tables
Otherwise, specify it using the start with directive.
Assignment
The Action and Goto Tables

CUP

Robb T.
Koether

The CUP
Parser
Generator CUP produces the action, reduce, and goto tables.
The CUP File
Parser Code We can use the command-line arguments
Terminals
Nonterminals
Precedence Rules
-dump_grammar
Productions
-dump_tables
The Action
and Goto to see the numbered grammar symbols and
Tables
productions and the action and goto tables.
Assignment
Example

CUP

Robb T.
Koether

The CUP
Parser
Generator

The CUP File


Parser Code
Terminals Example (CUP and JLex)
Nonterminals
Precedence Rules
Productions
Download and run the CUP-JLex String Demo.
The Action
and Goto
Tables

Assignment
Example

CUP

Robb T. Example (The CUP Productions)


Koether
0. stmts → stmts stmt
The CUP
Parser 1. S 0 → stmts $
Generator

The CUP File


2. stmts → ε
Parser Code
Terminals 3. A → ε
Nonterminals
Precedence Rules
Productions
4. stmt → expr A ;
The Action 5. expr → > expr
and Goto
Tables 6. expr → < expr
Assignment
7. expr → expr + expr
8. expr → num * expr
9. expr → ( expr )
10. expr → string
Example

CUP

Robb T. Example
Koether
The CUP Action Table
The CUP
Parser $ error NUM STR > < + * ( ) ;
Generator 0 r2 r2 r2 r2 r2 r2
1 s9 s2 s3 s5 s4 s7
The CUP File
2 s18
Parser Code
3 r10 r10 r10
Terminals
4 s2 s3 s5 s4 s7
Nonterminals
Precedence Rules
5 s2 s3 s5 s4 s7
Productions 6 r0 r0 r0 r0 r0 r0
7 s2 s3 s5 s4 s7
The Action 8 s11 r3
and Goto 9 r1
Tables 10 s13
11 s2 s3 s5 s4 s7
Assignment
12 r7 r7 r7
13 r4 r4 r4 r4 r4 r4
14 s11 s15
15 r9 r9 r9
16 r5 r5 r5
17 r6 r6 r6
18 s2 s3 s5 s4 s7
19 r8 r8 r8
Example

CUP

Robb T. Example
Koether
The CUP Goto Table
The CUP
Parser expr stmt stmts NT$0
Generator 0 1
1 8 6
The CUP File
2
Parser Code
3
Terminals
4 17
Nonterminals
Precedence Rules
5 16
Productions 6
7 14
The Action 8 10
and Goto 9
Tables 10
11 12
Assignment
12
13
14
15
16
17
18 19
19
Assignment

CUP

Robb T.
Koether
Homework
The CUP
Parser The grammar
Generator

The CUP File


Parser Code
R → R ∪ R | RR | R∗ | (R) | a | b
Terminals
Nonterminals
Precedence Rules
Productions
generates all regular expressions on the alphabet
The Action
Σ = {a, b}.
and Goto
Tables Create a JLex file and a CUP file that will parse the
Assignment regular expressions.
Let the actions be simply to display the productions that
were matched.

Das könnte Ihnen auch gefallen