C++ parse analysis Save and restore parser state

Slides:



Advertisements
Similar presentations
Parsing 4 Dr William Harrison Fall 2008
Advertisements

Vilmos Zsombori , Shanghai
Application: Yacc A parser generator A context-free grammar An LR parser Yacc Yacc input file:... definitions... %... production rules... %... user-defined.
A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Chapter 3 Syntax Analysis
Lexical and Syntactic Analysis Here, we look at two of the tasks involved in the compilation process –Given source code, we need to first break it into.
Control Flow C and Data Structures Baojian Hua
Structure of a C program
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 3 Lexical and Syntactic Analysis Syntactic.
Syntax Analysis – Part II Quick Look at Using Bison Top-Down Parsers EECS 483 – Lecture 5 University of Michigan Wednesday, September 20, 2006.
Compilers: Yacc/7 1 Compiler Structures Objective – –describe yacc (actually bison) – –give simple examples of its use , Semester 1,
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
Saumya Debray The University of Arizona Tucson, AZ 85721
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.
Scope Accessibility of Names. Review We’ve seen that C++ permits a programmer to declare names and then use those names in a manner consistent with their.
C Tokens Identifiers Keywords Constants Operators Special symbols.
1 Using Yacc: Part II. 2 Main() ? How do I activate the parser generated by yacc in the main() –See mglyac.y.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
LANGUAGE TRANSLATORS: WEEK 14 LECTURE: REGULAR EXPRESSIONS FINITE STATE MACHINES LEXICAL ANALYSERS INTRO TO GRAMMAR THEORY TUTORIAL: CAPTURING LANGUAGES.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Chapters 1-5 Review C++ Class. Chapter 1 – the big picture Objects Class Inheritance Reusability Polymorphism and Overloading.
–Writing a parser with YACC (Yet Another Compiler Compiler). Automatically generate a parser for a context free grammar (LALR parser) –Allows syntax direct.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Introduction to YACC Panfeng Xue
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
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.
Compiler Principle and Technology Prof. Dongming LU Mar. 26th, 2014.
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.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
C syntax (simplified) BNF. Program ::= [ ] Directives ::= [ ] ::= | |… ::=#include > ::=#define.
YACC Primer CS 671 January 29, CS 671 – Spring Yacc Yet Another Compiler Compiler Automatically constructs an LALR(1) parsing table from.
Parser Generation Tools (Yacc and Bison) CS 471 September 24, 2007.
YACC SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Error recovery in predictive parsing An error is detected during the predictive parsing when the terminal on top of the stack does not match the next input.
Syntax error handling –Errors can occur at many levels lexical: unknown operator syntactic: unbalanced parentheses semantic: variable never declared runtime:
Announcements/Reading
Basics (Variables, Assignments, I/O)
Programming Languages 2nd edition Tucker and Noonan
LESSON 3 IO, Variables and Operators
Core Core: Simple prog. language for which you will write an interpreter as your project. First define the Core grammar Next look at the details of how.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University
Chapter 4 Syntax Analysis.
Chapter 4 Top-Down Parsing Part-1 September 8, 2018
Formal Language Theory
Bison: Parser Generator
11/10/2018.
Lexical and Syntactic Analysis
Basics (Variables, Assignments, I/O)
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
CSE401 Introduction to Compiler Construction
Compilers B V Sai Aravind (11CS10008).
CSC 4181Compiler Construction Context-Free Grammars
Govt. Polytechnic,Dhangar
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
CSC 4181 Compiler Construction Context-Free Grammars
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Compiler Construction
Compiler Structures 7. Yacc Objectives , Semester 2,
Type compatibility and pointer operation
Saumya Debray The University of Arizona Tucson, AZ 85721
Decision making and control functions
nested-name-specifier
Compiler Construction
C++ Parse Analysis Introduction
Presentation transcript:

C++ parse analysis Save and restore parser state 2019.03.18 Kei Hasegawa

Example 1 void f() { } int (n); // Declaration statement ... int (n+n); // Expression statement }

Derivation `int’ at Ex 1 (n+n); int (n); int simple-type-specifier decl-specifier If next token is ‘(‘ after simple-type-specifier, it’s not obvious remained program text is init-declarator-list or ‘(‘ expression-list ‘)’ decl-specifier-seq

Give priority to declaration rule Report file of yacc/bison State X1 R11 type_specifier: simple_type_specifier . R12 postfix_expression: simple_type_specifier . '(' expression_list ')' R13 | simple_type_specifier . '(' ')' '(' shift, and go to state Y1 '(' [reduce using rule R11 (type_specifier)] $default reduce using rule R11 (type_specifier) This says that for ‘(‘ after simple-type-specifier, R11 is not used.

Give priority to declaration rule (continue) Insert bellow code to `yyparse’ of appropriate location: if ( yystate == X1 && yychar == ‘(‘ ) { yyn = R11 + 1; goto yyreduce; } Of cause, this cause sytax error for function style cast.

Save parser state Again fix previous code: if (yystate == X1 && yychar == ‘(‘ ) { if (!retry[X1] ) { // At 1st time save(yystate, ...); // Save parser state yyn = R11 + 1; // parse as declaration goto yyreduce; }

Restore parser state Insert bellow code to `yyparse’ where calls `yyerror’: if (parser states are saved) { restore(&yystate, ...); // Restore parser state ++retry[yystate]; // Increment retry counter goto yynewstate; }

Also save tokens which cause 1st syntax error int get_token() { int ret = ... ... if (parser states are saved) { // Save token for retrying // Also save token’s property if it has. } return ret;

Ex 2 struct T { ... }; int a; ... T t1(); // Function declaration whose return type is `T’ T t2(a); // Type `T’ object definition initialized with `a’

Even though giving priority to functio declaration, ... Report file of yacc/bison State X2 R21 declarator: direct_declarator . R22 direct_declarator: direct_declarator . '(' parameter_declaration_clause ')' ... '(' shift, and go to state Y2 '(' [reduce using rule R21 (declarator)] $default reduce using rule R21 (declarator) This says that, for ‘(‘ after declarator, R21 is not used: Object definition of Ex 2 cause syntax error.

Save parser state Insert code to `yyparse’ of appropriate location: if (yystate == X2 && yychar == ‘(‘ ) { if (!retry[X2] ) // At 1st time save(yystate, ...); // Save parser state else { yyn = R21 + 1; goto yyreduce; }

Ex 3 double f(); void g(int a) { int(f())+a; }

int(f())+a; Reading ‘(‘ after `int’, yystate becomes X1. Reading ‘(‘ after `f’, yystate becomes X2. Reading ‘+‘, discard state saved at X2 , and restore state saved at X1 . Note that it’s not correct to add `f’ into symbol table.

Discard the last saved state Insert code to `yyparse’ of appropriate location: if (yystate == X2) { switch (yychar) { case ‘(‘ : case ‘[‘ : ... break; // Not cause syntax error as declaration default: if (The last state is saved at X2) { Discard the last saved state; goto yyerrlab; }