Parsing Top-Down.

Slides:



Advertisements
Similar presentations
Chap. 5, Top-Down Parsing J. H. Wang Mar. 29, 2011.
Advertisements

Top-Down Parsing.
CS 310 – Fall 2006 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho,
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
Cse321, Programming Languages and Compilers 1 6/18/2015 Lecture #7, Feb. 5, 2007 Grammars Top down parsing Transition Diagrams Ambiguity Left recursion.
Parsing III (Eliminating left recursion, recursive descent parsing)
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
1 Predictive parsing Recall the main idea of top-down parsing: Start at the root, grow towards leaves Pick a production and try to match input May need.
CS 310 – Fall 2006 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho,
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 Chapter 4: Top-Down Parsing. 2 Objectives of Top-Down Parsing an attempt to find a leftmost derivation for an input string. an attempt to construct.
Professor Yihjia Tsai Tamkang University
Top-Down Parsing.
– 1 – CSCE 531 Spring 2006 Lecture 7 Predictive Parsing Topics Review Top Down Parsing First Follow LL (1) Table construction Readings: 4.4 Homework: Program.
CPSC 388 – Compiler Design and Construction
COP4020 Programming Languages Computing LL(1) parsing table Prof. Xin Yuan.
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.
Syntax and Semantics Structure of programming languages.
Parsing Chapter 4 Parsing2 Outline Top-down v.s. Bottom-up Top-down parsing Recursive-descent parsing LL(1) parsing LL(1) parsing algorithm First.
Top-Down Parsing - recursive descent - predictive parsing
Chapter 5 Top-Down Parsing.
Parsing III (Top-down parsing: recursive descent & LL(1) )
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
Ambiguity, LL1 Grammars and Table-driven Parsing
-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 5 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CSI 3120, Syntactic analysis, page 1 Syntactic Analysis and Parsing Based on A. V. Aho, R. Sethi and J. D. Ullman Compilers: Principles, Techniques and.
Syntax and Semantics Structure of programming languages.
Exercise 1 A ::= B EOF B ::=  | B B | (B) Tokens: EOF, (, ) Generate constraints and compute nullable and first for this grammar. Check whether first.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
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.
Lecture 3: Parsing CS 540 George Mason University.
1 Context free grammars  Terminals  Nonterminals  Start symbol  productions E --> E + T E --> E – T E --> T T --> T * F T --> T / F T --> F F --> (F)
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
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.
Top-Down Predictive Parsing We will look at two different ways to implement a non- backtracking top-down parser called a predictive parser. A predictive.
Parsing methods: –Top-down parsing –Bottom-up parsing –Universal.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
Bernd Fischer RW713: Compiler and Software Language Engineering.
UMBC  CSEE   1 Chapter 4 Chapter 4 (b) parsing.
Parsing III (Top-down parsing: recursive descent & LL(1) )
Exercises on Chomsky Normal Form and CYK parsing
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Bottom-up parsing. Bottom-up parsing builds a parse tree from the leaves (terminals) to the start symbol int E T * TE+ T (4) (2) (3) (5) (1) int*+ E 
Spring 16 CSCI 4430, A Milanova 1 Announcements HW1 due on Monday February 8 th Name and date your submission Submit electronically in Homework Server.
Programming Languages Translator
Context free grammars Terminals Nonterminals Start symbol productions
Top-down parsing cannot be performed on left recursive grammars.
FIRST and FOLLOW Lecture 8 Mon, Feb 7, 2005.
Parsing with Context Free Grammars
CS 404 Introduction to Compiler Design
Top-Down Parsing.
Top-Down Parsing CS 671 January 29, 2008.
CS 540 George Mason University
Compiler Design 7. Top-Down Table-Driven Parsing
Top-Down Parsing Identify a leftmost derivation for an input string
Top-Down Parsing The parse tree is created top to bottom.
Predictive Parsing Lecture 9 Wed, Feb 9, 2005.
Computing Follow(A) : All Non-Terminals
Syntax Analysis - Parsing
Chapter 3 Syntactic Analysis I.
Context Free Grammar – Quick Review
Presentation transcript:

