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.

Slides:



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

CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
1 Exceptions: An OO Way for Handling Errors Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Application: Yacc A parser generator A context-free grammar An LR parser Yacc Yacc input file:... definitions... %... production rules... %... user-defined.
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
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.
CPSC 388 – Compiler Design and Construction
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
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.
Chapter 8 Improving the User Interface
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
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.
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
Yacc YACC BNF grammar example.y Other modules example.tab.c Executable
Compiler Construction Parsing Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Environments and Evaluation
Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter 2.2 (Partial) Hashlama 11:00-14:00.
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.
Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter 2.2 (Partial)
Compiler Construction Parsing I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
CS 536 Spring Learning the Tools: JLex Lecture 6.
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
LEX and YACC work as a team
COP4020 Programming Languages
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
General Programming Introduction to Computing Science and Programming I.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
Automated Parser Generation (via CUP)CUP 1. High-level structure JFlexjavac Lexer spec Lexical analyzer text tokens.java CUPjavac Parser spec.javaParser.
CS 614: Theory and Construction of Compilers Lecture 10 Fall 2002 Department of Computer Science University of Alabama Joel Jones.
Lab 3: Using ML-Yacc Zhong Zhuang
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
CS308 Compiler Principles Introduction to Yacc Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University.
TDDD55- Compilers and Interpreters Lesson 1 Zeinab Ganjei Department of Computer and Information Science Linköping University.
INVISIBLE JACC By Ronald Hathaway. So, What is it? Invisible jacc is a parser generator implemented in java which produces lexical analyzers and parsers.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
1 Parsers and Grammar. 2 Categories of Grammar Rules  Declarations or definitions. AttributeDeclaration ::= [ final ] [ static ] [ access ] datatype.
Recursive Descent Parsers Lecture 6 Mon, Feb 2, 2004.
Compiler Principle and Technology Prof. Dongming LU Mar. 26th, 2014.
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 Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
Yacc. Yacc 2 Yacc takes a description of a grammar as its input and generates the table and code for a LALR parser. Input specification file is in 3 parts.
Parser Generation Using SLK and Flex++ Copyright © 2015 Curt Hill.
Firoze Abdur Rakib. Syntax errors, also known as parsing errors, are perhaps the most common kind of error you encounter while you are still learning.
LECTURE 7 Lex and Intro to Parsing. LEX Last lecture, we learned a little bit about how we can take our regular expressions (which specify our valid tokens)
LECTURE 11 Semantic Analysis and Yacc. REVIEW OF LAST LECTURE In the last lecture, we introduced the basic idea behind semantic analysis. Instead of merely.
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.
YACC (Yet Another Compiler-Compiler) Chung-Ju Wu
Syntax error handling –Errors can occur at many levels lexical: unknown operator syntactic: unbalanced parentheses semantic: variable never declared runtime:
JavaCUP JavaCUP (Construct Useful Parser) is a parser generator
CUP: An LALR Parser Generator for Java
Tutorial On Lex & Yacc.
Chapter 2 :: Programming Language Syntax
Parser and Scanner Generation: An Introduction
CPSC 388 – Compiler Design and Construction
Chapter 2 :: Programming Language Syntax
Chapter 2 :: Programming Language Syntax
Saumya Debray The University of Arizona Tucson, AZ 85721
Chapter 10: Compilers and Language Translation
CMPE 152: Compiler Design December 4 Class Meeting
Presentation transcript:

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 x, INT y ) BEGIN INT z; z := x*x - y*y; RETURN z; END INT MAIN f1() BEGIN INT x; READ(x, "A41.input"); INT y; READ(y, "A42.input"); INT z; z := f2(x,y) + f2(y,x); WRITE (z, "A4.output"); END You only need to check the syntax You don’t need to check things such as –Whether the types are correct; –Whether the program makes sense. How to count methods: program ::= methodDecl {:RESULT= 1;:} | methodDecl program:e {: RESULT=e+1); :} ;

