First, Follow and Select sets

Slides:



Advertisements
Similar presentations
Grammar and Algorithm }
Advertisements

1 Example S'  S, S  CC, C  cC, C  d State 0. Closure({[S'  S, $]}) = { S'  S, $ S   CC, FIRST( $)  S   CC, $ C   cC, FIRST(C$)  C.
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,
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.
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.
Attribute Grammars Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 17.
Top-Down Parsing - recursive descent - predictive parsing
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Parsing Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 3.
컴파일러 입문 제 7 장 LL 구문 분석.
Exercise 1 A ::= B EOF B ::=  | B B | (B) Tokens: EOF, (, ) Generate constraints and compute nullable and first for this grammar. Check whether first.
Syntax Analysis CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Compilers: syntax/4 1 Compiler Structures Objective – –describe general syntax analysis, grammars, parse trees, FIRST and FOLLOW sets ,
Parsing Top-Down.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
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 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
Context-Free Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.
Top-Down Parsing.
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.
Exam 1 Review (With answers) EECS 483 – Lecture 15 University of Michigan Monday, October 30, 2006.
EXAMPLES: FIRST, FOLLOW, LL PARSING SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
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 Language Concepts
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Context-free grammars
Parsing #1 Leonidas Fegaras.
Context-free grammars, derivation trees, and ambiguity
LL(1) grammars Module 07.1 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Compiler Construction
Top-down parsing cannot be performed on left recursive grammars.
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
FIRST and FOLLOW Lecture 8 Mon, Feb 7, 2005.
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
CS 404 Introduction to Compiler Design
CS 363 – Chapter 2 The first 2 phases of compilation Scanning Parsing
Top-Down Parsing.
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Implementing a LR parser engine
Regular grammars Module 04.1 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
3.2 Language and Grammar Left Factoring Unclear productions
Top-down parsing Module 06.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Bottom-up derivation tree, original grammar
Bottom-up derivation tree, original grammar
TaBle-driven LL(1) Parsing
Lecture 8 Bottom Up Parsing
Ambiguity, Precedence, Associativity & Top-Down Parsing
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.
TaBle-driven LL(1) Parsing
Predictive Parsing Lecture 9 Wed, Feb 9, 2005.
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
CSCE 531 Compiler Construction
Chapter 3 Syntactic Analysis I.
Compiler Structures 4. Syntax Analysis Objectives
Operator precedence and AST’s
Fall Compiler Principles Lecture 4b: Scanning/Parsing recap
Nonrecursive Predictive Parsing
Tutorial-4 LL(1) Parsing.
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Presentation transcript:

First, Follow and Select sets COP4620 – Programming Language Translators Dr. Manuel E. Bermudez

Topics Graph Algorithm for First sets Graph algorithm for Follow sets Select sets Define LL(1) Grammar

17/01/2019 Top-Down Parsing Most parsing methods impose bounds on the amount of stack lookback and input lookahead. For programming languages, a common choice is (1,1). We must define OPF (A,t), where A is the top element of the stack, and t is the first symbol on the input. Storage requirements: O(n2), where n is the size of the grammar vocabulary (a few hundred).

Ominiscient parsing function 17/01/2019 Ominiscient parsing function ω t … A … OPF (A, t) = A → ω if ω =>* t, for some . ω =>* ε, and S =>* At, for some , , where  =>* ε. or

OPF OPF (A, b) = A → BAd because BAd =>* bAd 17/01/2019 Example S → A A → BAd B → b (Illustrating case 1): → C C →c OPF b c d B B → b B → b B → b C C → c C → c C → c S S → A S → A S → A A A → BAd A → C ??? OPF (A, b) = A → BAd because BAd =>* bAd OPF (A, c) = A → C because C =>* c i.e., B begins with b, and C begins with c. OPF Red entries are optional. So is the ??? entry.

opf → Example (illustrating case 2): S → A A → bAd OPF b d  17/01/2019 opf Example (illustrating case 2): S → A A → bAd → OPF b d  S S → A S → A A A → bAd A → A → OPF (S, b) = S → A , because A =>* bAd OPF (S, d) = -------- , because S =>* αSdβ OPF (S,  ) = S → A , because S is legal OPF (A, b) = A → bAd , because A =>* bAd OPF (A, d) = A → , because S =>* bAd OPF (A,  ) = A → , because S =>*A

First and follow sets Definition: 17/01/2019 First and follow sets Definition: First (A) = {t / A =>* t, for some } Follow (A) = {t / S =>* Atβ, for some , β} Computing First sets: Build graph (Ф, δ), where (A,B)  δ if B → A,  =>* ε (First(A)  First(B)) Attach to each node an empty set of terminals. Add t to the set for A if A → t,  =>* ε. Propagate the elements of the sets along the edges of the graph.

First sets Example: S → ABCD A → CDA C → A B → BC → a D → AC → b → {a} 17/01/2019 First sets Example: S → ABCD A → CDA C → A B → BC → a D → AC → b → Nullable = {A, C, D} {b} {a, b} S B {a} {a} Black: Steps 2 and 3. Red: Propagating in step 4 A C D {a}

Follow sets Build graph (Ф, δ), where (A,B)  δ if 17/01/2019 Follow sets Build graph (Ф, δ), where (A,B)  δ if A → B,  =>* ε. Follow(A)  Follow(B): any symbol X that follows A, also follows B. A X B  α ε

17/01/2019 Follow sets Attach to each node an empty set of terminals. Add  to the set for the start symbol. Add First(X) to the set for A (i.e. Follow(A)) if B → AX,  =>* ε. Propagate the elements of the sets along the edges of the graph.

Follow sets S B A C Example: S → ABCD A → CDA C → A B → BC → a D → AC 17/01/2019 Follow sets Example: S → ABCD A → CDA C → A B → BC → a D → AC → b → Nullable = {A, C, D} First(S) = {a, b} First(C) = {a} First(A) = {a} First(D) = {a} First(B) = {b} So, Follow(S)={ } Follow(A)= Follow(C)= Follow(D)={a,b, } Follow(B)={a, }  S B a  A C a b  a b  Now, propagate b. And, propagate . D a b 

OPF and LL(1) parsing Back to Parsing … 17/01/2019 OPF and LL(1) parsing Back to Parsing … We want OPF(A, t) = A → ω if either t  First(ω), i.e. ω =>* tβ ω =>* ε and t  Follow(A), i.e. S =>* A =>* Atβ ω t β A α t β A α ω ε

17/01/2019 LL(1) Parsing Definition: Select (A→ ω) = First(ω) U if ω =>* ε then Follow(A) else ø So PT(A, t) = A → ω if t  Select(A → ω) Time to call it PT (Parse Table) rather than OPF, because it isn’t omniscient.

LL(1) Parsing Grammar is not LL(1) 17/01/2019 LL(1) Parsing First and Follow sets: First (S) = {a, b} Follow (S) = { } First (A) = {a} Follow(A) = {a, b, } First (B) = {b} Follow(B) = {a, } First (C) = {a} Follow (C) = {a, b, } First (D) = {a} Follow(D) = {a, b, } Grammar Selects sets S → ABCD {a, b} B → BC {b} → b {b} A → CDA {a, b, } → a {a} → {a, b, } C → A {a, b, } D → AC {a, b, } not disjoint not pair-wise disjoint Grammar is not LL(1)

L(1) parsing Non LL(1) grammar: multiple entries in PT. 17/01/2019 S → ABCD {a, b} B → BC {b} → b {b} A → CDA {a, b, } → a {a} → {a, b, } C → A {a, b, } D → AC {a, b, } L(1) parsing Non LL(1) grammar: multiple entries in PT. PT a b ┴ S S → ABCD S → ABCD A A → CDA, A→ a, A → A → CDA, A → A → CDA,A → B B → BC, B → b C C → A C → A C → A D D → AC D → AC D → AC

LL(1) Grammars Definition: A CFG G is LL(1) 17/01/2019 LL(1) Grammars Definition: A CFG G is LL(1) ( Left-to-right, Left-most, (1)-symbol lookahead) iff for all A  Ф, and for all productions A→, A → with   , Select (A → ) ∩ Select (A → ) =  Previous example: grammar is not LL(1). More later on what do to about it.

summary Graph Algorithm for First sets Graph algorithm for Follow sets Select sets Define LL(1) Grammar