Context free grammars Terminals Nonterminals Start symbol productions

Slides:



Advertisements
Similar presentations
Compiler Construction
Advertisements

1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
Top-Down Parsing.
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
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,
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.
Chapter 3 Chang Chi-Chung Parse tree intermediate representation The Role of the Parser Lexical Analyzer Parser Source Program Token Symbol.
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.
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
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.
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
11 Chapter 4 Grammars and Parsing Grammar Grammars, or more precisely, context-free grammars, are the formalism for describing the structure of.
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.
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.
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.
Parsing Top-Down.
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.
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.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
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.
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.
Parsing #1 Leonidas Fegaras.
Programming Languages Translator
The role of parser Lexical Analyzer Parser Rest of Front End Symbol
Lecture #12 Parsing Types.
Chapter 4 Syntax Analysis
Compilers Welcome to a journey to CS419 Lecture15: Syntax Analysis:
Parsing — Part II (Top-down parsing, left-recursion removal)
Parsing.
Compiler Construction
Introduction to Top Down Parser
Top-down parsing cannot be performed on left recursive grammars.
SYNTAX ANALYSIS (PARSING).
FIRST and FOLLOW Lecture 8 Mon, Feb 7, 2005.
Parsing with Context Free Grammars
Top-Down Parsing.
Chapter 4: Syntax Analysis Part 2: Top-Down Parsing
Parsing Techniques.
3.2 Language and Grammar Left Factoring Unclear productions
Top-Down Parsing CS 671 January 29, 2008.
Syntax-Directed Definition
CSC 4181 Compiler Construction Parsing
Lecture 7 Predictive Parsing
Syntax Analysis source program lexical analyzer tokens syntax analyzer
Lecture 8 Bottom Up 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.
LL PARSER The construction of a predictive parser is aided by two functions associated with a grammar called FIRST and FOLLOW. These functions allows us.
Computing Follow(A) : All Non-Terminals
Lecture 7 Predictive Parsing
Nonrecursive Predictive Parsing
Context Free Grammar – Quick Review
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Predictive Parsing Program
Presentation transcript:

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) F --> id

Derivations Productions are treated as rewriting rules to generate a string Rightmost and leftmost derivations E --> E + E | E * E | -E | (E) | id Derivations for –(id+id) E => -E => -(E) => -(E+E) => -(id+E)=>-(id+id)

Parse trees -(id+id) E => -E => -(E) => -(E+E) => -(id+E)=>-(id+id)

Ambiguity For some strings there exist more than one parse tree Or more than one leftmost derivation Or more than one rightmost derivation Example: id+id*id

Introduction A Top-down parser tries to create a parse tree from the root towards the leafs scanning input from left to right It can be also viewed as finding a leftmost derivation for an input string Example: id+id*id E -> TE’ E’ -> +TE’ | Ɛ T -> FT’ T’ -> *FT’ | Ɛ F -> (E) | id E lm E T E’ lm E T E’ F T’ lm E T E’ F T’ id lm E T E’ F T’ id Ɛ lm E T E’ F T’ id Ɛ +

Top-Down Parsing Choose production rule based on input symbol May require backtracking to correct a wrong choice. Example: S  c A d A  ab | a input: cad cad S c d A a b Problem: backtrack cad S c d A S cad S c d A a b cad S c d A a cad c d A a

Parsing – Top-Down & Predictive Top-Down Parsing  Parse tree / derivation of a token string occurs in a top down fashion. For Example, Consider: Start symbol type  simple |  id | array [ simple ] of type simple  integer | char | num dotdot num Suppose input is : array [ num dotdot num ] of integer Parsing would begin with type  ???

