SlideShare a Scribd company logo
1 of 58
SYNTAX
ANALYSIS
OR
PARSING
Lecture 05
AMBIGUOUS GRAMMAR
2
AMBIGUOUS GRAMMAR
 More than one Parse Tree for some sentence.
 The grammar for a programming language may be
ambiguous
 Need to modify it for parsing.
 Also: Grammar may be left recursive.
 Need to modify it for parsing.
3
ELIMINATION OF AMBIGUITY
 Ambiguous
 A Grammar is ambiguous if there are multiple
parse trees for the same sentence.
 Disambiguation
 Express Preference for one parse tree over others
 Add disambiguating rule into the grammar
4
RESOLVING PROBLEMS: AMBIGUOUS
GRAMMARS
Consider the following grammar segment:
stmt → if expr then stmt
| if expr then stmt else stmt
| other (any other statement)
If E1 then S1 else if E2 then S2 else S3
simple parse tree:
stmt
stmt
stmtexpr
exprE1
E2
S3
S1
S2
then
then
else
else
if
if
stmt stmt
5
EXAMPLE : WHAT HAPPENS
WITH THIS STRING?
If E1 then if E2 then S1 else S2
How is this parsed ?
if E1 then
if E2 then
S1
else
S2
if E1 then
if E2 then
S1
else
S2
vs.
6
PARSE TREES: IF E1 THEN IF E2
THEN S1 ELSE S2
Form 1:
stmt
stmt
stmtexpr
E1 S2
then elseif
expr
E2
S1
thenif
stmt
stmt
expr
E1
thenif
stmt
expr
E2
S2S1
then else
if
stmt stmt
Form 2:
7
REMOVING AMBIGUITY
Take Original Grammar:
stmt → if expr then stmt
| if expr then stmt else stmt
| other (any other statement)
Revise to remove ambiguity:
stmt → matched_stmt | unmatched_stmt
matched_stmt → if expr then matched_stmt else matched_stmt |
other
unmatched_stmt → if expr then stmt
| if expr then matched_stmt else unmatched_stmt
Rule: Match each else with the closest previous
unmatched then.
8
RESOLVING DIFFICULTIES : LEFT
RECURSION
A left recursive grammar has rules that support the
derivation : A ⇒ Aα, for some α.+
Top-Down parsing can’t reconcile this type of grammar,
since it could consistently make choice which wouldn’t
allow termination.
A ⇒ Aα ⇒ Aαα ⇒ Aααα … etc. A→ Aα |
β
Take left recursive grammar:
A → Aα | β
To the following:
A → βA’
A’ → αA’ | ∈
9
WHY IS LEFT RECURSION A
PROBLEM ?
Consider:
E → E + T | T
T → T * F | F
F → ( E ) | id
Derive : id + id + id
E ⇒ E + T ⇒
How can left recursion be removed ?
E → E + T | T What does this generate?
E ⇒ E + T ⇒ T + T
E ⇒ E + T ⇒ E + T + T ⇒ T + T + T
…
How does this build strings ?
What does each string have to start with ?
10
RESOLVING DIFFICULTIES : LEFT
RECURSION (2)
Informal Discussion:
Take all productions for A and order as:
A → Aα1 | Aα2 | … | Aαm | β1 | β2 | … | βn
Where no βi begins with A.
Now apply concepts of previous slide:
A → β1A’ | β2A’ | … | βnA’
A’ → α1A’ | α2A’| … | αm A’ | ∈
For our example:
E → E + T | T
T → T * F | F
F → ( E ) | id
E → TE’
E’ → + TE’ |
∈
T → FT’
T’ → * FT’ |
∈
F → ( E ) | id
11
RESOLVING DIFFICULTIES : LEFT
RECURSION (3)
Problem: If left recursion is two-or-more levels deep,
this isn’t enough
S → Aa | b
A → Ac | Sd | ∈
S ⇒ Aa ⇒ Sda
Algorithm:
Input: Grammar G with ordered Non-Terminals A1, ..., An
Output: An equivalent grammar with no left recursion
1. Arrange the non-terminals in some order A1=start NT,A2,…An
2. for i := 1 to n do begin
for j := 1 to i – 1 do begin
replace each production of the form Ai → Ajγ
by the productions Ai → δ1γ | δ2γ | … | δkγ
where Aj → δ1|δ2|…|δk are all current Aj productions;
end
eliminate the immediate left recursion among Ai productions
12
USING THE
ALGORITHM
Apply the algorithm to: A1 → A2a | b| ∈
A2 → A2c | A1d
i = 1
For A1 there is no left recursion
i = 2
for j=1 to 1 do
Take productions: A2 → A1γ and replace with
A2 → δ1 γ | δ2 γ | … | δk γ|
where A1→ δ1 | δ2 | … | δk are A1
productions
in our case A2 → A1d becomes A2 → A2ad | bd | dWhat’s left: A1→ A2a | b | ∈
A2 → A2 c | A2 ad | bd | d
Are we done ?
13
USING THE
ALGORITHM (2)
No ! We must still remove A2 left recursion !
A1→ A2a | b | ∈
A2 → A2 c | A2 ad | bd | d
Recall:
A → Aα1 | Aα2 | … | Aαm | β1 | β2 | … | βn
A → β1A’ | β2A’ | … | βnA’
A’ → α1A’ | α2A’| … | αm A’ | ∈
Apply to above case. What do you get ?
A1→ A2a | b | ∈
A2 → bdA2’ | dA2’
A2’ → c A2’ | adA2’ | ∈
14
REMOVING DIFFICULTIES : ∈-
MOVES
Transformation: In order to remove A→ ∈ find all
rules of the form B→ uAv and add the rule B→ uv to
the grammar G.
Why does this work ?
E → TE’
E’ → + TE’ |
∈T → FT’
T’ → * FT’ |
∈
F → ( E ) | id
Examples:
A1 → A2 a | b
A2 → bd A2’ | A2’
A2’ → c A2’ | bd A2’ | ∈
A is Grammar ∈-free if:
1. It has no ∈-production or
2. There is exactly one ∈-production
S → ∈ and then the start symbol S
does not appear on the right side of
any production.
15
REMOVING DIFFICULTIES :
CYCLES
How would cycles be removed ?
Make sure every production is adding some terminal(s) (except
a single ∈ -production in the start NT)…
e.g.
S → SS | ( S ) | ∈
Has a cycle: S ⇒ SS ⇒ S
S → ∈
Transform to:
S → S ( S ) | ( S ) | ∈
16
REMOVING DIFFICULTIES : LEFT
FACTORING
Problem : Uncertain which of 2 rules to choose:
stmt → if expr then stmt else stmt
| if expr then stmt
When do you know which one is valid ?
What’s the general form of stmt ?
A → αβ1 | αβ2 α : if expr then stmt
β1: else stmt β2 : ∈
Transform to:
A → α A’
A’ → β1 | β2
EXAMPLE:
stmt → if expr then stmt rest
rest → else stmt | ∈
17
TOP DOWN PARSING
 Find a left-most derivation
 Find (build) a parse tree
 Start building from the root and work down…
 As we search for a derivation
 Must make choices:
 Which rule to use
 Where to use it
 May run into problems!!
