1 / 18

CUP

CUP. Lecture 12 Fri, Feb 18, 2005. The CUP Parser Generator. CUP creates Java source code for an LR parser that uses the LALR algorithm. LALR = L ook- A head LR . LALR is similar to LR(1), whose tables are much larger than the LR(0) tables.

dezso
Télécharger la présentation

CUP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CUP Lecture 12 Fri, Feb 18, 2005

  2. The CUP Parser Generator • CUP creates Java source code for an LR parser that uses the LALR algorithm. • 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.

  3. CUP Input and Output • 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 the source code for the parser. • Using the –parser command-line argument, we can rename the parser class.

  4. CUP Input and Output • The sym class is a class that defines the terminals of the grammar as integer constants. • It is similar to the Token class that we have been using. • For example, the ‘+’ token is now sym.PLUS. • Using the –symbols command-line argument, we can rename the sym class.

  5. CUP and JLex • CUP may be used with or without JLex. • In Lab 6, we will see exactly how to make the two programs work together. • It is easy. • It is a little more involved to interface CUP with a hand-written lexer such as MyLexer. • We will not take the time to do that.

  6. CUP Input • The CUP file begins with any parser code that the programmer wants to add to the parser class. • Type the directive parsercode, and then include the code within the delimiters {: and :}. • See the CUP User’s Manual.

  7. CUP Terminals • Then list the terminals of the grammar. • 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. • JLex will use these for its tokens. • Terminals may have an associated data type. • For example, ID may have the type String. • See the CUP User’s Manual.

  8. CUP Nonterminals • Then list the nonterminals of the grammar. • type nonterminal, followed by a list of symbolic names for the nonterminals (e.g., expr, stmt). • Nonterminals may have an associated data type. • For example, type may have the type Integer. • See the CUP User’s Manual.

  9. CUP Precedence Rules • 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. • Example: precedence left PLUS, MINUS; • See the CUP User’s Manual.

  10. CUP Productions • The format of a production is nonterm::=rightside{:action:}; where • 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 |.

  11. CUP Productions • For example, expr ::= expr PLUS expr {: Action for PLUS :} | expr TIMES expr {: Action for TIMES :} ; • The actions may be embedded within the right side. • For example, expr ::= expr PLUS expr {: Action :} SEMI; • See the CUP User’s Manual.

  12. The Start Symbol • 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. • Example: start with stmt;

  13. Running CUP • To run CUP, we invoke the Main class in the subfolder java_cup (inside the CUP folder). • The Main class expects input from standard input, so we redirect it from the CUP file. • Write java java_cup.Main < grammar.cup • See the CUP User’s Manual.

  14. The Action and Goto Tables • CUP produces the action, reduce, 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. • See the CUP User’s Manual.

  15. Example: CUP and JLex • CUP-JLex Demo

  16. Example: The CUP Productions 0. stmtsstmtsstmt 1. $START stmts$ 2. stmts 3. NT$0  4. stmtexpr NT$0 ; 5. expr > expr 6. expr < expr 7. exprexpr+expr 8. expr NUM *expr 9. expr(expr) 10. expr STRING

  17. Example: The CUP Action Table

  18. Example: The CUP Goto Table

More Related