Top-Down Parse (type = start symbol) Lookahead symbol Input : array [ num dotdot num ] of integer type ? type ] simple of [ array type  simple |  id | array [ simple ] of type simple  integer | char | num dotdot num Start symbol Lookahead symbol Input : array [ num dotdot num ] of integer type ] simple of [ array num dotdot

Top-Down Parse (type = start symbol) Lookahead symbol Input : array [ num dotdot num ] of integer type ] simple of [ array num dotdot type  simple |  id | array [ simple ] of type simple  integer | char | num dotdot num Start symbol type ] simple of [ array num dotdot integer

Top-Down Parsing Recursive Descent Parser Operates by Attempting to Match Tokens in the Input Stream array [ num dotdot num ] of integer type  simple |  id | array [ simple ] of type simple  integer | char | num dotdot num procedure match ( t : token ) ; begin if lookahead = t then lookahead : = nexttoken else error end ;

Recursive Descent (continued) 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 (dotdot); match (num) end else error end ; type  simple |  id | array [ simple ] of type simple  integer | char | num dotdot num

Recursive Descent (continued) procedure type ; begin if lookahead is in { integer, char, num } then simple else if lookahead = ‘’ then begin match (‘’ ) ; match( id ) end else if lookahead = array then begin match( array ); match(‘[‘); simple; match(‘]’); match(of); type end else error end ; type  simple |  id | array [ simple ] of type simple  integer | char | num dotdot num

How to write tests for selecting the appropriate production rule ? Basic Tools: First: Let  be a string of grammar symbols. First() is the set that includes every terminal that appears leftmost in  or in any string originating from . NOTE: If   , then  is First( ). Follow: Let A be a non-terminal. Follow(A) is the set of terminals a that can appear directly to the right of A in some sentential form. (S  Aa, for some  and ). NOTE: If S  A, then $ is Follow(A). * * *

Computing First(X) : All Grammar Symbols 1. If X is a terminal, First(X) = {X} 2. If X  is a production rule, add  to First(X) 3. If X is a non-terminal, and X Y1Y2…Yk is a production rule Place First(Y1) -  in First(X) if Y1 , Place First(Y2) -  in First(X) if Y2  , Place First(Y3) -  in First(X) … if Yk-1  , Place First(Yk) in First(X) NOTE: As soon as Yi   , Stop. Repeat above steps until no more elements are added to any First( ) set. Checking “Yj   ?” essentially amounts to checking whether  belongs to First(Yj) * * * * *

Computing First(X) : All Grammar Symbols - continued Informally, suppose we want to compute First(X1 X2 … Xn ) = First (X1) -  “+” First(X2) if  is in First(X1) -  “+” First(X3) if  is in First(X2) -  “+” … First(Xn) if  is in First(Xn-1) Note 1: Only add  to First(X1 X2 … Xn) if  is in First(Xi) for all i Note 2: For First(X1), if X1 Z1 Z2 … Zm , then we need to compute First(Z1 Z2 … Zm) !

Example 1 Given the production rules: S  i E t SS’ | a S’  eS |  E  b

Example 1 Given the production rules: Verify that S  i E t SS’ | a S’  eS |  E  b Verify that First(S) = { i, a } First(S’) = { e,  } First(E) = { b }

Example 2 Computing First for: E  TE’ E’  + TE’ |  T  FT’ T’  * FT’ |  F  ( E ) | id

Example 2 Computing First for: E  TE’ E’  + TE’ |  T  FT’ T’  * FT’ |  F  ( E ) | id First(TE’) First(T) “+” First(E’) First(E) * Not First(E’) since T   First(T) First(F) “+” First(T’) First(F) * Not First(T’) since F   First((E)) “+” First(id) “(“ and “id” Overall: First(E) = { ( , id } = First(F) First(E’) = { + ,  } First(T’) = { * ,  } First(T)  First(F) = { ( , id }

Computing Follow(A) : All Non-Terminals 1. Place $ in Follow(A), where A is the start symbol and $ signals end of input 2. If there is a production B A, then everything in First() is in Follow(A) except for . 3. If B A is a production, or B A and    (First() contains  ), then everything in Follow(B) is in Follow(A) (Whatever followed B must follow A, since nothing follows A from the production rule) * We’ll calculate Follow for two grammars.

The Algorithm for Follow – pseudocode 1. Initialize Follow(X) for all non-terminals X to empty set. Place $ in Follow(S), where S is the start NT. 2. Repeat the following step until no modifications are made to any Follow-set For any production X  X1 X2 … Xm For j=1 to m, if Xj is a non-terminal then: Follow(Xj)=Follow(Xj)(First(Xj+1,…,Xm)-{}); If First(Xj+1,…,Xm) contains  or Xj+1,…,Xm=  then Follow(Xj)=Follow(Xj) Follow(X);

Computing Follow : 1st Example Recall: S  i E t SS’ | a S’  eS |  E  b First(S) = { i, a } First(S’) = { e,  } First(E) = { b }

Computing Follow : 1st Example Recall: S  i E t SS’ | a S’  eS |  E  b First(S) = { i, a } First(S’) = { e,  } First(E) = { b } Follow(S) – Contains $, since S is start symbol Since S  i E t SS’ , put in First(S’) – not  Since S’  , Put in Follow(S) Since S’  eS, put in Follow(S’) So…. Follow(S) = { e, $ } Follow(S’) = Follow(S) HOW? Follow(E) = { t } *

Example 2 Compute Follow for: E  TE’ E’  + TE’ |  T  FT’ T’  * FT’ |  F  ( E ) | id

Example 2 Compute Follow for: Follow First E $ ) E ( id E’ $ ) E’  + E  TE’ E’  + TE’ |  T  FT’ T’  * FT’ |  F  ( E ) | id Follow E $ ) E’ $ ) T + $ ) T’ + $ ) F + * $ ) First E ( id E’  + T ( id T’  * F ( id