Parsing Top-Down

Top-Down parsers The top-down parser must start at the root of the tree and determine, from the token stream, how to grow the parse tree that results in the observed tokens. This approach runs into several problems, which we will now deal with. Parsing Top-Down

Left Recursion Productions of the form (A->Aa) are left recursive. No Top-Down parser can handle left recursive grammars. Therefore we must re-write the grammar and eliminate both direct and indirect left recursion. Parsing Top-Down

How to eliminate Left Recursion (direct) Given: A -> Aa1 | Aa2 | Aa3 | … A -> d1 | d2 | d3 | ... Introduce A' A -> d1 A' | d2 A' | d3 A' | ... A' -> e | a1 A' | a2 A' | a3 A' | ... Example: S -> Sa | b Becomes S -> bS' S' -> e | a S' Parsing Top-Down

How to remove ALL Left Recursion. 1.Sort the nonterminals 2.for each nonterminal if B -> Ab and A -> g1 | g2 | g3 | ... then B -> g1b | g2b | g3b | ... 3.After all done, remove immediate left recursion. Parsing Top-Down

note: the S in A(3) -> but it is NOT left recursion Example: S -> aA | b | cS A -> Sd | e becomes A -> aAd | bd | cSd | e note: the S in A(3) -> but it is NOT left recursion Parsing Top-Down

Backtracking One way to carry out a top-down parse is simply to have the parser try all applicable productions exhaustively until it finds a tree. This is sometimes called the brute force method. It is similar to depth-first search of a graph Tokens may have to be put back on the input stream Parsing Top-Down

A Backtracking algorithm will not work properly with this grammar. Given a grammar: S -> ee | bAc | bAe A -> d | cA A Backtracking algorithm will not work properly with this grammar. Example: input string is bcde When you see a b you select S -> bAc This is wrong since the last letter is e not c Parsing Top-Down

The solution is Left Factorization. Def: Left Factorization: -- create a new non-terminal for a unique right part of a left factorable production. Left Factor the grammar given previously. S -> ee | bAQ Q -> c | e A -> d | cA Parsing Top-Down

Recursive-Descent Parsing There is one function for each non-terminal these functions try each production and call other functions for non-terminals. The stack is invisible for CFG's The problem is -- a new grammar requires new code. Parsing Top-Down

Example: Code: else if token_is ('c') then writeln ('S --> c') begin error ('S'); S := false end end; { S } Example: S -> bA | c A -> dSd | e Code: function S: boolean; begin S := true; if token_is ('b') then if A then writeln('S --> bA') else S := false; Parsing Top-Down

else A := false end if token_is ('e') then writeln ('A --> e') begin error ('A'); end; { A } function A: boolean begin A := true; if token_is ('d') then if S then writeln('A --> dSd'); else error ('A'); A := false end Parsing Top-Down

Input String: bdcd Parsing Top-Down

Parsing Top-Down

Predictive Parsers The goal of a predictive parser is to know which characters on the input string trigger which productions in building the parse tree. Backtracking can be avoided if the parser had the ability to look ahead in the grammar so as to anticipate what terminals are derivable (by leftmost derivations) from each of the various nonterminals on the RHS. Parsing Top-Down

First (a) (you construct first() of RHS’s) 1.if a begins with a terminal x, then first(a) = x. 2.if a =*=> e, then first(a) includes e. 3.First(e) = {e}. 4.if a begins with a nonterminal A, then first(a) includes first(A) - {e} Parsing Top-Down

Follow(a) 1.if A is the start symbol, then put the end marker $ into follow(A). 2.for each production with A on the right hand side Q -> xAy A) if y begins with a terminal q, q is in follow(A). else follow(A) includes first(y)-e. B) if y = e, or y is nullable (y =*=> e) then add follow(Q) to follow(A). Parsing Top-Down

