Chapter 6: Top-Down Parser1 Compiler Designs and Constructions Chapter 6: Top-Down Parser Objectives: Types of Parsers Backtracking Vs. Non-backtracking.

Slides:



Advertisements
Similar presentations
Parsing V: Bottom-up Parsing
Advertisements

A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
Compiler Designs and Constructions
Chapter 5: Languages and Grammar 1 Compiler Designs and Constructions ( Page ) Chapter 5: Languages and Grammar Objectives: Definition of Languages.
Chap. 5, Top-Down Parsing J. H. Wang Mar. 29, 2011.
Lecture # 11 Grammar Problems.
Mooly Sagiv and Roman Manevich School of Computer Science
Top-Down Parsing.
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
ISBN Chapter 4 Lexical and Syntax Analysis.
ISBN Chapter 4 Lexical and Syntax Analysis.
Slide1 Chapter 4 Lexical and Syntax Analysis. slide2 OutLines: In this chapter a major topics will be discussed : Introduction to lexical analysis, including.
Parsing III (Eliminating left recursion, recursive descent parsing)
COS 320 Compilers David Walker. last time context free grammars (Appel 3.1) –terminals, non-terminals, rules –derivations & parse trees –ambiguous grammars.
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
CS 310 – Fall 2006 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho,
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
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
COS 320 Compilers David Walker. last time context free grammars (Appel 3.1) –terminals, non-terminals, rules –derivations & parse trees –ambiguous grammars.
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.
Chapter 4 Lexical and Syntax Analysis. Chapter 4 Topics Introduction Lexical Analysis The Parsing Problem Recursive-Descent Parsing Bottom-Up Parsing.
Lexical and syntax analysis
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. Goals of Parsing Check the input for syntactic accuracy Return appropriate error messages Recover if possible Produce, or at least traverse,
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.
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?
Top-Down Parsing - recursive descent - predictive parsing
Chapter 5 Top-Down Parsing.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
Ambiguity, LL1 Grammars and Table-driven Parsing
1 Compiler Construction Syntax Analysis Top-down parsing.
Syntax and Semantics Structure of programming languages.
6/4/2016IT 3271 The most practical Parsers: Predictive parser: 1.input (token string) 2.Stacks, parsing table 3.output (syntax tree, intermediate codes)
ISBN Chapter 4 Lexical and Syntax Analysis.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Chapter 4 Top-Down Parsing Recursive-Descent Gang S. Liu College of Computer Science & Technology Harbin Engineering University.
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
Parsing Top-Down.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
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 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.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Parsing III (Top-down parsing: recursive descent & LL(1) )
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Lecture # 10 Grammar Problems. Problems with grammar Ambiguity Left Recursion Left Factoring Removal of Useless Symbols These can create problems for.
Syntax and Semantics Structure of programming languages.
Chapter 4 - Parsing CSCE 343.
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
Lecture #12 Parsing Types.
Table-driven parsing Parsing performed by a finite state machine.
Compiler Construction
Parsing with Context Free Grammars
4 (c) parsing.
Lexical and Syntax Analysis
Syntax-Directed Definition
Predictive Parsing Lecture 9 Wed, Feb 9, 2005.
Syntax Analysis - Parsing
Nonrecursive Predictive Parsing
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Chapter 6: Top-Down Parser1 Compiler Designs and Constructions Chapter 6: Top-Down Parser Objectives: Types of Parsers Backtracking Vs. Non-backtracking PDA Dr. Mohsen Chitsaz

Chapter 6: Top-Down Parser2 Definition: Alphabet: Set of Char (token) Language: Set of Token Sentence: a group of token Grammar: a set of (productions) that control the order of the occurrence of words Syntax: a set of rules Parser: software to check the syntax Parsing: Process of breaking down the lexemes

Chapter 6: Top-Down Parser3 Types of Parser Universal Parsing Method example: Cocke-Younger-Kasami Algorithm Can parse any grammar too inefficient to use in compiler production Top-Down Parsing Left most derivation Bottom-up Parsing Right most derivation

Chapter 6: Top-Down Parser4 Top-Down Parsing Non-backtracking Vs. Backtracking Large Plant Green Plant Recursive Descent Parsing (Predictive Parser) We execute a set of recursive procedures to process the input There is one procedure associated with each non-terminal of a grammar

