context free lang
TRANSCRIPT
![Page 1: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/1.jpg)
Context-Free Languages
![Page 2: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/2.jpg)
Regular Languages
}0:{ nba nn }{ Rww
**ba *)( ba
![Page 3: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/3.jpg)
Regular Languages
}{ nnba }{ Rww
Context-Free Languages
![Page 4: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/4.jpg)
Context-Free Languages
Pushdown
Automata
Context-Free
Grammars
stack
automaton
![Page 5: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/5.jpg)
Grammars
Grammars express languages
Example: the English language
verbpredicate
nounarticlephrasenoun
predicatephrasenounsentence
_
_
![Page 6: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/6.jpg)
walksverb
runsverb
dognoun
catnoun
thearticle
aarticle
![Page 7: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/7.jpg)
A derivation of “the dog walks”:
walksdogthe
verbdogthe
verbnounthe
verbnounarticle
verbphrasenoun
predicatephrasenounsentence
_
_
![Page 8: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/8.jpg)
A derivation of “a cat runs”:
runscata
verbcata
verbnouna
verbnounarticle
verbphrasenoun
predicatephrasenounsentence
_
_
![Page 9: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/9.jpg)
Language of the grammar:
L = { “a cat runs”,
“a cat walks”,
“the cat runs”,
“the cat walks”,
“a dog runs”,
“a dog walks”,
“the dog runs”,
“the dog walks” }
![Page 10: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/10.jpg)
Notation
dognoun
catnoun
Variable Terminal
Production Rules
![Page 11: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/11.jpg)
Another Example
Grammar:
Derivation of sentence :
S
aSbS
abaSbS
ab
aSbS S
![Page 12: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/12.jpg)
aabbaaSbbaSbS
aSbS S
aabb
S
aSbSGrammar:
Derivation of sentence :
![Page 13: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/13.jpg)
Other derivations:
aaabbbaaaSbbbaaSbbaSbS
aaaabbbbaaaaSbbbb
aaaSbbbaaSbbaSbS
![Page 14: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/14.jpg)
Language of the grammar
S
aSbS
}0:{ nbaL nn
![Page 15: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/15.jpg)
More Notation
Grammar PSTVG ,,,
:V
:T
:S
:P
Set of variables
Set of terminal symbols
Start variable
Set of Production rules
![Page 16: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/16.jpg)
Example
Grammar :
S
aSbSG
PSTVG ,,,
}{SV },{ baT
},{ SaSbSP
![Page 17: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/17.jpg)
More Notation
Sentential Form:
A sentence that contains
variables and terminals
Example:
aaabbbaaaSbbbaaSbbaSbS
Sentential Forms sentence
![Page 18: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/18.jpg)
We write:
Instead of:
aaabbbS*
aaabbbaaaSbbbaaSbbaSbS
![Page 19: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/19.jpg)
In general we write:
If:
nww*
1
nwwww 321
![Page 20: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/20.jpg)
By default:
ww*
![Page 21: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/21.jpg)
Example
S
aSbS
aaabbbS
aabbS
abS
S
*
*
*
*
Grammar Derivations
![Page 22: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/22.jpg)
baaaaaSbbbbaaSbb
aaSbbS
S
aSbS
Grammar
Example
Derivations
![Page 23: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/23.jpg)
Another Grammar Example
Grammar :
A
aAbA
AbS
Derivations:
aabbbaaAbbbaAbbAbS
abbaAbbAbS
bAbS
⇒⇒⇒⇒
⇒⇒⇒
⇒⇒
G
![Page 24: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/24.jpg)
More Derivations
aaaabbbbbaaaaAbbbbb
aaaAbbbbaaAbbbaAbbAbS
bbaS
bbbaaaaaabbbbS
aaaabbbbbS
nn
![Page 25: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/25.jpg)
Language of a Grammar
For a grammar
with start variable : G
S
}:{)( wSwGL
String of terminals
![Page 26: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/26.jpg)
Example
For grammar :
A
aAbA
AbS
}0:{)( nbbaGL nn
Since: bbaS nn
G
![Page 27: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/27.jpg)
A Convenient Notation
A
aAbA|aAbA
thearticle
aarticle
theaarticle |
![Page 28: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/28.jpg)
Example
A context-free grammar :
S
aSbS
aabbaaSbbaSbS
G
A derivation:
![Page 29: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/29.jpg)
A context-free grammar :
S
aSbS
aaabbbaaaSbbbaaSbbaSbS
G
Another derivation:
![Page 30: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/30.jpg)
S
aSbS
)(GL
(((( ))))
}0:{ nba nn
Describes parentheses:
![Page 31: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/31.jpg)
S
bSbS
aSaS
abbaabSbaaSaS
A context-free grammar : G
A derivation:
Example
![Page 32: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/32.jpg)
S
bSbS
aSaS
abaabaabaSabaabSbaaSaS
A context-free grammar : G
Another derivation:
![Page 33: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/33.jpg)
S
bSbS
aSaS
)(GL }*},{:{ bawwwR
![Page 34: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/34.jpg)
S
SSS
aSbS
ababSaSbSSSS
A context-free grammar : G
A derivation:
Example
![Page 35: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/35.jpg)
S
SSS
aSbS
abababaSbabSaSbSSSS
A context-free grammar : G
A derivation:
![Page 36: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/36.jpg)
S
SSS
aSbS
}prefixanyin
)()( and
),()(:{
v
vnvn
wnwnw
ba
ba
() ((( ))) (( ))
)(GL
Describes
matched
parentheses:
![Page 37: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/37.jpg)
Definition: Context-Free Grammars
Grammar
Productions of the form: xA
String of variables
and terminals
),,,( PSTVG
Variables Terminal
symbols
Start
variable
Variable
![Page 38: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/38.jpg)
*},:{)(*
TwwSwGL
),,,( PSTVG
![Page 39: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/39.jpg)
Definition: Context-Free Languages
A language is context-free
if and only if
there is a context-free grammar
with
L
G)(GLL
![Page 40: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/40.jpg)
Derivation Order
ABS .1
A
aaAA
.3
.2
B
BbB
.5
.4
aabaaBbaaBaaABABS54321
Leftmost derivation:
aabaaAbAbABbABS32541
Rightmost derivation:
![Page 41: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/41.jpg)
|AB
bBbA
aABS
Leftmost derivation:
abbbbabbbbB
abbBbbBabAbBabBbBaABS
Rightmost derivation:
abbbbabbBbb
abAbabBbaAaABS
![Page 42: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/42.jpg)
Derivation Trees
![Page 43: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/43.jpg)
ABS
ABS |aaAA |BbB
S
BA
![Page 44: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/44.jpg)
ABS |aaAA |BbB
aaABABS
a a A
S
BA
![Page 45: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/45.jpg)
ABS |aaAA |BbB
aaABbaaABABS
S
BA
a a A B b
![Page 46: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/46.jpg)
ABS |aaAA |BbB
aaBbaaABbaaABABS
S
BA
a a A B b
![Page 47: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/47.jpg)
ABS |aaAA |BbB
aabaaBbaaABbaaABABS
S
BA
a a A B b
Derivation Tree
![Page 48: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/48.jpg)
aabaaBbaaABbaaABABS
yield
aab
baa
S
BA
a a A B b
Derivation Tree
ABS |aaAA |BbB
![Page 49: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/49.jpg)
Ambiguity
![Page 50: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/50.jpg)
aEEEEEE |)(||
aaa
E
EE
EE
a
a a
aaaEaa
EEaEaEEE
*
leftmost derivation
![Page 51: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/51.jpg)
aEEEEEE |)(||
aaa
E
EE
a a
EE a
aaaEaa
EEaEEEEEE
leftmost derivation
![Page 52: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/52.jpg)
aEEEEEE |)(||
aaa
E
EE
a a
EE a
E
EE
EE
a
a a
Two derivation trees
![Page 53: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/53.jpg)
The grammar aEEEEEE |)(||
is ambiguous:
E
EE
a a
EE a
E
EE
EE
a
a a
string aaa has two derivation trees
![Page 54: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/54.jpg)
string aaa has two leftmost derivations
aaaEaa
EEaEEEEEE
aaaEaa
EEaEaEEE
*
The grammar aEEEEEE |)(||
is ambiguous:
![Page 55: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/55.jpg)
Definition:
A context-free grammar is ambiguous
if some string has:
two or more derivation trees
G
)(GLw
![Page 56: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/56.jpg)
In other words:
A context-free grammar is ambiguous
if some string has:
two or more leftmost derivations
G
)(GLw
(or rightmost)
![Page 57: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/57.jpg)
Why do we care about ambiguity?
E
EE
a a
EE a
E
EE
EE
a
a a
aaa
take 2a
![Page 58: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/58.jpg)
E
EE
EE
E
EE
EE
222
2
2 2 2 2
2
![Page 59: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/59.jpg)
E
EE
EE
E
EE
EE
6222
2
2 2 2 2
2
8222
4
2 2
2
6
2 2
24
8
![Page 60: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/60.jpg)
E
EE
EE
6222
2
2 2
4
2 2
2
6
Correct result:
![Page 61: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/61.jpg)
• We want to remove ambiguity
• Ambiguity is bad for programming languages
![Page 62: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/62.jpg)
Left Recursion & Right Recursion
It is possible for a recursive-descent parser to loop forever.
![Page 63: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/63.jpg)
The same effect can be achieved by rewriting the productions for A in the following manner, using a new nonterminal R:
![Page 64: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/64.jpg)
The left-recursion-elimination technique sketched in previous Fig. can also be applied to productions containing semantic actions.
First, the technique extends to multiple productions for A.
![Page 65: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/65.jpg)
![Page 66: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/66.jpg)
![Page 67: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/67.jpg)
![Page 68: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/68.jpg)
Position of Parser
![Page 69: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/69.jpg)
• There are three general types of parsers for grammars: • UNIVERSAL
• TOP-DOWN
• BOTTOM-UP
• Universal parsing methods such as • Cocke-Younger-Kasami algorithm
• Earley's algorithm can parse any grammar
• These general methods are, however, too inefficient to use in production compilers.
• The methods commonly used in compilers can be classified as being either top-down or bottom-up.
• Top-down methods build parse trees from the top (root) to the bottom (leaves), while Bottom-up methods start from the leaves and work their way up to the root.
• In either case, the input to the parser is scanned from left to right, one symbol at a time.
![Page 70: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/70.jpg)
• The most efficient top-down and bottom-up methods work only for subclasses of grammars,
• but several of these classes, particularly, LL and
LR grammars, are expressive enough to describe most of the syntactic constructs in modern programming languages.
• Parsers implemented by hand often use LL
grammars; • for example, the predictive-parsing approach
• Parsers for the larger class of LR grammars are usually constructed using automated tools.
![Page 71: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/71.jpg)
![Page 72: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/72.jpg)
![Page 73: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/73.jpg)
![Page 74: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/74.jpg)
![Page 75: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/75.jpg)
Associativity of operators Our grammar gives left associativity.
That is, if you traverse the parse tree in postorder and perform the indicated arithmetic you will evaluate the string left to right.
If you wished to generate right associativity, you would change the productions
![Page 76: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/76.jpg)
![Page 77: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/77.jpg)
![Page 78: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/78.jpg)
![Page 79: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/79.jpg)
![Page 80: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/80.jpg)
![Page 81: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/81.jpg)
![Page 82: Context Free Lang](https://reader034.vdocument.in/reader034/viewer/2022052307/557210fb497959fc0b8e0f1f/html5/thumbnails/82.jpg)