18
TOP-DOWN PARSING
 Recursive-Descent Parsing
 Backtracking is needed (If a choice of a production rule
does not work, we backtrack to try other alternatives.)
 It is a general parsing technique, but not widely used.
 Not efficient
 Predictive Parsing
 no backtracking
 efficient
 needs a special form of grammars (LL(1) grammars).
 Recursive Predictive Parsing is a special form of
Recursive Descent parsing without backtracking.
 Non-Recursive (Table Driven) Predictive Parser is also
known as LL(1) parser.
19
RECURSIVE DESCENT PARSING
(BACKTRACKING)
20
RECURSIVE DESCENT PARSING
(BACKTRACKING)
21
RECURSIVE DESCENT PARSING
(BACKTRACKING)
22
RECURSIVE DESCENT PARSING
(BACKTRACKING)
23
RECURSIVE DESCENT PARSING
(BACKTRACKING)
24
RECURSIVE DESCENT PARSING
(BACKTRACKING)
25
RECURSIVE DESCENT PARSING
(BACKTRACKING)
26
RECURSIVE DESCENT PARSING
(BACKTRACKING)
27
RECURSIVE DESCENT PARSING
(BACKTRACKING)
28
RECURSIVE DESCENT PARSING
(BACKTRACKING)
29
RECURSIVE DESCENT PARSING
(BACKTRACKING)
30
RECURSIVE DESCENT PARSING
(BACKTRACKING)
31
RECURSIVE DESCENT PARSING
(BACKTRACKING)
32
RECURSIVE DESCENT PARSING
(BACKTRACKING)
33
RECURSIVE DESCENT PARSING
(BACKTRACKING)
34
RECURSIVE DESCENT PARSING
(BACKTRACKING)
35
RECURSIVE DESCENT PARSING
(BACKTRACKING)
Successfully parsed!!
36
RECURSIVE-DESCENT PARSING
ALGORITHM
 A recursive-descent parsing program consists of a set of
procedures – one for each non-terminal
 Execution begins with the procedure for the start symbol
 Announces success if the procedure body scans the entire input
void A(){
for (j=1 to t){ /* assume there is t number of A-productions */
Choose a A-production, AjX1X2…Xk;
for (i=1 to k){
if (Xi is a non-terminal)
call procedure Xi();
else if (Xi equals the current input symbol a)
advance the input to the next symbol;
else backtrack in input and reset the pointer
}
}
}
37
PREDICTIVE PARSER
When re-writing a non-terminal in a derivation
step, a predictive parser can uniquely choose a
production rule by just looking the current symbol
in the input string.
A → α1 | ... | αn input: ... a .......
current token
38
PREDICTIVE PARSER (EXAMPLE)
stmt → if ...... |
while ...... |
begin ...... |
for .....
 When we are trying to write the non-terminal stmt, if the