Chapter 6: Top-Down Parser5 Example   ^id  array [ ] of integer  char  num.. num

Chapter 6: Top-Down Parser6 Procedure Match (T:token); Begin If LookaHead==T then LookaHead==NextToken Else Error(); End;

Chapter 6: Top-Down Parser7 Procedure Type( ); Begin If LookaHead in {integer, char, num} then Simple(); Else If LookaHead == ‘^’ then Begin Match (‘^’); Match (id); End Else If LookaHead == array then

Chapter 6: Top-Down Parser8 Begin Match (array); Match (‘[‘); Simple(); Match (‘]’); Match (of); Type(); End Else Error(); End;

Chapter 6: Top-Down Parser9 Procedure Simple(); Begin If LookaHead == integer then Match (integer) Else If LookaHead == char then Match (char) Else If LookaHead == num then Begin Match (num); Match (‘..’); Match (num); End Else Error(); End;

Chapter 6: Top-Down Parser10 Example   ^id  array [ ] of integer  char  num.. num

Chapter 6: Top-Down Parser11 Which production should we use? integer  ^id   integer  char Example

Chapter 6: Top-Down Parser12 First (simple) = {integer, char, num} First (^id) = {^} First (array [ ] of type = {array}) First (type) = {^, array, integer, char, num} Example

Chapter 6: Top-Down Parser13 Definition: A  ab First (A) = a A   A   First of  &  must be disjoint

Chapter 6: Top-Down Parser14 Push Down Automata (Non-recursive predictive Parsing)

Chapter 6: Top-Down Parser15 Push Down Automata (Non-recursive predictive Parsing) PDA = {Q, , H, , q0, Z, F} Q = finite set of States  = set of input alphabet H = set of stack symbols  = transition function (q, a, z) q0 = starting state Z = starting stack F = final states

Chapter 6: Top-Down Parser16 Operation on  state operation  change()  stay() stack operation  push()  pop()  replace()  none() input operation  advance()  retain()

Chapter 6: Top-Down Parser17 INPUT ()-| q1 A Push (A) State (S) Advance Pop State (S) Advance Reject q0 $ Push (A) State (S) Advance RejectAccept STARTSTART  = { (, ), -| } H = {A, $} q0 = {S} Z = {$} Example:

Chapter 6: Top-Down Parser18 If input is (())-| Stack Input $ (())-| $A ())-| $AA ))-| $A )-| $ -|

Chapter 6: Top-Down Parser19 Derivation Tree S  (S) |

Chapter 6: Top-Down Parser20 LL(1) Grammar (push down machine) Scan from Left (L), Leftmost derivation (L), Look a head 1 Token

Chapter 6: Top-Down Parser21 Definition: A production is called NULL if the RHS of that production is Ø A   A production is call NULLABLE if the RHS can be reduced to Ø B  A A  

Chapter 6: Top-Down Parser22 Example of LL(1) Grammar: 1- S  AbB-|First(AbB-|) ={a,e,g, } 2- S  dFirst(d) = {d} 3- A  C AbFirst(C Ab) = {a,e} 4- A  BFirst(B) = {g, } 5- B  gSdFirst(gSd) = {g} 6- B   First (Ø) = {} 7- C  aFirst (a) = {a} 8- C  edFirst (ed) = {e}

Chapter 6: Top-Down Parser23 First (  ) = {a|  =>*a  } Set of terminal symbols that occur at the beginning of string derived from 

Chapter 6: Top-Down Parser24 Follow (  )= Set of input symbols that can immediately follow  Follow (A) = {b} Follow (B) = {-|, b, d} 1 4 S --->AbB --->BbB S --->AbB --->AbgSd --->AbgAbBd

Chapter 6: Top-Down Parser25 SELECT (A->  ) = {First (  ) If  = Ø; First (  ) U Follow (A)} SELECT (1) = {a,e,g} U Follow(A)={a, e, g,b} SELECT (2) = {d} SELECT (3) = {a,e} SELECT (4) = First(4) U Follow (A) = {g,b} SELECT (5) = {g} SELECT (6) = First(6) U Follow (B) = {-|,b,d} SELECT (7)={a} SELECT (8) = {e} SELECT (PREDICT):

