Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)

Slides:



Advertisements
Similar presentations
Parsing V: Bottom-up Parsing
Advertisements

Parsing II : Top-down Parsing
Compiler Construction
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
Prof. Bodik CS 164 Lecture 81 Grammars and ambiguity CS164 3:30-5:00 TT 10 Evans.
Parsing III (Eliminating left recursion, recursive descent parsing)
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
Prof. Bodik CS 164 Lecture 61 Building a Parser II CS164 3:30-5:00 TT 10 Evans.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
1 The Parser Its job: –Check and verify syntax based on specified syntax rules –Report errors –Build IR Good news –the process can be automated.
1 CMPSC 160 Translation of Programming Languages Fall 2002 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #5 Introduction.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
1 CIS 461 Compiler Design & Construction Fall 2012 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #12 Parsing 4.
Parsing II : Top-down Parsing Lecture 7 CS 4318/5331 Apan Qasem Texas State University Spring 2015 *some slides adopted from Cooper and Torczon.
MIT Top-Down Parsing Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
Compiler Construction Parsing Part I
EECS 6083 Intro to Parsing Context Free Grammars
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
Parsing IV Bottom-up Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
Introduction to Parsing Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Parsing III (Top-down parsing: recursive descent & LL(1) )
-Mandakinee Singh (11CS10026).  What is parsing? ◦ Discovering the derivation of a string: If one exists. ◦ Harder than generating strings.  Two major.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Top Down Parsing - Part I Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Bottom-up Parsing, Part I Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Top-down Parsing Recursive Descent & LL(1) Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
1 CIS 461 Compiler Design and Construction Fall 2012 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #8 Parsing Techniques.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Top-Down Parsing.
Syntax Analyzer (Parser)
1 Pertemuan 7 & 8 Syntax Analysis (Parsing) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
1 CMPSC 160 Translation of Programming Languages Fall 2002 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #6 Parsing.
Parsing III (Top-down parsing: recursive descent & LL(1) )
Lecture # 10 Grammar Problems. Problems with grammar Ambiguity Left Recursion Left Factoring Removal of Useless Symbols These can create problems for.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
Introduction to Parsing
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
Compiler Construction Parsing Part I
lec02-parserCFG May 8, 2018 Syntax Analyzer
Parsing — Part II (Top-down parsing, left-recursion removal)
CS510 Compiler Lecture 4.
Parsing IV Bottom-up Parsing
Parsing — Part II (Top-down parsing, left-recursion removal)
Parsing Techniques.
Top-Down Parsing CS 671 January 29, 2008.
Lecture 7: Introduction to Parsing (Syntax Analysis)
Lecture 8: Top-Down Parsing
R.Rajkumar Asst.Professor CSE
Parsing IV Bottom-up Parsing
Introduction to Parsing
Introduction to Parsing
Parsing — Part II (Top-down parsing, left-recursion removal)
BNF 9-Apr-19.
lec02-parserCFG May 27, 2019 Syntax Analyzer
Presentation transcript:

Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)

from Cooper & Torczon2 Ambiguous Grammars Definitions If a grammar has more than one leftmost derivation for a single sentential form, the grammar is ambiguous If a grammar has more than one rightmost derivation for a single sentential form, the grammar is ambiguous The leftmost and rightmost derivations for a sentential form may differ, even in an unambiguous grammar Classic example — the if-then-else problem Stmt  if Expr then Stmt | if Expr then Stmt else Stmt | … other stmts … This ambiguity is entirely grammatical in nature