current token is if we have to choose first production rule.
 When we are trying to write the non-terminal stmt, we can
uniquely choose the production rule by just looking the
current token.
 We eliminate the left recursion in the grammar, and left
factor it. But it may not be suitable for predictive parsing
(not LL(1) grammar).
39
RECURSIVE PREDICTIVE
PARSING
 Each non-terminal corresponds to a procedure.
Ex: A → aBb (This is only the production rule
for A)
proc A {
- match the current token with a, and move to the
next token;
- call ‘B’;
- match the current token with b, and move to the
next token;
}
40
RECURSIVE PREDICTIVE PARSING
(CONT.)
A → aBb | bAB
proc A {
case of the current token {
‘a’: - match the current token with a, and move to the next
token;
- call ‘B’;
- match the current token with b, and move to the next
token;
‘b’: - match the current token with b, and move to the next
token;
- call ‘A’;
- call ‘B’;
}
} 41
RECURSIVE PREDICTIVE
PARSING (CONT.)
 When to apply ε-productions.
A → aA | bB | ε
 If all other productions fail, we should apply an ε-
production. For example, if the current token is
not a or b, we may apply the ε-production.
 Most correct choice: We should apply an ε-
production for a non-terminal A when the current
token is in the follow set of A (which terminals
can follow A in the sentential forms). 42
RECURSIVE PREDICTIVE PARSING
(EXAMPLE)
A → aBe | cBd | C
B → bB | ε
C → f
proc C { match the current token with f,
proc A { and move to the next token; }
case of the current token {
a: - match the current token with a,
and move to the next token; proc B {
- call B; case of the current token {
- match the current token with e, b: - match the current token with b,
and move to the next token; and move to the next token;
c: - match the current token with c, - call B
and move to the next token; e,d: do nothing
- call B; }
- match the current token with d, }
and move to the next token;
f: - call C
}
}
follow set of B
first set of C
43
FIRST FUNCTION
44
• FIRST (E) =
• FIRST (E’) =
• FIRST (T) =
• FIRST (T’) =
• FIRST (F) =
COMPUTING THE FIRST
FUNCTION
45
TO COMPUTE THE
FIRST(X1X2X3...XN)
46
FIRST - EXAMPLE
 P  i | c | n T S
 Q  P | a S | b S c S T
 R  b | ε
 S  c | R n | ε
 T  R S q
 FIRST(P) =
 FIRST(Q) =
 FIRST(R) =
 FIRST(S) =
 FIRST(T) =
47
FIRST - EXAMPLE
 S  a S e | S T S
 T  R S e | Q
 R  r S r | ε
 Q  S T | ε  FIRST(S) =
 FIRST(R) =
 FIRST(T) =
 FIRST(Q) =
48
FOLLOW SETS
 FOLLOW(A) is the set of terminals (including
end marker of input - $) that may follow non-
terminal A in some sentential form.
 FOLLOW(A) = {c | S ⇒+
…Ac…} ∪ {$} if S ⇒+
…
A
 For example, consider L ⇒+
(())(L)L
Both ‘)’ and end of file can follow L
 NOTE: ε is never in FOLLOW sets
49
COMPUTING FOLLOW(A)
1. If A is start symbol, put $ in FOLLOW(A)
2. Productions of the form B  α A β,
Add FIRST(β) – {ε} to FOLLOW(A)
3. Productions of the form B  α A or
B  α A β where β ⇒*
ε
Add FOLLOW(B) to FOLLOW(A)
50
EXAMPLE
 E  T E′
 E′  + T E′ | ε
 T  F T′
 T′  * F T′ | ε
 F  ( E ) | id
 FIRST(E) = {(, id}
 FIRST(E′) = {+, ε}
 FIRST(T) = {(, id}
 FIRST(T′) = {*, ε}
 FIRST(F) = {(, id}}
 FOLLOW(E) = {$}
 FOLLOW(E′) =
 FOLLOW(T) =
 FOLLOW(T′) =
 FOLLOW(F) =
Assume the first non-terminal is the start symbol
Using rule #1
1. If A is start symbol, put $ in FOLLOW(A)
51
EXAMPLE
 E  T E′
 E′  + T E′ | ε
 T  F T′
 T′  * F T′ | ε
 F  ( E ) | id
 FIRST(E) = {(, id}
 FIRST(E′) = {+, ε}
 FIRST(T) = {(, id}
 FIRST(T′) = {*, ε}
 FIRST(F) = {(, id}}
 FOLLOW(E) = {$, )}
 FOLLOW(E′) =
 FOLLOW(T) = {+}
 FOLLOW(T′) =
 FOLLOW(F) = {*}
Using rule #2
2. Productions of the form B  α A β,
Add FIRST(β) – {ε} to FOLLOW(A)
52
EXAMPLE
 E  T E′
 E′  + T E′ | ε
 T  F T′
 T′  * F T′ | ε
 F  ( E ) | id
 FIRST(E) = {(, id}
 FIRST(E′) = {+, ε}
 FIRST(T) = {(, id}
 FIRST(T′) = {*, ε}
 FIRST(F) = {(, id}}
 FOLLOW(E) = {$, )}
 FOLLOW(E′) = FOLLOW(E)
= {$, )}
 FOLLOW(T) = {+} ∪ FOLLOW(E′)
= {+, $, )}
 FOLLOW(T′) = FOLLOW(T)
= {+, $, )}
 FOLLOW(F) = {*} ∪ FOLLOW(T′)
= {*, +, $, )}
Using rule #3
3. Productions of the form B  α A or
B  α A β where β ⇒*
ε
Add FOLLOW(B) to FOLLOW(A)
53
EXAMPLE
 S  ( A) | ε
 A  T E
 E  & T E | ε
 T  ( A ) | a | b | c
 FIRST(T) =
 FIRST(E) =
 FIRST(A) =
 FIRST(S) =
 FOLLOW(S) =
 FOLLOW(A) =
 FOLLOW(E) =
 FOLLOW(T) = 54