Chapter 6: Top-Down Parser26 Is this grammar LL(1)? S SELECT (1)  SELECT (2) ={a,e,g,b}  {d}= Ø A SELECT (3)  SELECT (4) ={a,e}  {g,b}= Ø B SELECT (5)  SELECT (6) = {g}  {-|,b,d}= Ø C SELECT (7)  SELECT (8) = {a}  {e}= Ø Def: Grammar is LL(1), IIF production with the same LHS have disjoint prediction set.

Chapter 6: Top-Down Parser27 Creating the Table: Stack symbols=rows Input symbols=columns A  b  For row A, column b =REPLACE(  r ), ADVANCE A   For row A, column(selection set) =REPLACE (  r ), RETAIN A  b For row A, column b =POP, ADVANCE

Chapter 6: Top-Down Parser28 Row b, column b = POP, ADVANCE Row Δ, column -|=ACCEPT All other are ERROR Creating the Table:

Chapter 6: Top-Down Parser29 abgde-| S#1 #2#1Rej A#3#4 Rej#3Rej B #6#5#6Rej#6 C#7Rej #8Rej b Ad/PopRej d Ad/PRej Δ Accept Parsed Table

Chapter 6: Top-Down Parser30 1- Replace (Δ B b A), Retain 2- Pop, Advance 3- Replace (bAC), Retain 4- Replace (B), Retain 5- Replace (dS), Advance 6- Pop, Retain 7- Pop, Advance 8- Replace(d), Advance

Chapter 6: Top-Down Parser31 Input: b g d d -| StackInputProductionAction Sbgdd-|#1Replace (D BbA), Retain Δ BbA bgdd-|#4Replace(B), Retain Δ BbB bgdd-|#6Pop, Retain Δ Bb bgdd-|Pop, Advance Δ B gdd-|#5Replace (dS), Advance Δ dS dd-|#2Pop, Advance Δ d d-|Pop, Advance Δ -|Accept

Chapter 6: Top-Down Parser32 Parse Tree

Chapter 6: Top-Down Parser33 Recovery in a Top-Down Parser Advantage of TDP is efficient error recovery. Error: If Top of stack (token) is not the same as Look a Head (token) Recovery: Pop stack until Top: Synchronization Token (ST) ST: is a token that can end blocks of codes Read input symbol until current LookaHead symbol matches the symbol at the top of the stack or reaches the end of input If not end of input you are recovered

Chapter 6: Top-Down Parser34 LL(1) Grammars & Parser: facts: CFG Leftmost Parser Unambiguous O(n) Can be used for automatically generated parser

Chapter 6: Top-Down Parser35 Making Grammar: LL(1) 1-Remove Common Prefixed (Left Factor) S --> aBCdS --> aBD S --> aBD --> D --> Cd --> if then endif; --> if then else endif; --> if then --> endif; --> else endif;

Chapter 6: Top-Down Parser36 2-Remove Left Recursion:E E --> E + T E --> T T --> id A--> A  A-->B A-->A1 A2 A1-->B A2-->  A2 A2--> E-->E1E2 E1-->T E2-->+T E2 T-->id E2-->

Chapter 6: Top-Down Parser37 3-Remove Unreachable Products S-->aS -->a B-->S

Chapter 6: Top-Down Parser38 4-Corner Substitution S --> AB  B -->  1 B -->  2 B -->  3 S --> A  1  S --> A  2  S --> A  3  S --> a S --> AS A --> ab A --> cA S --> aB B --> B --> bS A --> ab A --> cA S --> cAS

Chapter 6: Top-Down Parser39 5-Singleton Substitution S-->  B  S-->  S'  S'--> B --> --> id: --> --> id:= ;

Chapter 6: Top-Down Parser40 Singleton Substitution - --> id: --> --> id:= ; - --> id: --> id:= ; --> id:= ; - --> id --> : --> := --> id :=

Chapter 6: Top-Down Parser41 Some Grammars Can Not be LL(1) *If _ Then _ Else If then If then else *S --> aBcS S --> aBcSeS B --> d S --> b

Chapter 6: Top-Down Parser42 Q-Grammar: A --> a  1 A --> b  1 A --> S-Grammar: A --> a  1 A --> b  2 A and S grammar are a LL(1) and we can make a push down Machine