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)

Slides:



Advertisements
Similar presentations
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
Advertisements

Lecture # 11 Grammar Problems.
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,
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
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 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice Grammars and Parsing.
Chapter 3 Chang Chi-Chung Parse tree intermediate representation The Role of the Parser Lexical Analyzer Parser Source Program Token Symbol.
– 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.
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
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.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
-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.
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.
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 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 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
Top-Down Parsing.
Syntax Analyzer (Parser)
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 ( )
Chapter 2 (part) + Chapter 4: Syntax Analysis S. M. Farhad 1.
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.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
Chapter 2: A Simple One Pass Compiler
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.
Programming Languages Translator
Context free grammars Terminals Nonterminals Start symbol productions
Lecture #12 Parsing Types.
Parsing.
Compiler Construction
Top-down parsing cannot be performed on left recursive grammars.
FIRST and FOLLOW Lecture 8 Mon, Feb 7, 2005.
Parsing with Context Free Grammars
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
Lecture 7 Predictive Parsing
Syntax Analysis source program lexical analyzer tokens syntax analyzer
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.
Computing Follow(A) : All Non-Terminals
Lecture 7 Predictive Parsing
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Predictive Parsing Program
Presentation transcript:

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

2Derivations  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)

3 Parse trees  -(id+id)  E => -E => -(E) => -(E+E) => -(id+E)=>-(id+id)

4Ambiguity  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

5Introduction  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 TE’ lm E TE’ FT’ lm E TE’ FT’ id lm E TE’ FT’ id Ɛ lm E TE’ FT’ id Ɛ +TE’

6 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 cd A S cd A ab S cd A ab Problem: backtrack cad S cd A a S cd A a

7 Parsing – Top-Down & Predictive  Top-Down Parsing  Parse tree / derivation of a token string occurs in a top down fashion.  For Example, Consider: 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  ??? Start symbol

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

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

10 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 ;

11 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

12 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

13 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). * * *

14 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  Y 1 Y 2 …Y k is a production rule Place First(Y 1 ) -  in First(X) if Y 1  , Place First(Y 2 ) -  in First(X) if Y 2  , Place First(Y 3 ) -  in First(X) … if Y k-1  , Place First(Y k ) in First(X) NOTE: As soon as Y i  , Stop. Repeat above steps until no more elements are added to any First( ) set. Checking “Y j   ?” essentially amounts to checking whether  belongs to First(Y j ) * * * * *

15 Computing First(X) : All Grammar Symbols - continued Informally, suppose we want to compute First(X 1 X 2 … X n ) = First (X 1 ) -  “+” First(X 2 ) if  is in First(X 1 ) -  “+” First(X 3 ) if  is in First(X 2 ) -  “+” … First(X n ) if  is in First(X n-1 ) Note 1: Only add  to First(X 1 X 2 … X n ) if  is in First(X i ) for all i Note 2: For First(X 1 ), if X 1  Z 1 Z 2 … Z m, then we need to compute First(Z 1 Z 2 … Z m ) !

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

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

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

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

20 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.

21 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 For any production X  X 1 X 2 … X m For j=1 to m, if is a non-terminal then: if X j is a non-terminal then: Follow(Follow( Follow(X j )=Follow(X j )  (First(X j+1,…,X m )-{  }); If If First(X j+1,…,X m ) contains  or X j+1,…,X m =  then Follow(Follow(Follow( then Follow(X j )=Follow(X j )  Follow(X);

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

23 Computing Follow : 1 st Example S  i E t SS’ | a S’  eS |  E  b First(S) = { i, a } First(S’) = { e,  } First(E) = { b } Recall: 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 } *

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

25 Example 2 Compute Follow for: 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