EXAMPLE
 S  ( A) | ε
 A  T E
 E  & T E | ε
 T  ( A ) | a | b | c
 FIRST(T) = {(,a,b,c}
 FIRST(E) = {&, ε }
 FIRST(A) = {(,a,b,c}
 FIRST(S) = {(, ε}
 FOLLOW(S) = {$}
 FOLLOW(A) = { ) }
 FOLLOW(E) = FOLLOW(A) = { ) }
 FOLLOW(T) = FIRST(E) ∪ FOLLOW(E) = {&, )}
55
EXAMPLE
 S  a S e | B
 B  b B C f | C
 C  c C g | d | ε
 FIRST(C) =
 FIRST(B) =
 FIRST(S) =
 FOLLOW(C) =
 FOLLOW(B) =
 FOLLOW(S) = {$}
Assume the first non-terminal is the start symbol
1. If A is start symbol, put $ in FOLLOW(A)
56
2. Productions of the form B  α A β,
Add FIRST(β) – {ε} to FOLLOW(A)
3. Productions of the form B  α A or
B  α A β where β ⇒*
ε
Add FOLLOW(B) to FOLLOW(A)
EXAMPLE
 S  a S e | B
 B  b B C f | C
 C  c C g | d | ε
 FIRST(C) = {c,d,ε}
 FIRST(B) = {b,c,d,ε}
 FIRST(S) = {a,b,c,d,ε}
 FOLLOW(C) =
 FOLLOW(B) =
 FOLLOW(S) = { }$, e
{c,d} ∪ FOLLOW(S)
= {c,d,e,$}
{f,g} ∪ FOLLOW(B)
= {c,d,e,f,g,$}
57
QUESTIONS ?
58

More Related Content

What's hot

Context free grammars
Context free grammarsContext free grammars
Context free grammars
Ronak Thakkar
 
13. Query Processing in DBMS
13. Query Processing in DBMS13. Query Processing in DBMS
13. Query Processing in DBMS
koolkampus
 

What's hot (20)

Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
Relationship Among Token, Lexeme & Pattern
Relationship Among Token, Lexeme & PatternRelationship Among Token, Lexeme & Pattern
Relationship Among Token, Lexeme & Pattern
 
Token, Pattern and Lexeme
Token, Pattern and LexemeToken, Pattern and Lexeme
Token, Pattern and Lexeme
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
 
Specification-of-tokens
Specification-of-tokensSpecification-of-tokens
Specification-of-tokens
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Recursive Descent Parsing
Recursive Descent Parsing  Recursive Descent Parsing
Recursive Descent Parsing
 
Syntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address CodeSyntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address Code
 
The structure of agents
The structure of agentsThe structure of agents
The structure of agents
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsing
 
Design and analysis of algorithms
Design and analysis of algorithmsDesign and analysis of algorithms
Design and analysis of algorithms
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lex
 
13. Query Processing in DBMS
13. Query Processing in DBMS13. Query Processing in DBMS
13. Query Processing in DBMS
 
Lecture 16 17 code-generation
Lecture 16 17 code-generationLecture 16 17 code-generation
Lecture 16 17 code-generation
 
First and follow set
First and follow setFirst and follow set
First and follow set
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 

Similar to Lecture 05 syntax analysis 2

lec02-Syntax Analysis and LL(1).pdf
lec02-Syntax Analysis and LL(1).pdflec02-Syntax Analysis and LL(1).pdf
lec02-Syntax Analysis and LL(1).pdf
wigewej294
 
Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.ppt
FamiDan
 
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
venkatapranaykumarGa
 

Similar to Lecture 05 syntax analysis 2 (20)

PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Natural Language Processing - Writing Grammar
Natural Language Processing - Writing GrammarNatural Language Processing - Writing Grammar
Natural Language Processing - Writing Grammar
 
