Java CUP.

Slides:



Advertisements
Similar presentations
JavaCUP JavaCUP (Construct Useful Parser) is a parser generator
Advertisements

Abstract Syntax Mooly Sagiv html:// 1.
1 JavaCUP JavaCUP (Construct Useful Parser) is a parser generator Produce a parser written in java, itself is also written in Java; There are many parser.
176 Formal Languages and Applications: We know that Pascal programming language is defined in terms of a CFG. All the other programming languages are context-free.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
Bottom-Up Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter (modified)
Context-Free Grammars Lecture 7
1 Problem 2 A Scanner / Parser for Simple C. 2 Outline l Language syntax for SC l Requirements for the scanner l Requirement for the parser l companion.
Bottom-Up Syntax Analysis Mooly Sagiv & Greta Yorsh Textbook:Modern Compiler Design Chapter (modified)
Lecture 14 Syntax-Directed Translation Harry Potter has arrived in China, riding the biggest initial print run for a work of fiction here since the Communist.
CPSC 388 – Compiler Design and Construction
Attribute Grammars They extend context-free grammars to give parameters to non-terminals, have rules to combine attributes Attributes can have any type,
Parser construction tools: YACC
Compilers: Yacc/7 1 Compiler Structures Objective – –describe yacc (actually bison) – –give simple examples of its use , Semester 1,
Saumya Debray The University of Arizona Tucson, AZ 85721
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
LEX and YACC work as a team
1 Chapter 2 A Simple Compiler. 2 Outlines 2.1 The Structure of a Micro Compiler 2.2 A Micro Scanner 2.3 The Syntax of Micro 2.4 Recursive Descent Parsing.
贺天行.  First please download to appetitzer in our webpage  This tutorial will be mainly about codes provided in the appetizer.
Automated Parser Generation (via CUP)CUP 1. High-level structure JFlexjavac Lexer spec Lexical analyzer text tokens.java CUPjavac Parser spec.javaParser.
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
PART I: overview material
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CS308 Compiler Principles Introduction to Yacc Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University.
Introduction to Yacc Ying-Hung Jiang
1 Using Yacc. 2 Introduction Grammar –CFG –Recursive Rules Shift/Reduce Parsing –See Figure 3-2. –LALR(1) –What Yacc Cannot Parse It cannot deal with.
YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another.
CS 614: Theory and Construction of Compilers Lecture 9 Fall 2002 Department of Computer Science University of Alabama Joel Jones.
Compiler Principles Fall Compiler Principles Lecture 6: Parsing part 5 Roman Manevich Ben-Gurion University.
CS 614: Theory and Construction of Compilers Lecture 7 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 JavaCUP JavaCup (Construct Useful Parser) is a parser generator; Produce a parser written in java, itself is also written in Java; There are many parser.
Compiler Principles Fall Compiler Principles Lecture 5: Parsing part 4 Roman Manevich Ben-Gurion University.
CPSC 388 – Compiler Design and Construction Parsers – Syntax Directed Translation.
More yacc. What is yacc – Tool to produce a parser given a grammar – YACC (Yet Another Compiler Compiler) is a program designed to compile a LALR(1) grammar.
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 6.
YACC Primer CS 671 January 29, CS 671 – Spring Yacc Yet Another Compiler Compiler Automatically constructs an LALR(1) parsing table from.
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 7.
Compiler Principles Fall Compiler Principles Lecture 5: Parsing part 4 Roman Manevich Ben-Gurion University.
CUP: An LALR Parser Generator for Java
A Simple Syntax-Directed Translator
Tutorial On Lex & Yacc.
Introduction to Parsing
CS 326 Programming Languages, Concepts and Implementation
Programming Languages Translator
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
Introduction to Parsing (adapted from CS 164 at Berkeley)
PROGRAMMING LANGUAGES
Fall Compiler Principles Lecture 4: Parsing part 3
CS 3304 Comparative Languages
CPSC 388 – Compiler Design and Construction
Basic Program Analysis: AST
Syntax-Directed Translation
Bison Marcin Zubrowski.
Parsing #2 Leonidas Fegaras.
Yet Another Compiler Compiler
Chapter 2: A Simple One Pass Compiler
Syntax-Directed Translation
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
Parsing #2 Leonidas Fegaras.
LL and Recursive-Descent Parsing
Compiler Construction
Fall Compiler Principles Lecture 4: Parsing part 3
Yet Another Compiler Compiler
More Applications of The Pumping Lemma
Compiler Structures 7. Yacc Objectives , Semester 2,
LL and Recursive-Descent Parsing Hal Perkins Autumn 2009
Saumya Debray The University of Arizona Tucson, AZ 85721
Compiler Design Yacc Example "Yet Another Compiler Compiler"
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
Faculty of Computer Science and Information System
Presentation transcript:

Java CUP

Last Time What do we want? An AST When do we want it? Now!

This Time A little review of ASTs The philosophy and use of a Parser Generator

