JavaCUP JavaCUP (Construct Useful Parser) is a parser generator

Slides:



Advertisements
Similar presentations
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Advertisements

Simplifications of Context-Free Grammars
An Introduction To Antlr
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Chapter 6 Writing a Program
1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
1 Packages: Putting Classes Together. 2 Introduction The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance)
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 1 The Study of Body Function Image PowerPoint
7 Copyright © 2005, Oracle. All rights reserved. Creating Classes and Objects.
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Programming Language Concepts
Chapter 2-2 A Simple One-Pass Compiler
Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.
Lilian Blot TO PROGRAMMING & PYTHON Introduction Autumn 2012 TPOP 1.
Semantic Analysis and Symbol Tables
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
8 VM code generation Aspects of code generation Address allocation
1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
25 seconds left…...
Types of selection structures
1 Week 9 Questions / Concerns Hand back Test#2 What’s due: Final Project due next Thursday June 5. Final Project check-off on Friday June 6 in class. Next.
PSSA Preparation.
Abstract Class, Packages and interface from Chapter 9
1 Abstract Class and Packages from Chapter 9 Lecture.
1 Assignment 3 Jianguo Lu. 2 Task: check whether the a program is syntactically correct /** this is a comment line in the sample program **/ INT f2(INT.
Chapter 8 Improving the User Interface
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
Semantics of PLs via Interpreters: Getting Started CS784: Programming Languages Prabhaker Mateti.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
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.
Bottom-Up Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter (modified)
Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Implementation in C Chapter 3.
Environments and Evaluation
Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter 2.2 (Partial) Hashlama 11:00-14:00.
Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter 2.2 (Partial)
Bottom-Up Syntax Analysis Mooly Sagiv & Greta Yorsh Textbook:Modern Compiler Design Chapter (modified)
Chapter 2 A Simple Compiler
Compiler Construction Parsing I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Automated Parser Generation (via CUP)CUP 1. High-level structure JFlexjavac Lexer spec Lexical analyzer text tokens.java CUPjavac Parser spec.javaParser.
1 YACC Parser Generator. 2 YACC YACC (Yet Another Compiler Compiler) Produce a parser for a given grammar.  Compile a LALR(1) grammar Original written.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
CPS 506 Comparative Programming Languages Syntax Specification.
1 Parsers and Grammar. 2 Categories of Grammar Rules  Declarations or definitions. AttributeDeclaration ::= [ final ] [ static ] [ access ] datatype.
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.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Yet Another Compiler-Compiler Stephen C. Johnson July 31, 1978 YACC.
Compiler Principles Fall Compiler Principles Lecture 5: Parsing part 4 Roman Manevich Ben-Gurion University.
Syntax Analysis (chapter 4) SLR, LR1, LALR: Improving the parser From the previous examples: => LR0 parsing is rather weak. Cannot handle many languages.
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.
Compiler Construction Sohail Aslam Lecture Parser Generators  YACC – Yet Another Compiler Compiler appeared in 1975 as a Unix application.  The.
JavaCUP JavaCUP (Construct Useful Parser) is a parser generator
CUP: An LALR Parser Generator for Java
Tutorial On Lex & Yacc.
Textbook:Modern Compiler Design
Java CUP.
Fall Compiler Principles Lecture 4: Parsing part 3
CPSC 388 – Compiler Design and Construction
Fall Compiler Principles Lecture 4: Parsing part 3
Presentation transcript:

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 generators. YACC (Yet Another Compiler-Compiler) for C programming language (dragon book chapter 4.9); There are also many parser generators written in Java JavaCC; ANTLR;

More on classification of java parser generators Bottom up Parser Generators Tools JavaCUP; jay, YACC for Java www.inf.uos.de/bernd/jay SableCC, The Sable Compiler Compiler www.sablecc.org Topdown Parser Generators Tools ANTLR, Another Tool for Language Recognition www.antlr.org JavaCC, Java Compiler Compiler www.webgain.com/java_cc

What is a parser generator l : = p r i c e + x ; Scanner Total := price + tax ; Parser assignment := Expr id Parser generator (JavaCup) Exp + id id Context Free Grammar

Steps to use JavaCup Write a javaCup specification (cup file) Defines the grammar and actions in a file (say, calc.cup) Run javaCup to generate a parser java java_cup.Main calc.cup Notice the package prefix java_cup before Main; Will generate parser.java and sym.java (default class names, which can be changed) Write your program that uses the parser For example, UseParser.java Compile and run your program

Example 1: parse an expression and evaluate it Grammar for arithmetic expression exprexpr ‘+’ expr | expr ‘–’ expr | expr ‘*’ expr | expr ‘/’expr | ‘(‘expr’)’ | number Example (2+4)*3 Our tasks: Tell whether an expression like “(2+4)*3” is syntactically correct; Evaluate the expression. (we are actually producing an interpreter for the “expression language”).

The overall picture java_cup.runtime Scanner Symbol lr_parser public interface Scanner { public Symbol next_token() throws java.lang.Exception; } The overall picture java_cup.runtime Scanner Symbol lr_parser implements extends CalcParser CalcScanner tokens expression (2+4)*3 CalcScanner CalcParser CalcParserUser result JLex javaCup calc.lex calc.cup

Calculator javaCup specification (calc.cup) terminal PLUS, MINUS, TIMES, DIVIDE, LPAREN, RPAREN; terminal Integer NUMBER; non terminal Integer expr; precedence left PLUS, MINUS; precedence left TIMES, DIVIDE; expr ::= expr PLUS expr | expr MINUS expr | expr TIMES expr | expr DIVIDE expr | LPAREN expr RPAREN | NUMBER ; Is the grammar ambiguous? Add precedence and associativity left means, that a + b + c is parsed as (a + b) + c lowest precedence comes first, so a + b * c is parsed as a + (b * c) How can we get PLUS, NUMBER, ...? They are the terminals returned by the scanner. How to connect with the scanner?

Ambiguous grammar error If we enter the grammar as below: Expression ::= Expression PLUS Expression; Without precedence JavaCUP will tell us: Shift/Reduce conflict found in state #4 between Expression ::= Expression PLUS Expression () and Expression ::= Expression () PLUS Expression under symbol PLUS Resolved in favor of shifting. The grammar is ambiguous! Telling JavaCUP that PLUS is left associative helps.

Corresponding scanner specification (calc.lex) import java_cup.runtime.Symbol; Import java_cup.runtime.Scanner; %% %implements java_cup.runtime.Scanner %type Symbol %function next_token %class CalcScanner %eofval{ return null; %eofval} NUMBER = [0-9]+ "+" { return new Symbol(CalcSymbol.PLUS); } "-" { return new Symbol(CalcSymbol.MINUS); } "*" { return new Symbol(CalcSymbol.TIMES); } "/" { return new Symbol(CalcSymbol.DIVIDE); } {NUMBER} { return new Symbol(CalcSymbol.NUMBER, new Integer(yytext()));} \r|\n|. {} Connection with the parser imports java_cup.runtime.*, Symbol, Scanner. implements Scanner next_token: defined in Scanner interface CalcSymbol, PLUS, MINUS, ... new Integer(yytext())

Run JLex D:\214>java JLex.Main calc.lex note the package prefix JLex program text generated: calc.lex.java D:\214>javac calc.lex.java classes generated: CalcScanner.class

Generated CalcScanner class import java_cup.runtime.Symbol; Import java_cup.runtime.Scanner; class CalcScanner implements java_cup.runtime.Scanner { ... .... public Symbol next_token () { ... ... case 3: { return new Symbol(CalcSymbol.MINUS); } case 6: { return new Symbol(CalcSymbol.NUMBER, new Integer(yytext()));} } Interface Scanner is defined in java_cup.runtime package public interface Scanner { public Symbol next_token() throws java.lang.Exception;

Run javaCup Run javaCup to generate the parser D:\214>java java_cup.Main -parser CalcParser -symbols CalcSymbol calc.cup classes generated: CalcParser; CalcSymbol; Compile the parser and relevant classes D:\214>javac CalcParser.java CalcSymbol.java CalcParserUser.java Use the parser D:\214>java CalcParserUser

The token class Symbol.java public class Symbol { public int sym, left, right; public Object value; public Symbol(int id, int l, int r, Object o) { this(id); left = l; right = r; value = o; } ... ... public Symbol(int id, Object o) { this(id, -1, -1, o); } public String toString() { return "#"+sym; } Instance variables: sym: the symbol type; left: left position in the original input file; right: right position in the original input file; value: the lexical value. Recall the action in lex file: return new Symbol(CalcSymbol.NUMBER, new Integer (yytext()));

CalcSymbol.java (default name is sym.java) public class CalcSymbol { public static final int MINUS = 3; public static final int DIVIDE = 5; public static final int NUMBER = 8; public static final int EOF = 0; public static final int PLUS = 2; public static final int error = 1; public static final int RPAREN = 7; public static final int TIMES = 4; public static final int LPAREN = 6; } Contain token declaration, one for each token (terminal); Generated from the terminal list in cup file terminal PLUS, MINUS, TIMES, DIVIDE, LPAREN, RPAREN; terminal Integer NUMBER Used by scanner to refer to symbol types, e.g., return new Symbol(CalcSymbol.PLUS); Class name comes from –symbols directive. java java_cup.Main -parser CalcParser -symbols CalcSymbol calc.cup

The program that uses the CalcPaser import java.io.*; class CalcParserUser { public static void main(String[] args) throws IOException{ File inputFile = new File ("d:/214/calc.input"); CalcParser parser= new CalcParser (new CalcScanner(new FileInputStream(inputFile))); parser.parse(); } The input text to be parsed can be any input stream (in this example it is a FileInputStream); The first step is to construct a parser object. A parser can be constructed using a scanner. this is how scanner and parser get connected. If there is no error report, the expression in the input file is correct.

Recap To write a parser, how many things you need to write? cup file; lex file; a program to use the parser; To run a parser, how many things you need to do? Run javaCup, to generate the parser; Run JLex, to generate the scanner; Compile the scanner, the parser, the relevant classes, and the class using the parser; relevant classes: CalcSymbol, Symbol Run the class that uses the parser.

Recap (cont.) java_cup.runtime Scanner Symbol lr_parser implements extends CalcParser CalcScanner tokens expression 2+(3*5) CalcScanner CalcParser CalcParserUser result JLex javaCup calc.lex calc.cup

Evaluate the expression The previous specification only indicates the success or failure of a parser. No semantic action is associated with grammar rules. To calculate the expression, we must add java code in the grammar to carry out actions at various points. Form of the semantic action: expr:e1 PLUS expr:e2 {: RESULT=new Integer(e1.intValue()+ e2.intValue()); :} Actions (java code) are enclosed within a pair {: :} Labels e2, e2: the objects that represent the corresponding terminal or non-terminal; RESULT: The type of RESULT should be the same as the type of the corresponding non-terminals. e.g., expr is of type Integer, so RESULT is of type integer. In the cup file, you need to specify expr is of Integer type. non terminal Integer expr;

Change the calc.cup terminal PLUS, MINUS, TIMES, DIVIDE, LPAREN, RPAREN; terminal Integer NUMBER; non terminal Integer expr; precedence left PLUS, MINUS; precedence left TIMES, DIVIDE; expr::= expr:e1 PLUS expr:e2 {: RESULT = new Integer(e1.intValue()+ e2.intValue()); :} | expr:e1 MINUS expr:e2 {: RESULT = new Integer(e1.intValue()- e2.intValue()); :} | expr:e1 TIMES expr:e2 {: RESULT = new Integer(e1.intValue()* e2.intValue()); :} | expr:e1 DIVIDE expr:e2 {: RESULT = new Integer(e1.intValue()/ e2.intValue()); :} | LPAREN expr:e RPAREN {: RESULT = e; :} | NUMBER:e {: RESULT= e; :} How do you guarantee NUMBER is of Ineter type? {NUMBER} { return new Symbol(CalcSymbol.NUMBER, new Integer(yytext()));}

non terminal Integer expr; Change CalcPaserUser import java.io.*; class CalcParserUser { public static void main(String[] a) throws Exception{ CalcParser parser= new CalcParser( new CalcScanner(new FileReader(“calc.input”))); Integer result= (Integer)parser.parse().value; System.out.println("result is "+ result); } Why the result of parser().value can be casted into an Integer? Can we cast that into other types? This is determined by the type of expr, which is the head of the first production in javaCup specification: non terminal Integer expr;

Calc: second round Calc program syntax Example program: program  statement | statement program statement  assignment SEMI assignment ID EQUAL expr expr  expr PLUS expr | expr MULTI expr | LPAREN expr RPAREN | NUMBER | ID Example program: X=1; y=2; z=x+y*2; Task: generate and display the parse tree in XML

Abstract syntax tree X=1; y=2; z=x+y*2;

OO Design Rationale Write a class for every non-terminal Program, Statement, Assignment, Expr Write an abstract class for non-terminal which has alternatives Given a rule: statementassignment | ifStatement Statement should be an abstract class; Assignment should extends Statement; Semantic part of the CUP file will construct the object; assignment ::= ID:e1 EQUAL expr:e2 {: RESULT = new Assignment(e1, e2); :} The first rule will return the top level object (the Program object) the result of parsing is a Program object It is similar to XML DOM parser.

Calc2.cup Common bugs in assignments: ; {: :} terminal String ID, LPAREN, RPAREN, EQUAL, SEMI, PLUS, MULTI; terminal Integer NUMBER; non terminal Expr expr; non terminal Statement statement; non terminal Program program; non terminal Assignment assignment; precedence left PLUS; precedence left MULTI; program ::= statement:e {: RESULT = new Program(e); :} | statement:e1 program:e2 {: RESULT=new Program(e1, e2); :}; statement ::= assignment:e SEMI {: RESULT = e; :} ; assignment::= ID:e1 EQUAL expr:e2 {: RESULT = new Assignment(e1, e2); :}; expr ::= expr:e1 PLUS:e expr:e2 {: RESULT=new Expr(e1,e2,e); :} | expr:e1 MULTI:e expr:e2 {: RESULT=new Expr(e1,e2,e); :} | LPAREN expr:e RPAREN {: RESULT = e; :} | NUMBER:e {: RESULT= new Expr(e); :} | ID:e {: RESULT = new Expr(e); :} ; Common bugs in assignments: ; {: :}

Program class import java.util.*; public class Program { private Vector statements; public Program(Statement s) { statements = new Vector(); statements.add(s); } public Program(Statement s, Program p) { statements = p.getStatements(); public Vector getStatements(){ return statements; } public String toXML() { ... ... } Program ::= statement:e {: RESULT=new Program(e); :} | statement:e1 program:e2 {: RESULT=new Program(e1, e2); :}

Assignment class assignment::=ID:e1 EQUAL expr:e2 class Assignment extends Statement{ private String lhs; private Expr rhs; public Assignment(String l, Expr r){ lhs=l; rhs=r; } String toXML(){ String result="<Assignment>"; result += "<lhs>" + lhs + "</lhs>"; result += rhs.toXML(); result += "</Assignment>"; return result; assignment::=ID:e1 EQUAL expr:e2 {: RESULT = new Assignment(e1, e2); :}

Expr class public class Expr { private int value; private String id; private Expr left; private Expr right; private String op; public Expr(Expr l, Expr r, String o){ left=l; right=r; op=o; } public Expr(Integer i){ value=i.intValue();} public Expr(String i){ id=i;} public String toXML() { ... } } expr::= expr:e1 PLUS:e expr:e2 {: RESULT = new Expr(e1, e2, e); :} | expr:e1 MULTI:e expr:e2 {: RESULT = new Expr(e1, e2, e);:} | LPAREN expr:e RPAREN {: RESULT = e; :} | NUMBER:e {: RESULT= new Expr(e); :} | ID:e {: RESULT = new Expr(e); :}

Calc2.lex import java_cup.runtime.*; %% %implements java_cup.runtime.Scanner %type Symbol %function next_token %class Calc2Scanner %eofval{ return null; %eofval} IDENTIFIER = [a-zA-Z][a-zA-Z0-9_]* NUMBER = [0-9]+ "+" { return new Symbol(Calc2Symbol.PLUS, yytext()); } "*" { return new Symbol(Calc2Symbol.MULTI, yytext()); } "=" { return new Symbol(Calc2Symbol.EQUAL, yytext()); } ";" { return new Symbol(Calc2Symbol.SEMI, yytext()); } "(" { return new Symbol(Calc2Symbol.LPAREN, yytext()); } ")" { return new Symbol(Calc2Symbol.RPAREN, yytext()); } {IDENTIFIER} {return new Symbol(Calc2Symbol.ID, yytext()); } {NUMBER} { return new Symbol(Calc2Symbol.NUMBER, new Integer(yytext()));} \n|\r|. { }

Calc2Parser User class ProgramProcessor { public static void main(String[] args) throws IOException{ File inputFile = new File ("d:/214/calc2.input"); Calc2Parser parser= new Calc2Parser( new Calc2Scanner(new FileInputStream(inputFile))); Program pm= (Program)parser.debug_parse().value; String xml=pm.toXML(); System.out.println("result is "+ xml); } Debug_parser(): print out debug info, such as the current token being processed, the rule being applied. Useful to debug javacup specification. Parsing result value is of Program type—this is decided by the type of the program rule: Program ::= statement:e {: RESULT = new Program(e); :} | statement:e1 program:e2 {: RESULT=new Program(e1, e2); :} ;

Another way to define the expression syntax terminal PLUS, MINUS, TIMES, DIV, LPAREN, RPAREN; terminal NUMLIT; non terminal Expression, Term, Factor; start with Expression; Expression ::= Expression PLUS Term | Expression MINUS Term | Term ; Term ::= Term TIMES Factor | Term DIV Factor | Factor Factor ::= NUMLIT | LPAREN Expression RPAREN