Chapter 4 Syntax Analysis.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Bottom up Parsing Bottom up parsing trys to transform the input string into the start symbol. Moves through a sequence of sentential forms (sequence of.
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter
Lecture #8, Feb. 7, 2007 Shift-reduce parsing,
1 Bottom-up parsing Goal of parser : build a derivation –top-down parser : build a derivation by working from the start symbol towards the input. builds.
Parser construction tools: YACC
Compilers: Yacc/7 1 Compiler Structures Objective – –describe yacc (actually bison) – –give simple examples of its use , Semester 1,
LALR Parsing Canonical sets of LR(1) items
LEX and YACC work as a team
Using the LALR Parser Generator yacc By J. H. Wang May 10, 2011.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Miscellaneous 컴파일러 입문.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Ambiguity in Grammar By Dipendra Pratap Singh 04CS1032.
–Writing a parser with YACC (Yet Another Compiler Compiler). Automatically generate a parser for a context free grammar (LALR parser) –Allows syntax direct.
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. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Yet Another Compiler-Compiler Stephen C. Johnson July 31, 1978 YACC.
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.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 6: LR grammars and automatic parser generators.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Lecture 5: LR Parsing CS 540 George Mason University.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
YACC (Yet Another Compiler-Compiler) Chung-Ju Wu
Eliminating Left-Recursion Where some of a nonterminal’s productions are left-recursive, top-down parsing is not possible “Immediate” left-recursion can.
1 Syntax Analysis Part III Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Overview of Previous Lesson(s) Over View  Structure of the LR Parsing Table  Consists of two parts: a parsing-action function ACTION and a goto function.
Syntax error handling –Errors can occur at many levels lexical: unknown operator syntactic: unbalanced parentheses semantic: variable never declared runtime:
Syntax Analysis Part III
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
Unit-3 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
Syntax Analysis Chapter 4.
Compiler design Bottom-up parsing: Canonical LR and LALR
LALR Parsing Canonical sets of LR(1) items
Bottom-Up Syntax Analysis
Syntax Analysis Part III
Syntax Analysis Part II
Syntax Analysis Sections :.
Subject Name:COMPILER DESIGN Subject Code:10CS63
Lexical and Syntax Analysis
Top-Down Parsing CS 671 January 29, 2008.
Syntax Analysis Part III
Bison Marcin Zubrowski.
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
4d Bottom Up Parsing.
Parsing #2 Leonidas Fegaras.
Syntax Analysis Part III
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Subject Name:Sysytem Software Subject Code: 10SCS52
Bottom Up Parsing.
LALR Parsing Adapted from Notes by Profs Aiken and Necula (UCB) and
Ambiguity in Grammar, Error Recovery
Syntax Analysis Part III
Parsing #2 Leonidas Fegaras.
Syntax Analysis - 3 Chapter 4.
Compiler Lecture Note, Miscellaneous
Compiler Structures 7. Yacc Objectives , Semester 2,
USING AMBIGUOUS GRAMMARS
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Predictive Parsing Program
Chap. 3 BOTTOM-UP PARSING
Compiler design Bottom-up parsing: Canonical LR and LALR
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Chapter 4 Syntax Analysis

Content Overview of this chapter 4.1 Introduction 4.2 Context-Free Grammars 4.3 Writing a Grammar 4.4 Top-Down Parsing 4.5 Bottom-Up Parsing 4.6 Introduction to LR Parsing: Simple LR 4.7 More Powerful LR Parsers 4.8 Using Ambiguous Grammars 4.9 Parser Generators

4.8 Using Ambiguous Grammars

4.8 Using Ambiguous Grammars Why use ambiguous grammars? Certain ambiguous grammars are quite useful 1. Provides a shorter, more natural specification 2. In isolating commonly occurring syntactic constructs for special-case optimization We specify disambiguating rules that allow only one parse tree for each sentence

4.8.1 Precedence and Associativity to Resolve Conflicts Consider the ambiguous grammar (4.3) E-> E+E | E*E | (E) | id LR(0) items: (I7 and I8 have conflicts)

4.8.1 Precedence and Associativity to Resolve Conflicts Using the precedence and associativity information for + and *, consider: id + id * id Parsing table:

4.8.2 The "Dangling-Else" Ambiguity Consider again the grammar: rewrite as: LR(0) items: (shift/reduce conflict in l4)

4.8.3 Error Recovery in LR Parsing Detect an error: Consults the parsing action table and finds an error entry (Never by consulting the goto table) Error Recovery in LR Parsing 1. Panic-mode: Eliminate the phrase containing the syntactic errors 2. Phrase-level recovery: Examine each error entry

4.8.3 Error Recovery in LR Parsing Panic-mode: scan down the stack until a state s with a goto on a particular nonterminal A is found. Zero or more input symbols are then discarded until a symbol a is found that can legitimately follow A. The parser then stacks the state GOTO(SA, ) and resumes normal parsing. There might be more than one choice for the nonterminal A. Phrase-level : examining each error entry in the LR parsing table and deciding on the basis of language usage the most likely programmer error that would give rise to that error.

4.8.3 Error Recovery in LR Parsing el: This routine is called from states 0, 2, 4 and 5, all of which expect the beginning of an operand. push state 3 (the goto of states 0, 2, 4 and 5 on id); issue diagnostic "missing operand."

4.9 Parser Generators

In this section, we 4.9 Parser Generators Show how a parser generator can be used Use the LALR parser generator Yacc as the basis of our discussion

4.9.1 The Parser Generator Yacc Creating an input/output translator with Yacc: A Yacc source program has three parts: declarations %% translation rules supporting C routines

4.9.1 The Parser Generator Yacc The Declarations Part two sections: both optional 1. Ordinary C declarations: %{ and %} 2. Declarations of grammar tokens: %token DIGIT

4.9.1 The Parser Generator Yacc The Translation Rules Part <head> : <body>1 {<semantic action>1} | <body>2 {<semantic action>2} … | <body>n {<semantic action>n} ; The Supporting C-Routines Part The lexical analyzer yylex() produces tokens consisting of a token name and its associated attribute value. If a token name such as DIGIT is returned, the token name must be declared in the first section of the Yacc specification. The attribute value associated with a token is communicated to the parser through a Yacc-defined variable yylval.

4.9.1 The Parser Generator Yacc

4.9.2 Using Yacc with Ambiguous Grammars Modify the Yacc specification as

4.9.2 Using Yacc with Ambiguous Grammars Two rules of resolve conflicts : 1. A reduce/reduce conflict is resolved by choosing the conflicting production listed first 2. A shift/reduce conflict is resolved in favor of shift The tokens are given precedences in the order in which they appear in the declarations part, lowest first. Tokens in the same declaration have the same precedence.

4.9.3 Creating Yacc Lexical Analyzers with Lex Lex was designed to produce lexical analyzers that could be used with Yacc Replace the routine yylex() #include “lex.yy.c”

Error recovery use a form of error productions: 4.9.4 Error Recovery in Yacc Error recovery use a form of error productions: 1. The user decides what "major“ nonterminals will have error recovery 2. The user adds to the grammar error productions of the form A -> error , error is a yacc reserved word. A is a major nonterminal and  is a string of grammar symbols, perhaps the empty string. 3. Yacc generates a parser from such a specification.

4.9.4 Error Recovery in Yacc On encountering an error, Yacc pops symbols from its stack until it finds the topmost state on its stack whose underlying set of items includes an item of the form A -> error . The parser then "shifts" a fictitious token error onto the stack, as though it saw the token error on its input.

4.9.4 Error Recovery in Yacc the parser starts popping symbols from its stack until it encounters a state that has a shift action on the token error. State 0 is such a state (in this example, it's the only such state) The parser shifts the token error onto the stack, and then proceeds to skip ahead in the input until it has found a newline character.

The end of Lecture07