SlideShare a Scribd company logo
1 of 31
CSE340 - Principles of
Programming Languages
Lecture 15:
Parsing Techniques III
Javier Gonzalez-Sanchez
javiergs@asu.edu
BYENG M1-38
Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 2
Parser | Error Recovery
PROGRAM
Line N: expected {
Line N: expected }
currentToken++;
Searching for
FIRST(BODY) or }
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 3
Parser | Error Recovery
Rule FIRST set FOLLOW set
PROGRAM { EOF
BODY FIRST (PRINT) U FIRST (ASIGNMENT) U FIRST(VARIABLE) U FIRST
(WHILE) U FIRST(IF) U FIRST (RETURN)
}
PRINT print ;
ASSIGNMENT identifier ;
VARIABLE int, float, boolean, void, char, string ;
WHILE while } U FIRST(BODY)
IF if } U FIRST(BODY)
RETURN return ;
EXPRESSION FIRST(X) ), ;
X FIRST(Y) | U FOLLOW(EXPRESSION)
Y ! U FIRST(R) & U FOLLOW(X)
R FIRST(E) FOLLOW(Y)
E FIRST (A) !=, ==, >, < U FOLLOW(R)
A FIRST (B) -, + U FOLLOW(E)
B - U FIRST (C) *, /, U FOLLOW(A)
C integer, octal, hexadecimal, binary, true, false, string, char, float, identifier, ( FOLLOW(B)
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 4
Calculating the First Set
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 5
FIRST set
Definition
FIRST (a) is the set of tokens that can begin the construction a.
Example
<E> → <A> {+ <A>}
<A> → <B> {* <B>}
<B> → -<C> | <C>
<C> → integer
FIRST(E) = {-, integer}
FIRST(A) = {-, integer}
FIRST(B) = {-, integer}
FIRST(C) = {integer}
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 6
FIRST set
Define FIRST (BODY)
FIRST(BODY) =
FIRST (PRINT) U FIRST (ASSIGNMENT) U FIRST(VARIABLE) U FIRST(WHILE) U
FIRST(IF) U FIRST(RETURN)
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 7
FIRST set
Define FIRST (C)
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 8
FIRST set
Define FIRST (A)
Define FIRST (B)
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 9
FIRST set
<S> → <A><B><C>
<S> → <F>
<A> → <E><F>d
<A> → a
<B> → a<B>b
<B> → ε
<C> → c<C>
<C> → d
<E> → e<E>
<E> → <F>
<F> → <F>f
<F> → ε
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 10
Calculate the FIRST set
1.  FIRST(X) = {X}
if X is a terminal
2. FIRST(ε) = {ε}.
note that this is not covered by the first rule because
ε is not a terminal.
3.  If A → Xα, add FIRST(X) − {ε} to FIRST(A)
4.  If A → A1A2A3 ...AiAi+1 ... Ak and
ε ∈ F IRST (A1) and ε ∈ FIRST (A2) and . . . and ε ∈ FIRST (Ai),
then add FIRST (Ai+1) − {ε} to FIRST (A).
5.  If A → A1A2A3 ...Ak and
ε ∈ FIRST(A1) and ε ∈ FIRST(A2) and... and ε ∈ FIRST(Ak),
then add ε to FIRST(A).
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 11
Calculate the FIRST set
loop
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 12
FIRST set
S → ABC
S → F
A → EFd
A → a
B → aBb
B → ε
C → cC
C → d
E → eE
E → F
F → Ff
F → ε
rule	
 FIRST  set  -­‐‑  evolution	
S	
 ø {a, ε} {a, ε, e, f} {a, ε, e, f, d}
A	
 ø {a} {a, e} {a, e, f, d}
B	
 ø {a, ε}
C	
 ø {c, d}
E	
 ø {e} {e, ε} {e, ε, f}
F	
 ø {ε} {ε, f}
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 13
FIRST set | Exercise
<X> → <A> | <A> a
<A> → <B> | <B> b
<B> → <C><D><E> | c d e | <C> c <D> d <E> e
<C> → one
<D> → two
<E> → three
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 14
FIRST set | Exercise
<X> → <A>
<X> → <A> a
<A> → <B>
<A> → <B> b
<B> → <C><D><E>
<B> → c d e
<B> → <C> c <D> d <E> e
<C> → one
<D> → two
<E> → three
OPTION 1:
FIRST(X) = {c, one}
FIRST(A) = {c, one}
FIRST(B) = {c, one}
FIRST(C) = {one}
FIRST(D) = {two}
FIRST(E) = {three}
OPTION 2:
FIRST(X) = {c, one, ε}
FIRST(A) = {b, c, one}
FIRST(B) = {c, one}
FIRST(C) = {one}
FIRST(D) = {two}
FIRST(E) = {three}
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 15
FIRST set | Exercise
<X> → <A>
<X> → <A> a
<A> → <B>
<A> → <B> b
<B> → <C><D><E>
<B> → c d e
<B> → <C> c <D> d <E> e
<C> → one
<C> → ε
<D> → two
<D> → ε
<E> → three
FIRST(X) = {c, one, three, two}
FIRST(A) = {c, one, three, two}
FIRST(B) = {c, one, three, two}
FIRST(C) = {one, ε}
FIRST(D) = {two, ε}
FIRST(E) = {three}
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 16
Calculating the Follow Set
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 17
FOLLOW set
Definition
FOLLOW (a) is the set of tokens that can follow the construction a.
Example
<E> → <A> {+ <A>}
<A> → <B> {* <B>}
<B> → -<C> | <C>
<C> → integer
5 + 4 + -7 * 12 + 75
5 + 4 + ((-7) * 12) + 75
What follows <C> ?
What follows <A> ?
What follows <E> ?
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 18
FOLLOW set
Definition
FOLLOW (a) is the set of tokens that can follow the construction a.
Example
<E> → <A> {+ <A>}
<A> → <B> {* <B>}
<B> → <C> | <C>
<C> → integer
FOLLOW(E) = {$} // $ represents end of input, i.e., EOF
FOLLOW(A) = {+, $}
FOLLOW(B) = {*, +, $}
FOLLOW(C) = {*, +, $}
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 19
FOLLOW set
Define FOLLOW (BODY)
FIRST(BODY) = }
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 20
FOLLOW set
Define FOLLOW (C)
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 21
FOLLOW set
<S> → <A><B><C>
<S> → <F>
<A> → <E><F>d
<A> → a
<B> → a<B>b
<B> → ε
<C> → c<C>
<C> → d
<E> → e<E>
<E> → <F>
<F> → <F>f
<F> → ε
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 22
Calculate the FOLLOW set
1.  First put $ (the end of input marker) in Follow(S) (S is the start
symbol)
2.  If there is a production A → aBb,
(where a can be a whole string)
then everything in FIRST(b) except for ε is placed in FOLLOW(B).
(apply the rule 4 in calculate FIRST set)
3.  If there is a production A → aB,
then add FOLLOW(A) to FOLLOW(B)
4.  If there is a production A → aBb,
where FIRST(b) contains ε,
then add FOLLOW(A) to FOLLOW(B)
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 23
Calculate the FOLLOW set
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 24
FOLLOW set
S → ABC
S → F
A → EFd
A → a
B → aBb
B → ε
C → cC
C → d
E → eE
E → F
F → Ff
F → ε
rule	
 FOLLOW  set  -­‐‑  evolution	