Translating Lists CFG IdList -> id | IdList comma id Input x, y, z AST IdNode “x” IdNode “y” IdNode “z” IdList , id “z” IdList , id “y” id “x”

Parser Generators Tools that take an SDT spec and build an AST YACC: Yet Another Compiler Compiler Java CUP: Constructor of Useful Parsers Conceptually similar to JLex Input: Language rules + actions Output: Java code Parser spec (xxx.cup) Java CUP Parser Source (parser.java) Symbols (sym.java)

Defines the token names used by both JLex and Java CUP Parser.java Constructor takes arg of type Scanner (i.e., yylex) Contains a parsing method return: Symbol whose value contains translation of root nonterminal Uses output of JLex Depends on scanner and TokenVals sym.java defines the communication language Uses defs of AST classes Also in xxx.cup Parser spec (xxx.cup) Java CUP Parser Source (parser.java) Symbols (sym.java) Defines the token names used by both JLex and Java CUP

Java CUP Input Spec Grammar rules Expr ::= intliteral | id | Expr plus Expr | Expr times Expr | lparens Expr rparens Terminal & nonterminal declarations Optional precedence and associativity declarations Grammar with rules and actions [no actions shown here] Terminal and Nonterminals terminal intliteral; terminal id; terminal plus; terminal minus; terminal times; terminal lparen; terminal rparen; non terminal Expr; lowest precedence first Precedence and Associativity precedence left plus, minus; precedence left times; prededence nonassoc less;

Java CUP Example Assume ExpNode subclasses Assume Token classes PlusNode, TimesNode have 2 children for operands IdNode has a String field IntLitNode has an int field Assume Token classes IntLitTokenVal with field intVal for the value of the integer literal IdTokenVal with field idVal for the actual identifier Step 1: Add types to terminals terminal IntLitTokenVal intliteral; terminal IdTokenVal id; terminal plus; terminal times; terminal lparen; terminal rparen; non terminal ExpNode expr;

Java CUP Example Expr ::= intliteral {: :} | id | Expr plus Expr | Expr times Expr | lparen Expr rparen ;

Java CUP Example Expr ::= intliteral:i {: RESULT = new IntLitNode(i.intVal); :} | id | Expr plus Expr | Expr times Expr | lparen Expr rparen ;

Java CUP Example Expr ::= intliteral:i {: RESULT = new IntLitNode(i.intVal); :} | id:i RESULT = new IdNode(i.idVal); | Expr:e1 plus Expr:e2 RESULT = new PlusNode(e1,e2); | Expr:e1 times Expr:e2 RESULT = new TimesNode(e1,e2); | lparen Expr:e rparen RESULT = e; ;

Java CUP Example PlusNode left: right: Input: 2 + 3 Expr IntLitNode val: IntLitNode val: Expr plus Expr 2 3 intliteral intliteral IntLitTokenVal linenum: … charnum: … intVal: IntLitTokenVal linenum: … charnum: … intVal: 2 3 Purple = Terminal Token (Built by Scanner) Blue = Symbol (Built by Parser)

Handling Lists in Java CUP stmtList ::= stmtList:sl stmt:s {: sl.addToEnd(s); RESULT = sl; :} | /* epsilon */ {: RESULT = new Sequence(); ; Another issue: left-recursion (as above) or right-recursion? For top-down parsers, must use right-recursion Left-recursion causes an infinite loop With Java CUP, use left-recursion! Java CUP is a bottom-up parser (LALR(1)) Left-recursion allows a bottom-up parser to recognize a list s1, s2, s3, s4 with no extra stack space: recognize instance of “stmtList ::= epsilon” (current nonterminal stmtList) recognize instance of “stmtList ::= stmtList:current stmt:s1” [s1] recognize instance of “stmtList ::= stmtList:current stmt:s2” [s1, s2] recognize instance of “stmtList ::= stmtList:current stmt:s3” [s1, s2, s3] recognize instance of “stmtList ::= stmtList:current stmt:s4” [s1, s2, s3, s4]

Handling Unary Minus UMINUS is a phony token never returned by the scanner. UMINUS is solely for the purpose of being used in “%prec UMINUS” /* precedences and associativities of operators */ precedence left PLUS, MINUS; precedence left TIMES, DIVIDE; precedence nonassoc UMINUS; // Also used for precedence of unary minus exp ::= . . . | MINUS exp:e {: RESULT = new UnaryMinusNode(e); :} %prec UMINUS /* artificially elevate the precedence to that of UMINUS */ | exp:e1 PLUS exp:e2 {: RESULT = new PlusNode(e1, e2); :} | exp:e1 MINUS exp:e2 {: RESULT = new MinusNode(e1, e2); . . . ; The precedence of a rule is that of the last token of the rule, unless assigned a specific precedence via “%prec <TOKEN>”

Java CUP Demo ~reps/private/CS536/spring17/CodeSolution java P3 test2.toy test2.out more test2.toy more test2.out parser.java: toy.cup java java_cup.Main < toy.cup