Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC 388 – Compiler Design and Construction

Similar presentations


Presentation on theme: "CPSC 388 – Compiler Design and Construction"— Presentation transcript:

1 CPSC 388 – Compiler Design and Construction
Parsers – java cup

2

3 Java cup Parser Generator
Java cup specification xxx.cup Java_cup.Main Parser Source code parser.java sym.java

4 Input to java_cup optional package and import declarations
optional user code terminal and nonterminal declarations optional precedence and associativity declarations grammar rules with associated actions

5 Output from java_cup parser.java sym.java class parser {
public parser(Yylex scanner) {…} public Symbol parse() {…} } sym.java Class sym { public final static int TERMINAL=0; returns a Symbol whose value field contains the translation of the root nonterminal one public final static int for each terminal declared in the Java Cup specification.

6 Terminal and Non-Terminal Declarations
/* terminals without values */ terminal name1, name2, ... ; /* terminals with values */ terminal type name1, name2, ... ; /* nonterminals */ non terminal type name1, name2, ... ; If you want to make use of the value associated with a terminal (the value field of the Symbol object returned by the scanner for that token) in your syntax-directed translation, then you must also declare the type of that value field. Similarly, you must declare the types of the translations associated with all of the nonterminals.

7 Precedence Declaration
exp -> exp PLUS exp | exp MINUS exp | exp TIMES exp | exp EQUALS exp | ... This CFG is ambiguous, and will cause conflicts. Declare precedence and associativity in java_cup to fix ambiguity precedence left PLUS, MINUS; precedence left TIMES, DIVIDE; precedence nonassoc EQUALS;

8 Unary Minus Some tokens have multiple precedence: subtraction and unary minus Create a phony UMINUS terminal and declare its precedence Use UMINUS to change precedence in production rules: exp ::= MINUS exp {: RESULT = ... :} %prec UMINUS ;

9 Grammar Rules Declare start non-terminal (otherwise uses head of first production rule) start with program; Declare terminals and non-terminals: terminal SEMICOLON; terminal INT; terminal IdTokenVal ID; non terminal VarDeclNode varDecl; non terminal TypeNode type; non terminal IdNode id;

10 Grammar Rules Finally create production rules and actions:
varDecl ::= type:t id:i SEMICOLON {: RESULT = new VarDeclNode(t, i); :} ; type ::= INT {: RESULT = new IntNode(); :} ; id ::= ID:i {: RESULT = new IdNode(i.idVal); :} ; ::= divides head from body of production rule :x used to give variable name to terminal/non terminals in body {: :} separate out the action (java code) RESULT special variable holding translation of head non-terminal ; each production rules ends with semicolon

11 java java_cup.Main < xxx.cup
Running java_cup java java_cup.Main < xxx.cup

12 Programming Assignment #3
Download PROG3 skeleton under resources on Sakai


Download ppt "CPSC 388 – Compiler Design and Construction"

Similar presentations


Ads by Google