S	
 {eof}
A	
 {a} {a, c, d}
B	
 {c, d} {c, d, b}
C	
 {eof}
E	
 {f} {f, d}
F	
 {eof} {eof, d} {eof, d, f}
FIRST sets:
S={a,ε,e,f,d}
A={a, e, f, d}
B={a, ε}
C= {c, d}
E={e, ε, f}
F={ε,f}
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 25
Another Example
<E> → <T> {+<T>}
<T> → <F> {*<F>}
<F> → (<E>) | integer
FIRST (E) = {(, integer}
FIRST (T) = {(, integer}
FIRST (F) = {(, integer}
FOLLOW(E) = {$, )}
FOLLOW(T) = {$, ),+ }
FOLLOW(F) = {$, ),+, * }
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 26
Prediction Rules
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 27
Prediction Rules
Rule 1.
It should always be possible to choose among several
alternatives in a grammar rule.
FIRST(R1) FIRST(R2) FIRST(R3)... FIRST(Rn) = Ø
BODY
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 28
Prediction Rules
Rule 1.1
The FIRST sets of any two choices in one rule must not
have tokens in common in order to implement a single-
symbol look ahead predictive parser.
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 29
Prediction Rules
Rule 2.
For any optional part, no token beginning the optional part
can also come after the optional part.
FIRST(RULE) != FOLLOW(RULE)
BODY PROGRAM
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 30
Homework
Programming Assignment #2
CSE340 - Principles of Programming Languages
Javier Gonzalez-Sanchez
javiergs@asu.edu
Summer 2015
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.