from Cooper & Torczon3 Ambiguity This sentential form has two derivations (If a derivation has more than 1 parse tree, the grammar is ambiguous if Expr 1 then if Expr 2 then Stmt 1 else Stmt 2 then else if then if E1E1 E2E2 S2S2 S1S1 production 2, then production 1 then if then if E1E1 E2E2 S1S1 else S2S2 production 1, then production 2

from Cooper & Torczon4 Ambiguity Removing the ambiguity Must rewrite the grammar to avoid generating the problem Match each else to innermost unmatched if (common sense rule) With this grammar, the example has only one derivation

from Cooper & Torczon5 Ambiguity if Expr 1 then if Expr 2 then Stmt 1 else Stmt 2 This binds the else controlling S 2 to the inner if

from Cooper & Torczon6 Deeper Ambiguity Ambiguity usually refers to confusion in the CFG Overloading can create deeper ambiguity a = f(17) In some languages, f could be either a function or a subscripted variable Disambiguating this one requires context Need values of declarations Really an issue of type, not context-free syntax Requires an extra-grammatical solution (not in CFG ) Must to handle these with a different mechanism  Step outside grammar rather than use a more complex grammar

from Cooper & Torczon7 Ambiguity - the Final Word Ambiguity arises from two distinct sources Confusion in the context-free syntax (if-then-else) Confusion that requires context to resolve (overloading) Resolving ambiguity To remove context-free ambiguity, rewrite the grammar To handle context-sensitive ambiguity takes cooperation  Knowledge of declarations, types, …  Accept a superset of L(G) & check it with other means †  This is a language design problem Sometimes, the compiler writer accepts an ambiguous grammar  Parsing techniques that “do the right thing” † See Chapter 4

from Cooper & Torczon8 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production & try to match the input Bad “pick”  may need to backtrack Some grammars are backtrack-free (predictive parsing) Bottom-up parsers (LR(1), operator precedence) Start at the leaves and grow toward root As input is consumed, encode possibilities in an internal state Start in a state valid for legal first tokens Bottom-up parsers handle a large class of grammars

from Cooper & Torczon9 A top-down parser starts with the root of the parse tree The root node is labeled with the goal symbol of the grammar Top-down parsing algorithm: Construct the root node of the parse tree Repeat until the fringe of the parse tree matches the input string  At a node labeled A, select a production with A on its lhs and, for each symbol on its rhs, construct the appropriate child  When a terminal symbol is added to the fringe and it doesn’t match the fringe, backtrack  Find the next node to be expanded (label  NT) The key is picking the right production in step 1  That choice should be guided by the input string Top-down Parsing

from Cooper & Torczon10 Remember the expression grammar? And the input x - 2 * y Version with precedence derived last lecture

from Cooper & Torczon11 Let’s try x - 2 * y : Example Goal Expr Term + Expr Term Fact.

from Cooper & Torczon12 Let’s try x - 2 * y : This worked well, except that “-” doesn’t match “+” The parser must backtrack to here Example Goal Expr Term + Expr Term Fact.

from Cooper & Torczon13 Example Continuing with x - 2 * y : Goal Expr Term - Expr Term Fact.

from Cooper & Torczon14 Example Continuing with x - 2 * y : Goal Expr Term - Expr Term Fact. We can advance past “-” to look at “2” This time, “-” and “-” matched  Now, we need to expand Term - the last NT on the fringe

from Cooper & Torczon15 Example Trying to match the “2” in x - 2 * y : Goal Expr Term - Expr Term Fact. Fact.

from Cooper & Torczon16 Example Trying to match the “2” in x - 2 * y : Where are we? “2” matches “2” We have more input, but no NT s left to expand The expansion terminated too soon  Need to backtrack Goal Expr Term - Expr Term Fact. Fact.

from Cooper & Torczon17 Example Trying again with “2” in x - 2 * y : This time, we matched & consumed all the input  Success! Goal Expr Term - Expr Term Fact. Fact. Term Fact. *

from Cooper & Torczon18 Other choices for expansion are possible This doesn’t terminate (obviously) Wrong choice of expansion leads to non-termination Non-termination is a bad property for a parser to have Parser must make the right choice Another possible parse consuming no input !

from Cooper & Torczon19 Left Recursion Top-down parsers cannot handle left-recursive grammars Formally, A grammar is left recursive if  A  NT such that  a derivation A  + A , for some string   (NT  T ) + Our expression grammar is left recursive This can lead to non-termination in a top-down parser For a top-down parser, any recursion must be right recursion We would like to convert the left recursion to right recursion Non-termination is a bad property in any part of a compiler

from Cooper & Torczon20 Eliminating Left Recursion To remove left recursion, we can transform the grammar Consider a grammar fragment of the form Fee  Fee  |  where neither  nor  start with Fee We can rewrite this as Fee   Fie Fie   Fie |  where Fie is a new non-terminal This accepts the same language, but uses only right recursion

from Cooper & Torczon21 Eliminating Left Recursion The expression grammar contains two cases of left recursion Applying the transformation yields These fragments use only right recursion They retains the original left associativity

from Cooper & Torczon22 Eliminating Left Recursion Substituting back into the grammar yields This grammar is correct, if somewhat non-intuitive. It is left associative, as was the original A top-down parser will terminate using it. A top-down parser may need to backtrack with it.

from Cooper & Torczon23 Eliminating Left Recursion The transformation eliminates immediate left recursion What about more general, indirect left recursion The general algorithm: arrange the NT s into some order A 1, A 2, …, A n for i  1 to n replace each production A i  A s  with A i   1  2  k , where A s   1  2  k are all the current productions for A s eliminate any immediate left recursion on A i using the direct transformation This assumes that the initial grammar has no cycles (A i  + A i ), and no epsilon productions

from Cooper & Torczon24 Eliminating Left Recursion How does this algorithm work? 1. Impose arbitrary order on the non-terminals 2. Outer loop cycles through NT in order 3. Inner loop ensures that a production expanding A i has no non- terminal A s in its rhs, for s < I 4. Last step in outer loop converts any direct recursion on A i to right recursion using the transformation showed earlier 5. New non-terminals are added at the end of the order & have no left recursion At the start of the i th outer loop iteration For all k < I, no production that expands A k contains a non-terminal A s in its rhs, for s < k