Construction of First and Follow Sets: Grammar: E -> T Q Q -> + T Q | - T Q | epsilon T -> F R R -> * F R | / F R | epsilon F -> ( E ) | I Construction of First and Follow Sets: Parsing Top-Down

First(Q->+TQ) = {+} First(Q->-TQ) = {-} First(Q-> e) = {e} First(E->TQ) = First(T) = First(F) = {i,(} First(Q->+TQ) = {+} First(Q->-TQ) = {-} First(Q-> e) = {e} First(R-> *FR) = {*} First(R->/FR) = {/} First(R-> e) = {e} First(F-> (E) ) = {(} First(F-> I) = {I} Follow(E) = {$,)} Follow(Q) = Follow(E) = {$, )} Follow(T) = First(Q) - e + Follow(E) = {+,-} + {),$} Follow(R) = Follow(T) Follow(F) = First(R) - e + Follow(T) = {*,/} + {+,-,),$} Parsing Top-Down

First(E) = First(T) = First(F) = {i,(} First(Q) = {+,-,e} First(R) = {*,/,e} Follow(E) = {$,)} Follow(Q) = Follow(E) = {$, )} Follow(T) = First(Q) - e + Follow(E) {+,-} + {),$} Follow(R) = Follow(T) Follow(F) = First(R) - e + Follow(T) {*,/} + {+,-,),$} Parsing Top-Down

LL(1) Grammars In a predictive parser, Follow tells us when to use the epsilon productions. Def: LL(1) -- Left to Right Scan of the tokens, Leftmost derivation, 1 token lookahead. Parsing Top-Down

For a grammar to be LL(1), we require that for every pair of productions A -> a|b 1.First(a)-e and First(b)-e must be disjoint. 2.if a is nullable, then First(b) and Follow(A) must be disjoint. if rule 1 is violated, we may not know which right hand side to choose if rule 2 is violated, we may not know when to choose b or e. Parsing Top-Down

Parsing Top-Down

Table-Driven Predictive Parsers Grammar E -> E + T | E - T | T T -> T * F | T / F | F F -> ( E ) | I Step 1: Eliminate Left Recursion. Parsing Top-Down

Grammar without left recursion E -> T Q Q -> + T Q | - T Q | epsilon T -> F R R -> * F R | / F R | epsilon F -> ( E ) | I It is easier to show you the table, and how it is used first, and to show how the table is constructed afterward. Parsing Top-Down

Table Parsing Top-Down

Driver Algorithm: Push $ onto the stack Put a similar end marker on the end of the string. Push the start symbol onto the stack. Parsing Top-Down

While (stack not empty do) Let x = top of stack and a = incoming token. If x is in T (a terminal) if x == a then pop x and goto next input token else error else (nonterminal) if Table[x,a] pop x push Table[x,a] onto stack in reverse order It is a successful parse if the stack is empty and the input is used up. Parsing Top-Down

Example 1: (i+i)*i Parsing Top-Down

Parsing Top-Down

Parsing Top-Down

Example 2: (i*) Parsing Top-Down

Parsing Top-Down

Constructing the Predictive Parser Table Go through all the productions. X -> b is your typical production. 1.For all terminals a in First(b), except e, Table[X,a] = b. 2.If b = e, or if e is in first(b) then For ALL a in Follow(X), Table[X,a] = e. So, Construct First and Follow for all Left and right hand sides. Parsing Top-Down

Conflicts A conflict occurs if there is more than 1 entry in a table slot. This can sometimes be fixed by Left Factoring, ... If a grammar is LL(1) there will not be multiple entries. Parsing Top-Down

Summary of Top Down Left Recursion Left Factorization First (A) Follow (A) Predictive Parsers (table driven) Parsing Top-Down