FIRST and FOLLOW Lecture 8 Mon, Feb 7, 2005.

Slides:



Advertisements
Similar presentations
LR Parsing – The Items Lecture 10 Mon, Feb 14, 2005.
Advertisements

Top-Down Parsing.
CS 310 – Fall 2006 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho,
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.
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
Top-Down 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.
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
Chapter 5 Top-Down Parsing.
# 1 CMPS 450 Parsing CMPS 450 J. Moloney. # 2 CMPS 450 Check that input is well-formed Build a parse tree or similar representation of input Recursive.
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
컴파일러 입문 제 7 장 LL 구문 분석.
Lesson 5 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Left Recursion Lecture 7 Fri, Feb 4, 2005.
11 Chapter 4 Grammars and Parsing Grammar Grammars, or more precisely, context-free grammars, are the formalism for describing the structure of.
Exercise 1 A ::= B EOF B ::=  | B B | (B) Tokens: EOF, (, ) Generate constraints and compute nullable and first for this grammar. Check whether first.
Compilers: syntax/4 1 Compiler Structures Objective – –describe general syntax analysis, grammars, parse trees, FIRST and FOLLOW sets ,
Pembangunan Kompilator.  The parse tree is created top to bottom.  Top-down parser  Recursive-Descent Parsing ▪ Backtracking is needed (If a choice.
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 Recursive Descent & LL(1) Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Lecture 3: Parsing CS 540 George Mason University.
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.
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 ( )
Bernd Fischer RW713: Compiler and Software Language Engineering.
Exercises on Chomsky Normal Form and CYK parsing
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
EXAMPLES: FIRST, FOLLOW, LL PARSING SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Parsing COMP 3002 School of Computer Science. 2 The Structure of a Compiler syntactic analyzer code generator program text interm. rep. machine code tokenizer.
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.
Context free grammars Terminals Nonterminals Start symbol productions
Compilers Welcome to a journey to CS419 Lecture15: Syntax Analysis:
Parsing.
Introduction to Top Down Parser
Top-down parsing cannot be performed on left recursive grammars.
Midterm Content – Excepted from PPTs
SYNTAX ANALYSIS (PARSING).
Parsing with Context Free Grammars
Top-Down Parsing.
3.2 Language and Grammar Left Factoring Unclear productions
CSC 4181 Compiler Construction Parsing
Lecture 7 Predictive Parsing
CS 540 George Mason University
LR Parsing – The Tables Lecture 11 Wed, Feb 16, 2005.
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.
Predictive Parsing Lecture 9 Wed, Feb 9, 2005.
Computing Follow(A) : All Non-Terminals
Lecture 7 Predictive Parsing
Chapter 3 Syntactic Analysis I.
Compiler Structures 4. Syntax Analysis Objectives
Nonrecursive Predictive Parsing
Context Free Grammar – Quick Review
 Unique leftmost derivation  Unique rightmost derivation num  Unique leftmost derivation  Unique rightmost derivation.
Presentation transcript:

FIRST and FOLLOW Lecture 8 Mon, Feb 7, 2005

Left Factoring A problem occurs when two productions for the same nonterminal begin with the same token. We cannot decide which production to use. This is not necessarily a problem since we could process the part they have in common, then make a decision based on what follows.

Left Factoring Consider the grammar A   | . We use left factorization to transform it into the form A  A' A'   | . Now we can apply the productions immediately and unambiguously.

Example: Left Factoring In the earlier example, we had the productions C  id == num | id != num | id < num To perform left factoring, introduce a nonterminal C': C  id C' C'  == num | != num | < num

Example: Left Factoring Consider the grammar of if statements. S  if C then S else S | if C then S We rewrite it as S  if C then S S' S'  else S | .

LL Parsing Methods LL parsing methods read the tokens from Left to right and parse them top-down according to a Leftmost derivation.

Table-Driven LL Parsing To build the parsing table, we need the notion of nullability and the two functions FIRST FOLLOW

Nullability A nonterminal A is nullable if A * . Clearly, A is nullable if it has a production A  . But A is also nullable if there are, for example, productions A  BC. B  A | aC | . C  aB | Cb | .

Nullability In other words, A is nullable if there is a production or there is a production A  B1B2…Bn, where B1, B2, ..., Bn are nullable.

Nullability In the grammar E  T E' E'  + T E' | . T  F T' F  (E) | id | num E' and T' are nullable. E, T, and F are not nullable.

Summary Nonterminal Nullable E No E' Yes T T' F

FIRST and FOLLOW Given a grammar G, we may define the functions FIRST and FOLLOW on the strings of symbols of G. FIRST() is the set of all terminals that may appear as the first symbol in a replacement string of . FOLLOW() is the set of all terminals that may follow  in a derivation.

FIRST For a grammar symbol X, FIRST(X) is defined as follows. For every terminal X, FIRST(X) = {X}. For every nonterminal X, if X  Y1Y2…Yn is a production, then FIRST(Y1)  FIRST(X). Furthermore, if Y1, Y2, …, Yk are nullable, then FIRST(Yk + 1)  FIRST(X).

FIRST We are concerned with FIRST(X) only for the nonterminals of the grammar. FIRST(X) for terminals is trivial. According to the definition, to determine FIRST(A), we must inspect all productions that have A on the left.

Example: FIRST Let the grammar be E  T E' E'  + T E' | . T  F T' F  (E) | id | num

Example: FIRST Find FIRST(E). E occurs on the left in only one production E  T E'. Therefore, FIRST(T)  FIRST(E). Furthermore, T is not nullable. Therefore, FIRST(E) = FIRST(T). We have yet to determine FIRST(T).

Example: FIRST Find FIRST(T). T occurs on the left in only one production T  F T'. Therefore, FIRST(F)  FIRST(T). Furthermore, F is not nullable. Therefore, FIRST(T) = FIRST(F). We have yet to determine FIRST(F).

Example: FIRST Find FIRST(F). Therefore, FIRST(F) = {(, id, num}. FIRST(E) = {(, id, num}. FIRST(T) = {(, id, num}.

Example: FIRST Find FIRST(E'). Find FIRST(T'). FIRST(E') = {+}.

Summary Nonterminal Nullable FIRST E No {(, id, num} E' Yes {+} T T' {*} F

FOLLOW For a grammar symbol X, FOLLOW(X) is defined as follows. If S is the start symbol, then $  FOLLOW(S). If A  B is a production, then FIRST()  FOLLOW(B). If A  B is a production, or A  B is a production and  is nullable, then FOLLOW(A)  FOLLOW(B).

FOLLOW We are concerned about FOLLOW(X) only for the nonterminals of the grammar. According to the definition, to determine FOLLOW(A), we must inspect all productions that have A on the right.

Example: FOLLOW Let the grammar be E  T E' E'  + T E' | . T  F T' F  (E) | id | num

Example: FOLLOW Find FOLLOW(E). E is the start symbol, therefore $  FOLLOW(E). E occurs on the right in only one production. F  (E). Therefore FOLLOW(E) = {$, )}.

Example: FOLLOW Find FOLLOW(E'). E' occurs on the right in two productions. E  T E' E'  + T E'. Therefore, FOLLOW(E') = FOLLOW(E) = {$, )}.

Example: FOLLOW Find FOLLOW(T). T occurs on the right in two productions. E  T E' E'  + T E'. Therefore, FOLLOW(T) contains FIRST(E') = {+}. However, E' is nullable, therefore it also contains FOLLOW(E) = {$, )} and FOLLOW(E') = {$, )}. Therefore, FOLLOW(T) = {+, $, )}.

Example: FOLLOW Find FOLLOW(T'). T' occurs on the right in two productions. T  F T' T'  * F T'. Therefore, FOLLOW(T') = FOLLOW(T) = {$, ), +}.

Example: FOLLOW Find FOLLOW(F). F occurs on the right in two productions. T  F T' T'  * F T'. Therefore, FOLLOW(F) contains FIRST(T') = {*}. However, T' is nullable, therefore it also contains FOLLOW(T) = {+, $, )} and FOLLOW(T') = {$, ), +}. Therefore, FOLLOW(F) = {*, $, ), +}.

Summary Nonterminal Nullable FIRST FOLLOW E No {(, id, num} {$, )} E' Yes {+} T {$, ), +} T' {*} F {*, $, ), +}

Exercise The grammar R  R  R | RR | R* | (R) | a | b generates all regular expressions on the alphabet {a, b}. Using the result of the exercise from the previous lecture, find FIRST(X) and FOLLOW(X) for each nonterminal X in the grammar.