Nonrecursive Predictive Parsing

Slides:



Advertisements
Similar presentations
Compiler Construction
Advertisements

Grammar and Algorithm }
Top-Down 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.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
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.
Top-Down parsing LL(1) parsing. Overview of Top-Down  There are only two actions 1.Replace 2.Match.
Professor Yihjia Tsai Tamkang University
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
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
Compiler Construction Sohail Aslam Lecture LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.
COP4020 Programming Languages Computing LL(1) parsing table Prof. Xin Yuan.
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
4 4 (c) parsing. Parsing A grammar describes the strings of tokens that are syntactically legal in a PL A recogniser simply accepts or rejects strings.
Chapter 5 Top-Down Parsing.
4 4 (c) parsing. Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces.
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
1 Compiler Construction Syntax Analysis Top-down parsing.
4 4 (c) parsing. Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces.
1 Problems with Top Down Parsing  Left Recursion in CFG May Cause Parser to Loop Forever.  Indeed:  In the production A  A  we write the program procedure.
Pembangunan Kompilator.  The parse tree is created top to bottom.  Top-down parser  Recursive-Descent Parsing ▪ Backtracking is needed (If a choice.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Top-Down Parsing The parse tree is created top to bottom. Top-down parser –Recursive-Descent Parsing.
LL(1) Parser. What does LL signify ? The first L means that the scanning takes place from Left to right. The first L means that the scanning takes place.
Top-down Parsing Recursive Descent & LL(1) Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
Top-Down Parsing.
Parsing methods: –Top-down parsing –Bottom-up parsing –Universal.
Bernd Fischer RW713: Compiler and Software Language Engineering.
UMBC  CSEE   1 Chapter 4 Chapter 4 (b) parsing.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Syntax and Semantics Structure of programming languages.
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.
9/30/2014IT 3271 How to construct an LL(1) parsing table ? 1.S  A S b 2.S  C 3.A  a 4.C  c C 5.C  abc$ S1222 A3 C545 LL(1) Parsing Table What is the.
Problems with Top Down Parsing
Compiler Construction
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
UNIT - 3 SYNTAX ANALYSIS - II
Lecture #12 Parsing Types.
Table-driven parsing Parsing performed by a finite state machine.
Compiler Construction
Introduction to Top Down Parser
Top-down parsing cannot be performed on left recursive grammars.
SYNTAX ANALYSIS (PARSING).
CS 404 Introduction to Compiler Design
Top-Down Parsing.
4 (c) parsing.
Parsing Techniques.
Subject Name:COMPILER DESIGN Subject Code:10CS63
Lecture 7 Predictive Parsing
4d Bottom Up Parsing.
BOTTOM UP PARSING Lecture 16.
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.
Chapter 4 Top Down Parser.
Ambiguity, Precedence, Associativity & Top-Down Parsing
Predictive Parsing Lecture 9 Wed, Feb 9, 2005.
Computing Follow(A) : All Non-Terminals
Syntax Analysis - Parsing
Lecture 7 Predictive Parsing
4d Bottom Up Parsing.
Compiler Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Predictive Parsing Program
Presentation transcript:

Nonrecursive Predictive Parsing It is possible to build a nonrecursive predictive parser This is done by maintaining an explicit stack

Nonrecursive Predictive Parsing It is possible to build a nonrecursive predictive parser This is done by maintaining an explicit stack

Table-driven Parsers The nonrecursive LL(1) parser looks up the production to apply by looking up a parsing table

Table-driven Parsers LL(1) table: One dimension for current non-terminal to expand One dimension for next token Table entry contains one production

Table-driven Parsers LL(1) table: One dimension for current non-terminal to expand One dimension for next token Table entry contains one production

Table-driven Parsers LL(1) table: One dimension for current non-terminal to expand One dimension for next token Table entry contains one production

* F T' Consider the expression grammar 1 E → T E' 2 E' + T E' 3 | e 4 5 T' * F T' 6 7 F ( E ) 8 id

Predictive Parsing Table id + * ( ) $ E E →TE' E' E' → +TE' E' →e T T →FT' T' T' → e T →*FT' T' →e F F → id F →(E ) Columns for next token Rows for current non-terminal to expand

Predictive Parsing Table id + * ( ) $ E E →TE' E' E' → +TE' E' →e T T →FT' T' T' → e T →*FT' T' →e F F → id F →(E ) Blank entries are errors Table entries are productions

Predictive Parsers The predictive parser uses an explicit stack to keep track of pending non-terminals It can thus be implemented without recursion.

Predictive Parsers The predictive parser uses an explicit stack to keep track of pending non-terminals It can thus be implemented without recursion.

Predictive Parsers a input + b $ stack X Predictive parser output Y Z Parsing table M

LL(1) Parsing Algorithm The input buffer contains the string to be parsed; $ is the end-of-input marker The stack contains a sequence of grammar symbols

LL(1) Parsing Algorithm Initially, the stack contains the start symbol of the grammar on the top of $.

LL(1) Parsing Algorithm The parser is controlled by a program that behaves as follows:

LL(1) Parsing Algorithm The program considers X, the symbol on top of the stack, and a, the current input symbol.

LL(1) Parsing Algorithm These two symbols, X and a determine the action of the parser. There are three possibilities.

LL(1) Parsing Algorithm These two symbols, X and a determine the action of the parser. There are three possibilities.

LL(1) Parsing Algorithm 1. X  a  $, the parser halts and annouces successful completion.

LL(1) Parsing Algorithm 2. X  a  $ the parser pops X off the stack and advances input pointer to next input symbol

LL(1) Parsing Algorithm 3. If X is a nonterminal, the program consults entry M[X,a] of parsing table M.

LL(1) Parsing Algorithm If the entry is a production M[X,a] = {X → UVW } the parser replaces X on top of the stack by WVU (with U on top).

LL(1) Parsing Algorithm As output, the parser just prints the production used: X → UVW However, any other code could be executed here.

LL(1) Parsing Algorithm If M[X,a] =error, the parser calls an error recovery routine

LL(1) Parsing Algorithm Example: Let’s parse the input string id+idid using the nonrecursive LL(1) parser

id + id  id $ E $ Parsing Table M stack

Predictive Parsing Table id + * ( ) $ E E →TE' E' E' → +TE' E' →e T T →FT' T' T' → e T →*FT' T' →e F F → id F →(E )

Compiler Construction Sohail Aslam Lecture 17 compiler: intro

id + id  id $ E $ E → T E' Parsing Table M stack

id + id  id $ T E' $ T → F T' Parsing Table M stack end of lec 16 compiler: intro

id + id  id $ F T' E' $ F → id Parsing Table M stack

id + id  id $ id T' E' $ Parsing Table M stack

+ id id  id $ T' E' $ T' →  Parsing Table M stack

+ id id  id $ E' $ E' → + T E' Parsing Table M stack

+ id id  id $ + T E' $ Parsing Table M stack

Stack Input Ouput $E id+idid$ $E' T E →TE' $E' T' F T →FT' $E'T' id F → id $E' T' +idid$ $E' T' →e $E' T + E' → +TE'

Stack Input Ouput $E' T idid$ $E' T' F T →FT' $E' T' id F → id $E' T' id$ $E' T' F  T → FT' id$ $E'T' id

Stack Input Ouput $E' T' $ $E' T' →e E' →e

LL(1) Parsing Algorithm Note that productions output are tracing out a lefmost derivation The grammar symbols on the stack make up left-sentential forms

LL(1) Parsing Algorithm Note that productions output are tracing out a lefmost derivation The grammar symbols on the stack make up left-sentential forms

LL(1) Table Construction Top-down parsing expands a parse tree from the start symbol to the leaves Always expand the leftmost non-terminal

LL(1) Table Construction Top-down parsing expands a parse tree from the start symbol to the leaves Always expand the leftmost non-terminal

id+idid E T E' F T' + T E' id e and so on ....

The leaves at any point form a string b A g id T' e + The leaves at any point form a string b A g

b only contains terminals F id T' e + b only contains terminals

id + id  id input string is bbd The prefix b matches Next token is b

LL(1) Table Construction Consider the state S → bAg with b the next token and we are trying to match bbg There are two possibilities

LL(1) Table Construction Consider the state S → bAg with b the next token and we are trying to match bbg There are two possibilities

LL(1) Table Construction b belongs to an expansion of A Any A → a can be used if b can start a string derived from a

LL(1) Table Construction b belongs to an expansion of A Any A → a can be used if b can start a string derived from a

LL(1) Table Construction In this case we say that b  FIRST(a)

LL(1) Table Construction b does not belong to an expansion of A Expansion of A is empty, i.e., A → e and b belongs an expansion of g, e.g., bw

LL(1) Table Construction b does not belong to an expansion of A Expansion of A is empty, i.e., A → e and b belongs an expansion of g, e.g., bw

LL(1) Table Construction which means that b can appear after A in a derivation of the form S → bAbw

LL(1) Table Construction We say that b  FOLLOW(A)

LL(1) Table Construction Any A → a can be used if a expands to e We say that e  FIRST(A) in this case

Computing FIRST Sets Definition FIRST(X) = { b | X → ba }  { e | X → e }