3 Modify the EBNF to javaCup Expression -> MultiplicativeExpr (( '+' | '-' ) MultiplicativeExpr)* MultiplicativeExpr -> PrimaryExpr (( '*' | '/' ) PrimaryExpr)* PrimaryExpr -> Num // Integer or Real numbers | Id | '(' Expression ')' | Id '(' ActualParams ')' BoolExpression -> Expression '==' Expression | Expression '!=' Expression ActualParams -> [Expression ( ',' Expression)*] You need to remove * and [ ]. P::=M | M P ; optionalMain::=MAIN | ; You don’t need to use the expression definition here –Use precedence rule would be more concise –Try to reduce file sizes.

4 If you have difficult in locating the errors, send s to GAs or to me, attaching your lex and cup files and detailed error message. Test extensively on your program. Those sample inputs are not enough. Write short programs. Remove anything that you are not sure of. A3 is based on A2. – Lex file should be enhanced {ID} { return new Symbol(…); } –A4 is based on A3. –Will be tested in exam –important

5 Common problems: JavaCup syntax –{: :} for actions –; after each grammar rule –types for terminals and non terminals program ::= methodDecl {:RESULT= 1;:} | methodDecl program:e {: RESULT=e+1); :} ; JavaCup semantics –shift/reduce conflicts –empty parameter list, empty statement list –if-then-else Connection between Jlex and javacup –Terminals not declared in JavaCup

6 Debugging methods turn on debugging mode in the parser, by changing parser.parse().value; to parser.debug_parse().value; Print out symbols in scanner "+" {System.out.println(yytext()); return new Symbol(CalcSymbol.PLUS); } "-" {System.out.println(yytext()); return new Symbol(CalcSymbol.MINUS); } {NUMBER} { System.out.println(yytext()); return new Symbol(CalcSymbol.NUMBER, new Integer(yytext()));} \r|\n|. {System.out.println(yytext()); }

7 Use debug_parse() luna:~/214>java A3User # Initializing parser # Current Symbol is #10 Syntax error # Attempting error recovery # Finding recovery state on stack # Pop stack by one, state was # 0 # No recovery state found on stack # Error recovery fails Couldn't repair and continue parse Exception in thread "main" java.lang.Exception: Can't recover from previous error(s) at java_cup.runtime.lr_parser.report_fatal_error(lr_parser.java:361) at java_cup.runtime.lr_parser.unrecovered_syntax_error(lr_parser.java:409) at java_cup.runtime.lr_parser.debug_parse(lr_parser.java:801) at A3User.main(A3User.java:6)

8 Check A3Symbol.java // // The following code was generated by CUP v0.10k // Fri Mar 16 11:28:02 EDT 2007 // /** CUP generated class containing symbol constants. */ public class A3Symbol { /* terminals */ public static final int TIMES = 21; public static final int READ = 16; public static final int ELSE = 4; public static final int PLUS = 19; public static final int RPAREN = 24; public static final int INT = 11; public static final int EQUAL = 5; public static final int THEN = 3; public static final int SEMI = 15; public static final int NOTEQUAL = 6; public static final int RETURN = 18; public static final int END = 14; public static final int IF = 2; … public static final int ID = 10; …

9 Error tokens What if the input is Scanner:. { return new Symbol(ERROR); } Parser: –Does not need to do anything; –You don’t need to specify rules such as ID ERROR + ID {: some error msg :} – You only need to specify the correct rules, such as ID+ID –When the parser expects to see “+”, but an ERROR token (or any other token) is encountered, the parser knows there is an error

10 Follow the first example in javacup slides. –the second the and third examples are for assignment 4. create script in a bat file, say a3.bat in windows: java JLex.Main A3.lex java java_cup.Main -parser A3Parser -symbols A3Symbol < A3Lu.cup javac -classpath..;. A3Lu.lex.java A3Parser.java A3Symbol.java A3User.java java A3User more A3.output > a3 in unix, change access mode of the file (say “a3”) to executable % chmod 700 a3 % a3