Lexical 2
Lexical 2Lexical 2
Lexical 2
 
lec02-Syntax Analysis and LL(1).pdf
lec02-Syntax Analysis and LL(1).pdflec02-Syntax Analysis and LL(1).pdf
lec02-Syntax Analysis and LL(1).pdf
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
Ch06
Ch06Ch06
Ch06
 
Parsing
ParsingParsing
Parsing
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic Notation
 
Functional Concepts for OOP Developers
Functional Concepts for OOP DevelopersFunctional Concepts for OOP Developers
Functional Concepts for OOP Developers
 
Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.ppt
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
 
3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx
 
constructing_generic_algorithms__ben_deane__cppcon_2020.pdf
constructing_generic_algorithms__ben_deane__cppcon_2020.pdfconstructing_generic_algorithms__ben_deane__cppcon_2020.pdf
constructing_generic_algorithms__ben_deane__cppcon_2020.pdf
 
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
 
a7f0bc7785844884c4de624f61ce5978_MIT18_404f20_lec4.pptx
a7f0bc7785844884c4de624f61ce5978_MIT18_404f20_lec4.pptxa7f0bc7785844884c4de624f61ce5978_MIT18_404f20_lec4.pptx
a7f0bc7785844884c4de624f61ce5978_MIT18_404f20_lec4.pptx
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Lecture7 syntax analysis_3
Lecture7 syntax analysis_3Lecture7 syntax analysis_3
Lecture7 syntax analysis_3
 

More from Iffat Anjum

Qo s based mac protocol for medical wireless body area sensor networks
Qo s based mac protocol for medical wireless body area sensor networksQo s based mac protocol for medical wireless body area sensor networks
Qo s based mac protocol for medical wireless body area sensor networks
Iffat Anjum
 

More from Iffat Anjum (20)

Fog computing ( foggy cloud)
Fog computing  ( foggy cloud)Fog computing  ( foggy cloud)
Fog computing ( foggy cloud)
 
Cognitive radio network_MS_defense_presentation
Cognitive radio network_MS_defense_presentationCognitive radio network_MS_defense_presentation
Cognitive radio network_MS_defense_presentation
 
Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environment
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 
Lecture 13 intermediate code generation 2.pptx
Lecture 13 intermediate code generation 2.pptxLecture 13 intermediate code generation 2.pptx
Lecture 13 intermediate code generation 2.pptx
 
Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2
 
Lecture 09 syntax analysis 05
Lecture 09 syntax analysis 05Lecture 09 syntax analysis 05
Lecture 09 syntax analysis 05
 
Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01
 
Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4
 
Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3
 
Lecture 03 lexical analysis
Lecture 03 lexical analysisLecture 03 lexical analysis
Lecture 03 lexical analysis
 
Lecture 04 syntax analysis
Lecture 04 syntax analysisLecture 04 syntax analysis
Lecture 04 syntax analysis
 
Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysis
 
Compiler Design - Introduction to Compiler
Compiler Design - Introduction to CompilerCompiler Design - Introduction to Compiler
Compiler Design - Introduction to Compiler
 
Distributed contention based mac protocol for cognitive radio
Distributed contention based mac protocol for cognitive radioDistributed contention based mac protocol for cognitive radio
Distributed contention based mac protocol for cognitive radio
 
On qo s provisioning in context aware wireless sensor networks for healthcare
On qo s provisioning in context aware wireless sensor networks for healthcareOn qo s provisioning in context aware wireless sensor networks for healthcare
On qo s provisioning in context aware wireless sensor networks for healthcare
 
Data link control
Data link controlData link control
Data link control
 
Pnp mac preemptive slot allocation and non preemptive transmission for provid...
Pnp mac preemptive slot allocation and non preemptive transmission for provid...Pnp mac preemptive slot allocation and non preemptive transmission for provid...
Pnp mac preemptive slot allocation and non preemptive transmission for provid...
 
Qo s based mac protocol for medical wireless body area sensor networks
Qo s based mac protocol for medical wireless body area sensor networksQo s based mac protocol for medical wireless body area sensor networks
Qo s based mac protocol for medical wireless body area sensor networks
 

Recently uploaded

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 

Recently uploaded (20)

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 