More Related Content

What's hot

Let us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solutionLet us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solutionHazrat Bilal
 
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1rohit kumar
 
My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfixSenthil Kumar
 
Let us c chapter 4 solution
Let us c chapter 4 solutionLet us c chapter 4 solution
Let us c chapter 4 solutionrohit kumar
 
SMU BCA SEM 1 FALL 2016 ASSIGNMENTS
SMU BCA SEM 1 FALL 2016 ASSIGNMENTSSMU BCA SEM 1 FALL 2016 ASSIGNMENTS
SMU BCA SEM 1 FALL 2016 ASSIGNMENTSsolved_assignments
 
Conversion from infix to prefix using stack
Conversion from infix to prefix using stackConversion from infix to prefix using stack
Conversion from infix to prefix using stackHaqnawaz Ch
 
Let us c(by yashwant kanetkar) chapter 2 solution
Let us c(by yashwant kanetkar) chapter 2 solutionLet us c(by yashwant kanetkar) chapter 2 solution
Let us c(by yashwant kanetkar) chapter 2 solutionrohit kumar
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsAfaq Mansoor Khan
 
Infix to-postfix examples
Infix to-postfix examplesInfix to-postfix examples
Infix to-postfix examplesmua99
 
7. Pointer Arithmetic
7. Pointer Arithmetic7. Pointer Arithmetic
7. Pointer ArithmeticGem WeBlog
 
Mini project title prime number generator
Mini project title prime number generatorMini project title prime number generator
Mini project title prime number generatorMd. Asad Chowdhury Dipu
 
Testing lecture after lec 4
Testing lecture after lec 4Testing lecture after lec 4
Testing lecture after lec 4emailharmeet
 

What's hot (20)

2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
 
201506 CSE340 Lecture 22
201506 CSE340 Lecture 22201506 CSE340 Lecture 22
201506 CSE340 Lecture 22
 
Area of trapezoids
Area of trapezoidsArea of trapezoids
Area of trapezoids
 
Let us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solutionLet us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solution
 
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
 
My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfix
 
Let us c chapter 4 solution
Let us c chapter 4 solutionLet us c chapter 4 solution
Let us c chapter 4 solution
 
SMU BCA SEM 1 FALL 2016 ASSIGNMENTS
SMU BCA SEM 1 FALL 2016 ASSIGNMENTSSMU BCA SEM 1 FALL 2016 ASSIGNMENTS
SMU BCA SEM 1 FALL 2016 ASSIGNMENTS
 
Ankita sharma focp
Ankita sharma focpAnkita sharma focp
Ankita sharma focp
 
Conversion from infix to prefix using stack
Conversion from infix to prefix using stackConversion from infix to prefix using stack
Conversion from infix to prefix using stack
 
Let us c(by yashwant kanetkar) chapter 2 solution
Let us c(by yashwant kanetkar) chapter 2 solutionLet us c(by yashwant kanetkar) chapter 2 solution
Let us c(by yashwant kanetkar) chapter 2 solution
 
C applications
C applicationsC applications
C applications
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
Application of Stacks
Application of StacksApplication of Stacks
Application of Stacks
 
Infix to-postfix examples
Infix to-postfix examplesInfix to-postfix examples
Infix to-postfix examples
 
7. Pointer Arithmetic
7. Pointer Arithmetic7. Pointer Arithmetic
7. Pointer Arithmetic
 
Mini project title prime number generator
Mini project title prime number generatorMini project title prime number generator
Mini project title prime number generator
 
Testing lecture after lec 4
Testing lecture after lec 4Testing lecture after lec 4
Testing lecture after lec 4
 
Lab 1
Lab 1Lab 1
Lab 1
 
Topological Sort
Topological SortTopological Sort
Topological Sort
 

Viewers also liked

Viewers also liked (20)

Cup A Soup MiNi
Cup A Soup MiNiCup A Soup MiNi
Cup A Soup MiNi
 
