DOC PREVIEW
UW-Madison CS 536 - Lecture 17

This preview shows page 1-2-22-23 out of 23 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

220CS 536 Spring 2008©Java CUPJava CUP is a parser-generationtool, similar to Yacc.CUP builds a Java parser forLALR(1) grammars fromproduction rules and associatedJava code fragments.When a particular production isrecognized, its associated codefragment is executed (typically tobuild an AST).CUP generates a Java source fileparser.java. It contains a classparser, with a methodSymbol parse()The Symbol returned by the parseris associated with the grammar’sstart symbol and contains the ASTfor the whole source program.221CS 536 Spring 2008©The file sym.java is also built foruse with a JLex-built scanner (sothat both scanner and parser usethe same token codes).If an unrecovered syntax erroroccurs,Exception() is thrown bythe parser.CUP and Yacc accept exactly thesame class of grammars—all LL(1)grammars, plus many useful non-LL(1) grammars.CUP is called asjava java_cup.Main < file.cup222CS 536 Spring 2008©Java CUP SpecificationsJava CUP specifications are of theform:• Package and import specifications• User code additions• Terminal and non-terminaldeclarations• A context-free grammar,augmented with Java codefragmentsPackage and Import SpecificationsYou define a package name as:package name ;You add imports to be used as:import java_cup.runtime.*;223CS 536 Spring 2008©User Code AdditionsYou may define Java code to beincluded within the generatedparser:action code {: /*java code */ :}This code is placed within thegenerated action class (whichholds user-specified productionactions).parser code {: /*java code */ :}This code is placed within thegenerated parser class .init with{: /*java code */ :}This code is used to initialize thegenerated parser.scan with{: /*java code */ :}This code is used to tell thegenerated parser how to gettokens from the scanner.224CS 536 Spring 2008©Terminal and Non-terminalDeclarationsYou define terminal symbols youwill use as:terminal classname name1, name2, ...classname is a class used by thescanner for tokens (CSXToken,CSXIdentifierToken, etc.)You define non-terminal symbolsyou will use as:non terminal classname name1, name2, ...classname is the class for theAST node associated with thenon-terminal (stmtNode,exprNode, etc.)225CS 536 Spring 2008©Production RulesProduction rules are of the formname ::= name1 name2 ... action ;orname ::= name1name2 ...action1| name3 name4 ... action2| ...;Names are the names of terminalsor non-terminals, as declaredearlier.Actions are Java code fragments,of the form{: /*java code */ :}The Java object assocated with asymbol (a token or AST node) maybe named by adding a :id suffixto a terminal or non-terminal in arule.226CS 536 Spring 2008©RESULT names the left-hand sidenon-terminal.The Java classes of the symbolsare defined in the terminal andnon-terminal declaration sections.For example,prog ::= LBRACE:l stmts:s RBRACE{: RESULT =new csxLiteNode(s,l.linenum,l.colnum); :}This corresponds to the productionprog → { stmts }The left brace is named l; thestmts non-terminal is called s.In the action code, a newCSXLiteNode is created andassigned to prog. It isconstructed from the AST nodeassociated with s. Its line andcolumn numbers are those givento the left brace,l (by the scanner).227CS 536 Spring 2008©To tell CUP what non-terminal touse as the start symbol (prog inour example), we use thedirective:start with prog;228CS 536 Spring 2008©ExampleLet’s look at the CUP specificationfor CSX-lite. Recall its CFG isprogram → { stmts }stmts → stmt stmts| λstmt → id = expr ;| if ( expr ) stmtexpr → expr + id| expr - id|id229CS 536 Spring 2008©The corresponding CUPspecification is:/***This Is A Java CUP Specification ForCSX-lite, a Small Subset of The CSXLanguage, Used In Cs536 ***//* Preliminaries to set up and use thescanner. */import java_cup.runtime.*;parser code {: public void syntax_error(Symbol cur_token){ report_error(“CSX syntax error at line “+String.valueOf(((CSXToken)cur_token.value).linenum),null);}:};init with {: :};scan with {:return Scanner.next_token();:};230CS 536 Spring 2008©/* Terminals (tokens returned by thescanner). */terminal CSXIdentifierToken IDENTIFIER;terminal CSXToken SEMI, LPAREN, RPAREN,ASG, LBRACE, RBRACE;terminal CSXToken PLUS, MINUS, rw_IF;/* Non terminals */non terminal csxLiteNode prog;non terminal stmtsNode stmts;non terminal stmtNode stmt;non terminal exprNode exp;non terminal nameNode ident;start with prog;prog::= LBRACE:l stmts:s RBRACE {: RESULT=new csxLiteNode(s,l.linenum,l.colnum); :};stmts::= stmt:s1 stmts:s2 {: RESULT=new stmtsNode(s1,s2,s1.linenum,s1.colnum); :}231CS 536 Spring 2008©| {: RESULT= stmtsNode.NULL; :};stmt::= ident:id ASG exp:e SEMI {: RESULT=new asgNode(id,e,id.linenum,id.colnum); :}| rw_IF:i LPAREN exp:e RPAREN stmt:s {: RESULT=new ifThenNode(e,s, stmtNode.NULL,i.linenum,i.colnum); :};exp::=exp:leftval PLUS:op ident:rightval {: RESULT=new binaryOpNode(leftval,sym.PLUS, rightval,op.linenum,op.colnum); :}| exp:leftval MINUS:op ident:rightval {: RESULT=new binaryOpNode(leftval,sym.MINUS,rightval,op.linenum,op.colnum); :}| ident:i {: RESULT = i; :};232CS 536 Spring 2008©ident::= IDENTIFIER:i {: RESULT = new nameNode(new identNode(i.identifierText, i.linenum,i.colnum),exprNode.NULL,i.linenum,i.colnum); :};233CS 536 Spring 2008©Let’s parse{ a = b ; }First, a is parsed usingident::= IDENTIFIER:i {: RESULT = new nameNode(new identNode(i.identifierText, i.linenum,i.colnum),exprNode.NULL,i.linenum,i.colnum); :}We buildnameNodeidentNodenullExprNodea234CS 536 Spring 2008©Next, b is parsed usingident::= IDENTIFIER:i {: RESULT = new nameNode(new identNode(i.identifierText, i.linenum,i.colnum),exprNode.NULL,i.linenum,i.colnum); :}We buildnameNodeidentNodenullExprNodeb235CS 536 Spring 2008©Then b’s subtree is recognized asan exp:| ident:i {: RESULT = i; :}Now the assignment statement isrecognized:stmt::= ident:id ASG exp:e SEMI {: RESULT=new asgNode(id,e,id.linenum,id.colnum); :}We buildnameNodeidentNodenullExprNodeanameNodeidentNodenullExprNodebasgNode236CS 536 Spring 2008©The stmts →λ production ismatched (indicating that there areno more statements in theprogram).CUP matchesstmts::= {: RESULT= stmtsNode.NULL; :}and we buildNext,stmts → stmt stmtsis matched usingstmts::= stmt:s1 stmts:s2 {: RESULT=new stmtsNode(s1,s2,s1.linenum,s1.colnum); :}nullStmtsNode237CS 536 Spring 2008©This buildsAs the last step of the parse, theparser matchesprogram → { stmts }using the CUP ruleprog::= LBRACE:l stmts:s


View Full Document

UW-Madison CS 536 - Lecture 17

Download Lecture 17
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 17 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 17 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?