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

Slides:



Advertisements
Similar presentations
Compiler Construction
Advertisements

Grammar and Algorithm }
Lecture # 11 Grammar Problems.
Top-Down Parsing.
Pertemuan 9, 10, 11 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.
Discussion #51/18 Discussion #5 LL(1) Grammars &Table-Driven Parsing.
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.
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.
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
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.
Ambiguity, LL1 Grammars and Table-driven Parsing
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
1 Compiler Construction Syntax Analysis Top-down parsing.
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.
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 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)
Top-Down Parsing.
Chapter 3 Chang Chi-Chung
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.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Chapter 2 (part) + Chapter 4: Syntax Analysis S. M. Farhad 1.
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.
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.
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.
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
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.
Top-Down Parsing.
Parsing Techniques.
Lecture 7 Predictive Parsing
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
Nonrecursive Predictive Parsing
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Predictive Parsing Program
Presentation transcript:

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

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

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

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

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

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

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

8 Predictive Parsing Table id +*()$ EE → TE' E'E' → +TE' E' →  TT → FT' T' T' →  T → *FT' T' →  FF → idF → (E ) Rows for current non-terminal to expand Columns for next token

9 Predictive Parsing Table id +*()$ EE → TE' E'E' → +TE' E' →  TT → FT' T' T' →  T → *FT' T' →  FF → idF → (E ) Table entries are productions Blank entries are errors

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

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

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

13 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

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

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

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

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

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

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

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

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

22 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).

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

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

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

26 id E +  $ $ stack Parsing Table M

27 Predictive Parsing Table id +*()$ EE → TE' E'E' → +TE' E' →  TT → FT' T' T' →  T → *FT' T' →  FF → idF → (E )

Compiler Construction Sohail Aslam Lecture 17

29 id E +  $ $ stack Parsing Table M E → TE'

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

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

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

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

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

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

36 StackInputOuput $E$E id+id  id$ $E' T id+id  id$ E → TE' $E' T' F id+id  id$ T → FT' $E'T' id id+id  id$ F → id $E' T' +id  id$ $E' +id  id$T' →  $E' T + +id  id$ E' → +TE'

37 StackInputOuput $E' T id  id$ $E' T' F id  id$ T → FT' $E' T' id id  id$ F → id $E' T'  id$ $E' T' F  id$T →  FT' $E' T' Fid$ $E'T' idid$F → id

38 StackInputOuput $E' T'$ $E'$ T' →  $$ E' → 

39 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

40 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

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

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

43 id+id  id E T E' F id T'  E' T + and so on....

44 E T E' F id T'  E' T + The leaves at any point form a string  A 

45 E T E' F id T'  E' T +  only contains terminals

46 E T E' F id T'  + E' T id + id  id  b  input string is  b  The prefix  matches Next token is b

47 LL(1) Table Construction  Consider the state S →   A  with b the next token and we are trying to match  b   There are two possibilities

48 LL(1) Table Construction  Consider the state S →   A  with b the next token and we are trying to match  b   There are two possibilities

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

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

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

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

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

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

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

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

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