악플과 악플의 재생산
악플과 악플의 재생산악플과 악플의 재생산
악플과 악플의 재생산
 
lectura
lecturalectura
lectura
 
Eeuwigblijvenleren
EeuwigblijvenlerenEeuwigblijvenleren
Eeuwigblijvenleren
 
Syndrome metabolique et maladies vasculaires
Syndrome metabolique et maladies vasculairesSyndrome metabolique et maladies vasculaires
Syndrome metabolique et maladies vasculaires
 
Diego Ernesto Leal
Diego Ernesto LealDiego Ernesto Leal
Diego Ernesto Leal
 
Jay Cross Vivo Versao Final Corrigida
Jay Cross Vivo Versao Final CorrigidaJay Cross Vivo Versao Final Corrigida
Jay Cross Vivo Versao Final Corrigida
 
Irem presentation final
Irem presentation   finalIrem presentation   final
Irem presentation final
 
201505 CSE340 Lecture 05
201505 CSE340 Lecture 05201505 CSE340 Lecture 05
201505 CSE340 Lecture 05
 
201505 CSE340 Lecture 06
201505 CSE340 Lecture 06201505 CSE340 Lecture 06
201505 CSE340 Lecture 06
 
Postcards From Pure Michigan
Postcards From Pure MichiganPostcards From Pure Michigan
Postcards From Pure Michigan
 
Fun at the Sculpture Gardens with Ally and Lauren
Fun at the Sculpture Gardens with Ally and LaurenFun at the Sculpture Gardens with Ally and Lauren
Fun at the Sculpture Gardens with Ally and Lauren
 
Heirloom Travel: Wine Country - Sonoma
Heirloom Travel: Wine Country - SonomaHeirloom Travel: Wine Country - Sonoma
Heirloom Travel: Wine Country - Sonoma
 
Sexto AñO Basico
Sexto AñO BasicoSexto AñO Basico
Sexto AñO Basico
 
Twitter en Facebook voor journalisten
Twitter en Facebook voor journalistenTwitter en Facebook voor journalisten
Twitter en Facebook voor journalisten
 
Thomasville
ThomasvilleThomasville
Thomasville
 
Elalamy DiabèTe Et Aap Sfa 2009
Elalamy DiabèTe Et Aap Sfa 2009Elalamy DiabèTe Et Aap Sfa 2009
Elalamy DiabèTe Et Aap Sfa 2009
 
201506 CSE340 Lecture 11
201506 CSE340 Lecture 11201506 CSE340 Lecture 11
201506 CSE340 Lecture 11
 
Demonstration Presentation
Demonstration PresentationDemonstration Presentation
Demonstration Presentation
 
Huizenprijzen in amsterdam
Huizenprijzen in amsterdamHuizenprijzen in amsterdam
Huizenprijzen in amsterdam
 

Similar to 201506 CSE340 Lecture 15

Similar to 201506 CSE340 Lecture 15 (20)

SETS [Algebra]
SETS [Algebra]SETS [Algebra]
SETS [Algebra]
 
presentation about set theorem
presentation about set theorempresentation about set theorem
presentation about set theorem
 
201506 CSE340 Lecture 14
201506 CSE340 Lecture 14201506 CSE340 Lecture 14
201506 CSE340 Lecture 14
 
201506 CSE340 Lecture 12
201506 CSE340 Lecture 12201506 CSE340 Lecture 12
201506 CSE340 Lecture 12
 
Answerkeyoe Exer1
Answerkeyoe  Exer1Answerkeyoe  Exer1
Answerkeyoe Exer1
 
201506 CSE340 Lecture 09
201506 CSE340 Lecture 09201506 CSE340 Lecture 09
201506 CSE340 Lecture 09
 
Sets
SetsSets
Sets
 
set theory --.pptx
set theory --.pptxset theory --.pptx
set theory --.pptx
 
important Questions class 11 chapter 1 sets
important Questions class 11 chapter 1 setsimportant Questions class 11 chapter 1 sets
important Questions class 11 chapter 1 sets
 
Cerutti -- TAFA2013
Cerutti -- TAFA2013Cerutti -- TAFA2013
Cerutti -- TAFA2013
 
ALE2014 let tests drive or let dijkstra derive
ALE2014 let tests drive or let dijkstra deriveALE2014 let tests drive or let dijkstra derive
ALE2014 let tests drive or let dijkstra derive
 
