Parsing — Part II (Top-down parsing, left-recursion removal)

Slides:



Advertisements
Similar presentations
Parsing V: Bottom-up Parsing
Advertisements

Parsing 4 Dr William Harrison Fall 2008
Parsing II : Top-down Parsing
Compiler Construction
Parsing III (Eliminating left recursion, recursive descent parsing)
Prof. Bodik CS 164 Lecture 61 Building a Parser II CS164 3:30-5:00 TT 10 Evans.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
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.
Compiler Construction Parsing Part I
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.
Review: –How do we define a grammar (what are the components in a grammar)? –What is a context free grammar? –What is the language defined by a grammar?
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
-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.
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.
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.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Top-Down Parsing.
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
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) )
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.
Lecture 7 Syntax Analysis (5) Operator-Precedence Parsing
CSE 3302 Programming Languages
Compiler Construction Parsing Part I
Parsing #1 Leonidas Fegaras.
lec02-parserCFG May 8, 2018 Syntax Analyzer
Parsing — Part II (Top-down parsing, left-recursion removal)
Programming Languages Translator
Bottom-up parsing Goal of parser : build a derivation
CS510 Compiler Lecture 4.
Lexical and Syntax Analysis
Context free grammars Terminals Nonterminals Start symbol productions
Unit-3 Bottom-Up-Parsing.
Parsing IV Bottom-up Parsing
Table-driven parsing Parsing performed by a finite state machine.
Syntax Analysis Chapter 4.
4 (c) parsing.
Parsing Techniques.
Top-Down Parsing.
Syntax Analysis Sections :.
Top-Down Parsing CS 671 January 29, 2008.
Lecture 8 Bottom Up Parsing
Compiler Design 7. Top-Down Table-Driven Parsing
Lecture 6 Grammar Modifications
Lecture 7: Introduction to Parsing (Syntax Analysis)
Lecture 8: Top-Down Parsing
Bottom Up Parsing.
Programming Language Syntax 5
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
Parsing IV Bottom-up Parsing
Introduction to Parsing
Introduction to Parsing
Parsing — Part II (Top-down parsing, left-recursion removal)
LL and Recursive-Descent Parsing
BNF 9-Apr-19.
LL and Recursive-Descent Parsing Hal Perkins Autumn 2009
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
lec02-parserCFG May 27, 2019 Syntax Analyzer
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Parsing — Part II (Top-down parsing, left-recursion removal)

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

Top-down Parsing 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

Remember the expression grammar? Version with precedence derived last lecture And the input x – 2 * y

Example Let’s try x – 2 * y : Goal Expr Term + Fact. <id,x> Leftmost derivation, choose productions in an order that exposes problems

Example Let’s try x – 2 * y : This worked well, except that “–” doesn’t match “+” The parser must backtrack to here Goal Expr Term + Fact. <id,x>

Example Continuing with x – 2 * y : Goal Expr Term – Fact. <id,x>

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

Example Trying to match the “2” in x – 2 * y : Goal Expr Term – Fact. <id,x> <num,2>

Example Trying to match the “2” in x – 2 * y : Where are we? “2” matches “2” We have more input, but no NTs left to expand The expansion terminated too soon Need to backtrack Goal Expr Term - Fact. <id,x> <num,2>

Example Trying again with “2” in x – 2 * y : This time, we matched & consumed all the input Success! Goal Expr Term – Fact. <id,x> <id,y> <num,2> *

Another possible parse 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 consuming no input !

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

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

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

Eliminating Left Recursion Substituting them 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.

Eliminating Left Recursion The transformation eliminates immediate left recursion What about more general, indirect left recursion ? The general algorithm: arrange the NTs into some order A1, A2, …, An for i  1 to n for s  1 to i – 1 replace each production Ai  As with Ai  12k, where As  12k are all the current productions for As eliminate any immediate left recursion on Ai using the direct transformation This assumes that the initial grammar has no cycles (Ai + Ai ), and no epsilon productions Must start with 1 to ensure that A1  A1  is transformed And back

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 Ai has no non-terminal As in its rhs, for s < i 4. Last step in outer loop converts any direct recursion on Ai 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 ith outer loop iteration For all k < i, no production that expands Ak contains a non-terminal As in its rhs, for s < k

Example Order of symbols: G, E, T G E E  E + T E  T T  E ~ T T  id

Example Order of symbols: G, E, T 1. Ai = G G E E  E + T E  T T  id

Example Order of symbols: G, E, T 1. Ai = G G E E  E + T E  T T  id 2. Ai = E G E E  T E' E'  + T E' E'  T  E ~ T T  id

Example Order of symbols: G, E, T 1. Ai = G G E E  E + T E  T T  id 2. Ai = E G E E  T E' E'  + T E' E'  T  E ~ T T  id 3. Ai = T, As = E G E E  T E' E'  + T E' E'  T  T E' ~ T T  id Go to Algorithm

Example Order of symbols: G, E, T 1. Ai = G G E E  E + T E  T T  id 2. Ai = E G E E  T E' E'  + T E' E'  T  E ~ T T  id 3. Ai = T, As = E G E E  T E' E'  + T E' E'  T  T E' ~ T T  id 4. Ai = T G E E  T E' E'  + T E' E'  T  id T' T'  E' ~ T T' T' 