Lecture 05 syntax analysis 2

  • 3. AMBIGUOUS GRAMMAR  More than one Parse Tree for some sentence.  The grammar for a programming language may be ambiguous  Need to modify it for parsing.  Also: Grammar may be left recursive.  Need to modify it for parsing. 3
  • 4. ELIMINATION OF AMBIGUITY  Ambiguous  A Grammar is ambiguous if there are multiple parse trees for the same sentence.  Disambiguation  Express Preference for one parse tree over others  Add disambiguating rule into the grammar 4
  • 5. RESOLVING PROBLEMS: AMBIGUOUS GRAMMARS Consider the following grammar segment: stmt → if expr then stmt | if expr then stmt else stmt | other (any other statement) If E1 then S1 else if E2 then S2 else S3 simple parse tree: stmt stmt stmtexpr exprE1 E2 S3 S1 S2 then then else else if if stmt stmt 5
  • 6. EXAMPLE : WHAT HAPPENS WITH THIS STRING? If E1 then if E2 then S1 else S2 How is this parsed ? if E1 then if E2 then S1 else S2 if E1 then if E2 then S1 else S2 vs. 6
  • 7. PARSE TREES: IF E1 THEN IF E2 THEN S1 ELSE S2 Form 1: stmt stmt stmtexpr E1 S2 then elseif expr E2 S1 thenif stmt stmt expr E1 thenif stmt expr E2 S2S1 then else if stmt stmt Form 2: 7
  • 8. REMOVING AMBIGUITY Take Original Grammar: stmt → if expr then stmt | if expr then stmt else stmt | other (any other statement) Revise to remove ambiguity: stmt → matched_stmt | unmatched_stmt matched_stmt → if expr then matched_stmt else matched_stmt | other unmatched_stmt → if expr then stmt | if expr then matched_stmt else unmatched_stmt Rule: Match each else with the closest previous unmatched then. 8
  • 9. RESOLVING DIFFICULTIES : LEFT RECURSION A left recursive grammar has rules that support the derivation : A ⇒ Aα, for some α.+ Top-Down parsing can’t reconcile this type of grammar, since it could consistently make choice which wouldn’t allow termination. A ⇒ Aα ⇒ Aαα ⇒ Aααα … etc. A→ Aα | β Take left recursive grammar: A → Aα | β To the following: A → βA’ A’ → αA’ | ∈ 9
  • 10. WHY IS LEFT RECURSION A PROBLEM ? Consider: E → E + T | T T → T * F | F F → ( E ) | id Derive : id + id + id E ⇒ E + T ⇒ How can left recursion be removed ? E → E + T | T What does this generate? E ⇒ E + T ⇒ T + T E ⇒ E + T ⇒ E + T + T ⇒ T + T + T … How does this build strings ? What does each string have to start with ? 10
  • 11. RESOLVING DIFFICULTIES : LEFT RECURSION (2) Informal Discussion: Take all productions for A and order as: A → Aα1 | Aα2 | … | Aαm | β1 | β2 | … | βn Where no βi begins with A. Now apply concepts of previous slide: A → β1A’ | β2A’ | … | βnA’ A’ → α1A’ | α2A’| … | αm A’ | ∈ For our example: E → E + T | T T → T * F | F F → ( E ) | id E → TE’ E’ → + TE’ | ∈ T → FT’ T’ → * FT’ | ∈ F → ( E ) | id 11
  • 12. RESOLVING DIFFICULTIES : LEFT RECURSION (3) Problem: If left recursion is two-or-more levels deep, this isn’t enough S → Aa | b A → Ac | Sd | ∈ S ⇒ Aa ⇒ Sda Algorithm: Input: Grammar G with ordered Non-Terminals A1, ..., An Output: An equivalent grammar with no left recursion 1. Arrange the non-terminals in some order A1=start NT,A2,…An 2. for i := 1 to n do begin for j := 1 to i – 1 do begin replace each production of the form Ai → Ajγ by the productions Ai → δ1γ | δ2γ | … | δkγ where Aj → δ1|δ2|…|δk are all current Aj productions; end eliminate the immediate left recursion among Ai productions 12
  • 13. USING THE ALGORITHM Apply the algorithm to: A1 → A2a | b| ∈ A2 → A2c | A1d i = 1 For A1 there is no left recursion i = 2 for j=1 to 1 do Take productions: A2 → A1γ and replace with A2 → δ1 γ | δ2 γ | … | δk γ| where A1→ δ1 | δ2 | … | δk are A1 productions in our case A2 → A1d becomes A2 → A2ad | bd | dWhat’s left: A1→ A2a | b | ∈ A2 → A2 c | A2 ad | bd | d Are we done ? 13
  • 14. USING THE ALGORITHM (2) No ! We must still remove A2 left recursion ! A1→ A2a | b | ∈ A2 → A2 c | A2 ad | bd | d Recall: A → Aα1 | Aα2 | … | Aαm | β1 | β2 | … | βn A → β1A’ | β2A’ | … | βnA’ A’ → α1A’ | α2A’| … | αm A’ | ∈ Apply to above case. What do you get ? A1→ A2a | b | ∈ A2 → bdA2’ | dA2’ A2’ → c A2’ | adA2’ | ∈ 14
  • 15. REMOVING DIFFICULTIES : ∈- MOVES Transformation: In order to remove A→ ∈ find all rules of the form B→ uAv and add the rule B→ uv to the grammar G. Why does this work ? E → TE’ E’ → + TE’ | ∈T → FT’ T’ → * FT’ | ∈ F → ( E ) | id Examples: A1 → A2 a | b A2 → bd A2’ | A2’ A2’ → c A2’ | bd A2’ | ∈ A is Grammar ∈-free if: 1. It has no ∈-production or 2. There is exactly one ∈-production S → ∈ and then the start symbol S does not appear on the right side of any production. 15
  • 16. REMOVING DIFFICULTIES : CYCLES How would cycles be removed ? Make sure every production is adding some terminal(s) (except a single ∈ -production in the start NT)… e.g. S → SS | ( S ) | ∈ Has a cycle: S ⇒ SS ⇒ S S → ∈ Transform to: S → S ( S ) | ( S ) | ∈ 16
  • 17. REMOVING DIFFICULTIES : LEFT FACTORING Problem : Uncertain which of 2 rules to choose: stmt → if expr then stmt else stmt | if expr then stmt When do you know which one is valid ? What’s the general form of stmt ? A → αβ1 | αβ2 α : if expr then stmt β1: else stmt β2 : ∈ Transform to: A → α A’ A’ → β1 | β2 EXAMPLE: stmt → if expr then stmt rest rest → else stmt | ∈ 17
  • 18. TOP DOWN PARSING  Find a left-most derivation  Find (build) a parse tree  Start building from the root and work down…  As we search for a derivation  Must make choices:  Which rule to use  Where to use it  May run into problems!! 18
  • 19. TOP-DOWN PARSING  Recursive-Descent Parsing  Backtracking is needed (If a choice of a production rule does not work, we backtrack to try other alternatives.)  It is a general parsing technique, but not widely used.  Not efficient  Predictive Parsing  no backtracking  efficient  needs a special form of grammars (LL(1) grammars).  Recursive Predictive Parsing is a special form of Recursive Descent parsing without backtracking.  Non-Recursive (Table Driven) Predictive Parser is also known as LL(1) parser. 19
  • 37. RECURSIVE-DESCENT PARSING ALGORITHM  A recursive-descent parsing program consists of a set of procedures – one for each non-terminal  Execution begins with the procedure for the start symbol  Announces success if the procedure body scans the entire input void A(){ for (j=1 to t){ /* assume there is t number of A-productions */ Choose a A-production, AjX1X2…Xk; for (i=1 to k){ if (Xi is a non-terminal) call procedure Xi(); else if (Xi equals the current input symbol a) advance the input to the next symbol; else backtrack in input and reset the pointer } } } 37
  • 38. PREDICTIVE PARSER When re-writing a non-terminal in a derivation step, a predictive parser can uniquely choose a production rule by just looking the current symbol in the input string. A → α1 | ... | αn input: ... a ....... current token 38
  • 39. PREDICTIVE PARSER (EXAMPLE) stmt → if ...... | while ...... | begin ...... | for .....  When we are trying to write the non-terminal stmt, if the current token is if we have to choose first production rule.  When we are trying to write the non-terminal stmt, we can uniquely choose the production rule by just looking the current token.  We eliminate the left recursion in the grammar, and left factor it. But it may not be suitable for predictive parsing (not LL(1) grammar). 39
  • 40. RECURSIVE PREDICTIVE PARSING  Each non-terminal corresponds to a procedure. Ex: A → aBb (This is only the production rule for A) proc A { - match the current token with a, and move to the next token; - call ‘B’; - match the current token with b, and move to the next token; } 40
  • 41. RECURSIVE PREDICTIVE PARSING (CONT.) A → aBb | bAB proc A { case of the current token { ‘a’: - match the current token with a, and move to the next token; - call ‘B’; - match the current token with b, and move to the next token; ‘b’: - match the current token with b, and move to the next token; - call ‘A’; - call ‘B’; } } 41
  • 42. RECURSIVE PREDICTIVE PARSING (CONT.)  When to apply ε-productions. A → aA | bB | ε  If all other productions fail, we should apply an ε- production. For example, if the current token is not a or b, we may apply the ε-production.  Most correct choice: We should apply an ε- production for a non-terminal A when the current token is in the follow set of A (which terminals can follow A in the sentential forms). 42
  • 43. RECURSIVE PREDICTIVE PARSING (EXAMPLE) A → aBe | cBd | C B → bB | ε C → f proc C { match the current token with f, proc A { and move to the next token; } case of the current token { a: - match the current token with a, and move to the next token; proc B { - call B; case of the current token { - match the current token with e, b: - match the current token with b, and move to the next token; and move to the next token; c: - match the current token with c, - call B and move to the next token; e,d: do nothing - call B; } - match the current token with d, } and move to the next token; f: - call C } } follow set of B first set of C 43
  • 44. FIRST FUNCTION 44 • FIRST (E) = • FIRST (E’) = • FIRST (T) = • FIRST (T’) = • FIRST (F) =
  • 47. FIRST - EXAMPLE  P  i | c | n T S  Q  P | a S | b S c S T  R  b | ε  S  c | R n | ε  T  R S q  FIRST(P) =  FIRST(Q) =  FIRST(R) =  FIRST(S) =  FIRST(T) = 47
  • 48. FIRST - EXAMPLE  S  a S e | S T S  T  R S e | Q  R  r S r | ε  Q  S T | ε  FIRST(S) =  FIRST(R) =  FIRST(T) =  FIRST(Q) = 48
  • 49. FOLLOW SETS  FOLLOW(A) is the set of terminals (including end marker of input - $) that may follow non- terminal A in some sentential form.  FOLLOW(A) = {c | S ⇒+ …Ac…} ∪ {$} if S ⇒+ … A  For example, consider L ⇒+ (())(L)L Both ‘)’ and end of file can follow L  NOTE: ε is never in FOLLOW sets 49
  • 50. COMPUTING FOLLOW(A) 1. If A is start symbol, put $ in FOLLOW(A) 2. Productions of the form B  α A β, Add FIRST(β) – {ε} to FOLLOW(A) 3. Productions of the form B  α A or B  α A β where β ⇒* ε Add FOLLOW(B) to FOLLOW(A) 50
  • 51. EXAMPLE  E  T E′  E′  + T E′ | ε  T  F T′  T′  * F T′ | ε  F  ( E ) | id  FIRST(E) = {(, id}  FIRST(E′) = {+, ε}  FIRST(T) = {(, id}  FIRST(T′) = {*, ε}  FIRST(F) = {(, id}}  FOLLOW(E) = {$}  FOLLOW(E′) =  FOLLOW(T) =  FOLLOW(T′) =  FOLLOW(F) = Assume the first non-terminal is the start symbol Using rule #1 1. If A is start symbol, put $ in FOLLOW(A) 51
  • 52. EXAMPLE  E  T E′  E′  + T E′ | ε  T  F T′  T′  * F T′ | ε  F  ( E ) | id  FIRST(E) = {(, id}  FIRST(E′) = {+, ε}  FIRST(T) = {(, id}  FIRST(T′) = {*, ε}  FIRST(F) = {(, id}}  FOLLOW(E) = {$, )}  FOLLOW(E′) =  FOLLOW(T) = {+}  FOLLOW(T′) =  FOLLOW(F) = {*} Using rule #2 2. Productions of the form B  α A β, Add FIRST(β) – {ε} to FOLLOW(A) 52
  • 53. EXAMPLE  E  T E′  E′  + T E′ | ε  T  F T′  T′  * F T′ | ε  F  ( E ) | id  FIRST(E) = {(, id}  FIRST(E′) = {+, ε}  FIRST(T) = {(, id}  FIRST(T′) = {*, ε}  FIRST(F) = {(, id}}  FOLLOW(E) = {$, )}  FOLLOW(E′) = FOLLOW(E) = {$, )}  FOLLOW(T) = {+} ∪ FOLLOW(E′) = {+, $, )}  FOLLOW(T′) = FOLLOW(T) = {+, $, )}  FOLLOW(F) = {*} ∪ FOLLOW(T′) = {*, +, $, )} Using rule #3 3. Productions of the form B  α A or B  α A β where β ⇒* ε Add FOLLOW(B) to FOLLOW(A) 53
  • 54. EXAMPLE  S  ( A) | ε  A  T E  E  & T E | ε  T  ( A ) | a | b | c  FIRST(T) =  FIRST(E) =  FIRST(A) =  FIRST(S) =  FOLLOW(S) =  FOLLOW(A) =  FOLLOW(E) =  FOLLOW(T) = 54
  • 55. EXAMPLE  S  ( A) | ε  A  T E  E  & T E | ε  T  ( A ) | a | b | c  FIRST(T) = {(,a,b,c}  FIRST(E) = {&, ε }  FIRST(A) = {(,a,b,c}  FIRST(S) = {(, ε}  FOLLOW(S) = {$}  FOLLOW(A) = { ) }  FOLLOW(E) = FOLLOW(A) = { ) }  FOLLOW(T) = FIRST(E) ∪ FOLLOW(E) = {&, )} 55
  • 56. EXAMPLE  S  a S e | B  B  b B C f | C  C  c C g | d | ε  FIRST(C) =  FIRST(B) =  FIRST(S) =  FOLLOW(C) =  FOLLOW(B) =  FOLLOW(S) = {$} Assume the first non-terminal is the start symbol 1. If A is start symbol, put $ in FOLLOW(A) 56 2. Productions of the form B  α A β, Add FIRST(β) – {ε} to FOLLOW(A) 3. Productions of the form B  α A or B  α A β where β ⇒* ε Add FOLLOW(B) to FOLLOW(A)
  • 57. EXAMPLE  S  a S e | B  B  b B C f | C  C  c C g | d | ε  FIRST(C) = {c,d,ε}  FIRST(B) = {b,c,d,ε}  FIRST(S) = {a,b,c,d,ε}  FOLLOW(C) =  FOLLOW(B) =  FOLLOW(S) = { }$, e {c,d} ∪ FOLLOW(S) = {c,d,e,$} {f,g} ∪ FOLLOW(B) = {c,d,e,f,g,$} 57