SET THEORY
SET THEORYSET THEORY
SET THEORY
 
1g. Pedagogy of Mathematics (Part II) - Set language introduction and Ex.1.7
1g. Pedagogy of Mathematics (Part II) - Set language introduction and Ex.1.71g. Pedagogy of Mathematics (Part II) - Set language introduction and Ex.1.7
1g. Pedagogy of Mathematics (Part II) - Set language introduction and Ex.1.7
 
01_Sets.pdf
01_Sets.pdf01_Sets.pdf
01_Sets.pdf
 
07012023.pptx
07012023.pptx07012023.pptx
07012023.pptx
 
determine pi & epi of the boolean function (v2)
determine pi & epi of the boolean function (v2)determine pi & epi of the boolean function (v2)
determine pi & epi of the boolean function (v2)
 
Pdm presentation
Pdm presentationPdm presentation
Pdm presentation
 
Barisan dan deret .ingg
Barisan dan deret .inggBarisan dan deret .ingg
Barisan dan deret .ingg
 
Barisan dan deret .ingg
Barisan dan deret .inggBarisan dan deret .ingg
Barisan dan deret .ingg
 
Add math may june 2016 p1
Add math may june 2016 p1Add math may june 2016 p1
Add math may june 2016 p1
 

More from Javier Gonzalez-Sanchez (20)

201804 SER332 Lecture 01
201804 SER332 Lecture 01201804 SER332 Lecture 01
201804 SER332 Lecture 01
 
201801 SER332 Lecture 03
201801 SER332 Lecture 03201801 SER332 Lecture 03
201801 SER332 Lecture 03
 
201801 SER332 Lecture 04
201801 SER332 Lecture 04201801 SER332 Lecture 04
201801 SER332 Lecture 04
 
201801 SER332 Lecture 02
201801 SER332 Lecture 02201801 SER332 Lecture 02
201801 SER332 Lecture 02
 
201801 CSE240 Lecture 26
201801 CSE240 Lecture 26201801 CSE240 Lecture 26
201801 CSE240 Lecture 26
 
201801 CSE240 Lecture 25
201801 CSE240 Lecture 25201801 CSE240 Lecture 25
201801 CSE240 Lecture 25
 
201801 CSE240 Lecture 24
201801 CSE240 Lecture 24201801 CSE240 Lecture 24
201801 CSE240 Lecture 24
 
201801 CSE240 Lecture 23
201801 CSE240 Lecture 23201801 CSE240 Lecture 23
201801 CSE240 Lecture 23
 
201801 CSE240 Lecture 22
201801 CSE240 Lecture 22201801 CSE240 Lecture 22
201801 CSE240 Lecture 22
 
201801 CSE240 Lecture 21
201801 CSE240 Lecture 21201801 CSE240 Lecture 21
201801 CSE240 Lecture 21
 
201801 CSE240 Lecture 20
201801 CSE240 Lecture 20201801 CSE240 Lecture 20
201801 CSE240 Lecture 20
 
201801 CSE240 Lecture 19
201801 CSE240 Lecture 19201801 CSE240 Lecture 19
201801 CSE240 Lecture 19
 
201801 CSE240 Lecture 18
201801 CSE240 Lecture 18201801 CSE240 Lecture 18
201801 CSE240 Lecture 18
 
201801 CSE240 Lecture 17
201801 CSE240 Lecture 17201801 CSE240 Lecture 17
201801 CSE240 Lecture 17
 
201801 CSE240 Lecture 16
201801 CSE240 Lecture 16201801 CSE240 Lecture 16
201801 CSE240 Lecture 16
 
201801 CSE240 Lecture 15
201801 CSE240 Lecture 15201801 CSE240 Lecture 15
201801 CSE240 Lecture 15
 
201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
 
201801 CSE240 Lecture 13
201801 CSE240 Lecture 13201801 CSE240 Lecture 13
201801 CSE240 Lecture 13
 
201801 CSE240 Lecture 12
201801 CSE240 Lecture 12201801 CSE240 Lecture 12
201801 CSE240 Lecture 12
 
201801 CSE240 Lecture 11
201801 CSE240 Lecture 11201801 CSE240 Lecture 11
201801 CSE240 Lecture 11
 

Recently uploaded

8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 

Recently uploaded (20)

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 

