CUP Lecture 18 CUP User s Manual (online) Robb T. Koether Hampden-Sydney College Fri, Feb 27, 2015 Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 1 / 31
1 The CUP Parser Generator 2 The CUP File Parser Code Terminals Nonterminals Precedence and Associativity Rules Productions 3 The Action and Goto Tables 4 Assignment Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 2 / 31
Outline 1 The CUP Parser Generator 2 The CUP File Parser Code Terminals Nonterminals Precedence and Associativity Rules Productions 3 The Action and Goto Tables 4 Assignment Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 3 / 31
The CUP Parser Generator The CUP parser generator creates Java source code for an LR parser that uses the LALR algorithm. CUP = Common Useful Parsers. LALR = Look-Ahead LR. LALR is similar to LR(1), whose tables are much larger than the LR(0) tables. LALR tables are compact, the same size as the LR(0) tables. A LALR grammar is an LR(1) grammar whose LR(1) tables may be compressed without creating conflicts. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 4 / 31
The CUP Parser Generator CUP reads a description of the grammar from a.cup file. From that description, it produces two files: parser.java and sym.java. The file parser.java contains Java source code for the parser. The sym class is a class that defines the terminals of the grammar as integer constants, e.g., sym.num. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 5 / 31
The CUP Parser Generator Using the -parser command-line argument, we can rename the parser class. Using the -symbols command-line argument, we can rename the sym class. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 6 / 31
CUP Usage Typically invocations of CUP: $ java java_cup.main < grammar.cup $ java java_cup.main -parser MyParser < grammar.cup $ java java_cup.main -symbols Token < grammar.cup Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 7 / 31
CUP and JFlex CUP may be used with or without JFlex. In Assignment 7, we will see exactly how to make the two programs work together. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 8 / 31
CUP and JFlex CUP may be used with or without JFlex. In Assignment 7, we will see exactly how to make the two programs work together. It is, by design, easy. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 8 / 31
Outline 1 The CUP Parser Generator 2 The CUP File Parser Code Terminals Nonterminals Precedence and Associativity Rules Productions 3 The Action and Goto Tables 4 Assignment Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 9 / 31
Outline 1 The CUP Parser Generator 2 The CUP File Parser Code Terminals Nonterminals Precedence and Associativity Rules Productions 3 The Action and Goto Tables 4 Assignment Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 10 / 31
CUP Input Parser Code parser code {: public static void main(string args[]) throws IOException { Reader r = InputStreamReader(System.in); parser myparser = new parser(new Yylex(r)); } :} The CUP file begins with optional parser code that the programmer may add to the parser class. Type the directive parser code, and then include the code within the delimiters {: and :}. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 11 / 31
Outline 1 The CUP Parser Generator 2 The CUP File Parser Code Terminals Nonterminals Precedence and Associativity Rules Productions 3 The Action and Goto Tables 4 Assignment Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 12 / 31
CUP Terminals Terminals terminal PLUS, TIMES, LPAREN, RPAREN, ASSIGN, SEMI; terminal Integer NUM; terminal String ID; Type terminal, followed by a list of symbolic names for the terminals (e.g., ID, NUM, PLUS). These are used to create the sym class. JFlex will use these for its tokens. Terminals may have an associated data type. For example, ID may have the type String. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 13 / 31
Outline 1 The CUP Parser Generator 2 The CUP File Parser Code Terminals Nonterminals Precedence and Associativity Rules Productions 3 The Action and Goto Tables 4 Assignment Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 14 / 31
CUP Nonterminals Nonterminals non terminal stmt; non terminal Integer expr; List the nonterminals of the grammar. Type non terminal, followed by a list of symbolic names for the nonterminals (e.g., stmt, expr). Nonterminals may have an associated data type. For example, expr may have the type Integer. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 15 / 31
Outline 1 The CUP Parser Generator 2 The CUP File Parser Code Terminals Nonterminals Precedence and Associativity Rules Productions 3 The Action and Goto Tables 4 Assignment Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 16 / 31
CUP Precedence Rules Precedence Rules precedence right ASSIGN; precedence left PLUS; precedence left TIMES; For each precedence rule, Type precedence, State the associativity (left, right, or nonassoc), Write the names of the terminals that represent the operators at that precedence level. The precedence rules are listed from lowest to highest precedence. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 17 / 31
Outline 1 The CUP Parser Generator 2 The CUP File Parser Code Terminals Nonterminals Precedence and Associativity Rules Productions 3 The Action and Goto Tables 4 Assignment Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 18 / 31
CUP Productions Productions stmt ::= id:i ASSIGN expr:e SEMI {: System.out.println(i + " = " + e); :}; expr ::= expr:e1 PLUS expr:e2 {: RESULT = e1 + e2; :}; The format of a production is head ::= body {: action :}; where head is a nonterminal. body 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. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 19 / 31
The Start Symbol The Start Symbol start with prog; The start symbol is assumed to be the nonterminal on the left side of the first production. Otherwise, specify it using the start with directive. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 20 / 31
Outline 1 The CUP Parser Generator 2 The CUP File Parser Code Terminals Nonterminals Precedence and Associativity Rules Productions 3 The Action and Goto Tables 4 Assignment Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 21 / 31
The Action and Goto Tables CUP produces the action and goto tables. We can use the command-line arguments -dump_grammar -dump_tables to see the numbered grammar symbols and productions and the action and goto tables. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 22 / 31
Example (The Grammar) Let the grammar be E + ( E, E) * ( E, E) num Let the semantic actions be + ( E, E) Return the sum of the expressions * ( E, E) Return the product of the expressions num Return the value of the token Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 23 / 31
Grammar Example (Grammar) ===== Terminals ===== [0]EOF [1]error [2]PLUS [3]TIMES [4]LPAREN [5]RPAREN [6]COMMA [7]NUM ===== Non terminals ===== [0]stmt [1]expr ===== Productions ===== [0] stmt ::= expr [1] $START ::= stmt EOF [2] expr ::= PLUS LPAREN expr COMMA expr RPAREN [3] expr ::= TIMES LPAREN expr COMMA expr RPAREN [4] expr ::= NUM Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 24 / 31
Action Table Example (Action Table) -------- ACTION_TABLE -------- From state #0 [term 2:SHIFT(to state 2)] [term 3:SHIFT(to state 5)] [term 7:SHIFT(to state 1)] From state #1 [term 0:REDUCE(with prod 4)] [term 5:REDUCE(with prod 4)] [term 6:REDUCE(with prod 4)] From state #2 [term 4:SHIFT(to state 12)] From state #3 [term 0:SHIFT(to state 11)] From state #4 [term 0:REDUCE(with prod 0)] From state #5 [term 4:SHIFT(to state 6)] Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 25 / 31
Action Table Example (Action Table) From state #6 [term 2:SHIFT(to state 2)] [term 3:SHIFT(to state 5)] [term 7:SHIFT(to state 1)] From state #7 [term 6:SHIFT(to state 8)] From state #8 [term 2:SHIFT(to state 2)] [term 3:SHIFT(to state 5)] [term 7:SHIFT(to state 1)] From state #9 [term 5:SHIFT(to state 10)] From state #10 [term 0:REDUCE(with prod 3)] [term 5:REDUCE(with prod 3)] [term 6:REDUCE(with prod 3)] From state #11 [term 0:REDUCE(with prod 1)] Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 26 / 31
Action Table Example (Action Table) From state #12 [term 2:SHIFT(to state 2)] [term 3:SHIFT(to state 5)] [term 7:SHIFT(to state 1)] From state #13 [term 6:SHIFT(to state 14)] From state #14 [term 2:SHIFT(to state 2)] [term 3:SHIFT(to state 5)] [term 7:SHIFT(to state 1)] From state #15 [term 5:SHIFT(to state 16)] From state #16 [term 0:REDUCE(with prod 2)] [term 5:REDUCE(with prod 2)] [term 6:REDUCE(with prod 2)] ------------------------------ Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 27 / 31
Goto Table Example (Goto Table) -------- REDUCE_TABLE -------- From state #0 [non term 0->state 3] [non term 1->state 4] From state #1 From state #2 From state #3 From state #4 From state #5 From state #6 [non term 1->state 7] From state #7 From state #8 [non term 1->state 9] Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 28 / 31
Goto Table Example (Goto Table) From state #9 From state #10 From state #11 From state #12 [non term 1->state 13] From state #13 From state #14 [non term 1->state 15] From state #15 From state #16 ----------------------------- Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 29 / 31
Outline 1 The CUP Parser Generator 2 The CUP File Parser Code Terminals Nonterminals Precedence and Associativity Rules Productions 3 The Action and Goto Tables 4 Assignment Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 30 / 31
Assignment Assignment Let the grammar be E& E E + E E * num Create a JFlex file and a CUP file that will parse this grammar. Let the actions be to display the productions that were matched and to compute the value of the expression as a postfix expression. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 31 / 31