201506 CSE340 Lecture 15

  • 1. CSE340 - Principles of Programming Languages Lecture 15: Parsing Techniques III Javier Gonzalez-Sanchez javiergs@asu.edu BYENG M1-38 Office Hours: By appointment
  • 2. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 2 Parser | Error Recovery PROGRAM Line N: expected { Line N: expected } currentToken++; Searching for FIRST(BODY) or }
  • 3. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 3 Parser | Error Recovery Rule FIRST set FOLLOW set PROGRAM { EOF BODY FIRST (PRINT) U FIRST (ASIGNMENT) U FIRST(VARIABLE) U FIRST (WHILE) U FIRST(IF) U FIRST (RETURN) } PRINT print ; ASSIGNMENT identifier ; VARIABLE int, float, boolean, void, char, string ; WHILE while } U FIRST(BODY) IF if } U FIRST(BODY) RETURN return ; EXPRESSION FIRST(X) ), ; X FIRST(Y) | U FOLLOW(EXPRESSION) Y ! U FIRST(R) & U FOLLOW(X) R FIRST(E) FOLLOW(Y) E FIRST (A) !=, ==, >, < U FOLLOW(R) A FIRST (B) -, + U FOLLOW(E) B - U FIRST (C) *, /, U FOLLOW(A) C integer, octal, hexadecimal, binary, true, false, string, char, float, identifier, ( FOLLOW(B)
  • 4. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 4 Calculating the First Set
  • 5. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 5 FIRST set Definition FIRST (a) is the set of tokens that can begin the construction a. Example <E> → <A> {+ <A>} <A> → <B> {* <B>} <B> → -<C> | <C> <C> → integer FIRST(E) = {-, integer} FIRST(A) = {-, integer} FIRST(B) = {-, integer} FIRST(C) = {integer}
  • 6. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 6 FIRST set Define FIRST (BODY) FIRST(BODY) = FIRST (PRINT) U FIRST (ASSIGNMENT) U FIRST(VARIABLE) U FIRST(WHILE) U FIRST(IF) U FIRST(RETURN)
  • 7. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 7 FIRST set Define FIRST (C)
  • 8. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 8 FIRST set Define FIRST (A) Define FIRST (B)
  • 9. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 9 FIRST set <S> → <A><B><C> <S> → <F> <A> → <E><F>d <A> → a <B> → a<B>b <B> → ε <C> → c<C> <C> → d <E> → e<E> <E> → <F> <F> → <F>f <F> → ε
  • 10. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 10 Calculate the FIRST set 1.  FIRST(X) = {X} if X is a terminal 2. FIRST(ε) = {ε}. note that this is not covered by the first rule because ε is not a terminal. 3.  If A → Xα, add FIRST(X) − {ε} to FIRST(A) 4.  If A → A1A2A3 ...AiAi+1 ... Ak and ε ∈ F IRST (A1) and ε ∈ FIRST (A2) and . . . and ε ∈ FIRST (Ai), then add FIRST (Ai+1) − {ε} to FIRST (A). 5.  If A → A1A2A3 ...Ak and ε ∈ FIRST(A1) and ε ∈ FIRST(A2) and... and ε ∈ FIRST(Ak), then add ε to FIRST(A).
  • 11. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 11 Calculate the FIRST set loop
  • 12. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 12 FIRST set S → ABC S → F A → EFd A → a B → aBb B → ε C → cC C → d E → eE E → F F → Ff F → ε rule FIRST  set  -­‐‑  evolution S ø {a, ε} {a, ε, e, f} {a, ε, e, f, d} A ø {a} {a, e} {a, e, f, d} B ø {a, ε} C ø {c, d} E ø {e} {e, ε} {e, ε, f} F ø {ε} {ε, f}
  • 13. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 13 FIRST set | Exercise <X> → <A> | <A> a <A> → <B> | <B> b <B> → <C><D><E> | c d e | <C> c <D> d <E> e <C> → one <D> → two <E> → three
  • 14. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 14 FIRST set | Exercise <X> → <A> <X> → <A> a <A> → <B> <A> → <B> b <B> → <C><D><E> <B> → c d e <B> → <C> c <D> d <E> e <C> → one <D> → two <E> → three OPTION 1: FIRST(X) = {c, one} FIRST(A) = {c, one} FIRST(B) = {c, one} FIRST(C) = {one} FIRST(D) = {two} FIRST(E) = {three} OPTION 2: FIRST(X) = {c, one, ε} FIRST(A) = {b, c, one} FIRST(B) = {c, one} FIRST(C) = {one} FIRST(D) = {two} FIRST(E) = {three}
  • 15. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 15 FIRST set | Exercise <X> → <A> <X> → <A> a <A> → <B> <A> → <B> b <B> → <C><D><E> <B> → c d e <B> → <C> c <D> d <E> e <C> → one <C> → ε <D> → two <D> → ε <E> → three FIRST(X) = {c, one, three, two} FIRST(A) = {c, one, three, two} FIRST(B) = {c, one, three, two} FIRST(C) = {one, ε} FIRST(D) = {two, ε} FIRST(E) = {three}
  • 16. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 16 Calculating the Follow Set
  • 17. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 17 FOLLOW set Definition FOLLOW (a) is the set of tokens that can follow the construction a. Example <E> → <A> {+ <A>} <A> → <B> {* <B>} <B> → -<C> | <C> <C> → integer 5 + 4 + -7 * 12 + 75 5 + 4 + ((-7) * 12) + 75 What follows <C> ? What follows <A> ? What follows <E> ?
  • 18. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 18 FOLLOW set Definition FOLLOW (a) is the set of tokens that can follow the construction a. Example <E> → <A> {+ <A>} <A> → <B> {* <B>} <B> → <C> | <C> <C> → integer FOLLOW(E) = {$} // $ represents end of input, i.e., EOF FOLLOW(A) = {+, $} FOLLOW(B) = {*, +, $} FOLLOW(C) = {*, +, $}
  • 19. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 19 FOLLOW set Define FOLLOW (BODY) FIRST(BODY) = }
  • 20. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 20 FOLLOW set Define FOLLOW (C)
  • 21. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 21 FOLLOW set <S> → <A><B><C> <S> → <F> <A> → <E><F>d <A> → a <B> → a<B>b <B> → ε <C> → c<C> <C> → d <E> → e<E> <E> → <F> <F> → <F>f <F> → ε
  • 22. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 22 Calculate the FOLLOW set 1.  First put $ (the end of input marker) in Follow(S) (S is the start symbol) 2.  If there is a production A → aBb, (where a can be a whole string) then everything in FIRST(b) except for ε is placed in FOLLOW(B). (apply the rule 4 in calculate FIRST set) 3.  If there is a production A → aB, then add FOLLOW(A) to FOLLOW(B) 4.  If there is a production A → aBb, where FIRST(b) contains ε, then add FOLLOW(A) to FOLLOW(B)
  • 23. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 23 Calculate the FOLLOW set
  • 24. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 24 FOLLOW set S → ABC S → F A → EFd A → a B → aBb B → ε C → cC C → d E → eE E → F F → Ff F → ε rule FOLLOW  set  -­‐‑  evolution S {eof} A {a} {a, c, d} B {c, d} {c, d, b} C {eof} E {f} {f, d} F {eof} {eof, d} {eof, d, f} FIRST sets: S={a,ε,e,f,d} A={a, e, f, d} B={a, ε} C= {c, d} E={e, ε, f} F={ε,f}
  • 25. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 25 Another Example <E> → <T> {+<T>} <T> → <F> {*<F>} <F> → (<E>) | integer FIRST (E) = {(, integer} FIRST (T) = {(, integer} FIRST (F) = {(, integer} FOLLOW(E) = {$, )} FOLLOW(T) = {$, ),+ } FOLLOW(F) = {$, ),+, * }
  • 26. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 26 Prediction Rules
  • 27. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 27 Prediction Rules Rule 1. It should always be possible to choose among several alternatives in a grammar rule. FIRST(R1) FIRST(R2) FIRST(R3)... FIRST(Rn) = Ø BODY
  • 28. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 28 Prediction Rules Rule 1.1 The FIRST sets of any two choices in one rule must not have tokens in common in order to implement a single- symbol look ahead predictive parser.
  • 29. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 29 Prediction Rules Rule 2. For any optional part, no token beginning the optional part can also come after the optional part. FIRST(RULE) != FOLLOW(RULE) BODY PROGRAM
  • 30. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 30 Homework Programming Assignment #2
  • 31. CSE340 - Principles of Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu Summer 2015 Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.