topics in context-free grammar cfg’sdefinition: context-free grammars definition 3.1.1 (a. sudkamp...
TRANSCRIPT
![Page 1: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/1.jpg)
Topics in Context-Free Grammar CFG’s
HUSSEIN S. AL-SHEAKH
1
![Page 2: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/2.jpg)
OutlineContext-Free Grammar
Ambiguous Grammars
LL(1) Grammars
Eliminating Useless Variables
Removing Epsilon
Nullable Symbols
2
![Page 3: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/3.jpg)
Context-Free Grammar (CFG)Context-free grammars are powerful enough to describe the syntax of most programming languages; in fact, the syntax of most programming languages is specified using context-free grammars. In linguistics and computer science, a context-free grammar (CFG) is a formal grammar in which every production rule is of the form
V → w Where V is a “non-terminal symbol” and w is a “string” consisting of terminals and/or non-terminals. The term "context-free" expresses the fact that the non-terminal V can always be replaced by w, regardless of the context in which it occurs.
3
![Page 4: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/4.jpg)
Definition: Context-Free GrammarsDefinition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)
A context-free grammar is a quadruple (V, Z, P, S) where:
V is a finite set of variables.
E (the alphabet) is a finite set of terminal symbols.
P is a finite set of rules (Ax).
Where x is string of variables and terminals
S is a distinguished element of V called the start symbol.
The sets V and E are assumed to be disjoint.
4
![Page 5: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/5.jpg)
Definition: Context-Free LanguagesA language L is context-free
IF AND ONLY IF
there is a grammar G with L=L(G) .
5
![Page 6: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/6.jpg)
Example
A context-free grammar :
A derivation:
SaSbSG
aabbaaSbbaSbS
)(GL }0:{ nba nn
(((( ))))6
![Page 7: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/7.jpg)
7
Derivation OrderABS .1
AaaAA
.3
.2
BBbB
.5
.4
aabaaBbaaBaaABABS54321
Leftmost derivation:
aabaaAbAbABbABS32541
Rightmost derivation:
![Page 8: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/8.jpg)
Derivation Trees
ABS
ABS |aaAA |BbB
S
BA
8
![Page 9: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/9.jpg)
ABS |aaAA |BbB
aaABABS
a a A
S
BA
9
![Page 10: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/10.jpg)
ABS |aaAA |BbB
aaABbaaABABS S
BA
a a A B b
10
![Page 11: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/11.jpg)
ABS |aaAA |BbB
aaBbaaABbaaABABS S
BA
a a A B b
11
![Page 12: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/12.jpg)
ABS |aaAA |BbB
aabaaBbaaABbaaABABS S
BA
a a A B b
Derivation Tree
12
![Page 13: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/13.jpg)
aabaaBbaaABbaaABABS
yield
aabbaa
S
BA
a a A B b
Derivation Tree
ABS |aaAA |BbB
13
![Page 14: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/14.jpg)
Ambiguous Grammars problem: compilers use parse trees to interpret the meaning of parsed expressions. Assigns a unique parse tree to each string in the language is important in many application.A CFG is ambiguous if there is in its language that has at least two different parse trees (yield of two or more parse trees).Two different leftmost / rightmost derivations should produce different parse trees.Definition:A context-free grammar is ambiguous if some string has: two or more leftmost/rightmost derivation trees.
14Q1) give a definition and example of ambiguous Grammars?
G )(GLw
![Page 15: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/15.jpg)
15
The grammar aEEEEEE |)(|| IS ambiguous:
E
EE
a a
EE a
E
EE
EE
a
a a
string aaa has two derivation trees
Replaces the Second child of the root by E*E Replaces the first child of the root by E+E
![Page 16: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/16.jpg)
16
aaaEaaEEaEEEEEE
aaaEaaEEaEaEEE
*
The grammar aEEEEEE |)(|| IS ambiguous:
string aaa has two derivation trees
![Page 17: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/17.jpg)
Why do we care about ambiguity?
17
222
?
Lets
![Page 18: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/18.jpg)
18
Correct result is ???
![Page 19: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/19.jpg)
19
E
EE
EE
6222
2
2 2
4
2 2
2
6
Correct result =
![Page 20: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/20.jpg)
20
We need to remove ambiguity
Therefore,
Ambiguity is bad for programming languages
![Page 21: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/21.jpg)
21
Fix the ambiguous grammar:
aEEEEEE |)(||
New non-ambiguous grammar:
aFEF
FTFTT
TETEE
)(
![Page 22: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/22.jpg)
22
Unique derivation tree
![Page 23: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/23.jpg)
23
The grammar :
aFEF
FTFTT
TETEE
)(
IS non-ambiguous:Every string hasa unique derivation tree
G
)(GLw
![Page 24: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/24.jpg)
LL(1) Grammars“Leftmost derivation, Left-to-right scan, 1 symbol lookahead.” First L: scans input from left to right. Second L: produces a leftmost derivation. 1: uses one input symbol of lookahead at each step to make a
parsing decision. A grammar whose parsing table has no multiply-defined entries is
a LL(1) grammar. No ambiguous or left-recursive grammar can be LL(1)
24
![Page 25: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/25.jpg)
Definition 16.1.1 from (A. Sudkamp book – Language and Machine 2ed Ed.)
Let G = (V, E, P, S) be a context-free grammar and A V.The lookahead set of the variable A, LA(A), is defined by
LA(A) = { x | S * uAv *: ux * }
For each rule A w in P, the lookahead set of the rule A w is defined byLA(A -> w) = {x | wv* x where x * and S * uAv}
LA(A) consists of all terminal strings derivable from strings Av, where uAv is a left
sentential form of the grammar.
LA(A w) is the subset of LA(A) in which the subderivations Av *x are initiated with the rule A w.
25
![Page 26: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/26.jpg)
Example 16.1.1 From (A. Sudkamp book – Language and Machine 2ed Ed.)
The lookahead sets are constructed for the variables and the rules of the grammar
G1: S Aabd |cAbcdA a I b I
LA(S) consists of all terminal strings derivable from S.LA(S) = {aabd, babd, abd, cabcd, cbbcd, cbcd}LA(S Aabd) = {aabd, babd, abd}LA(S cAbcd) = {cabcd, cbbcd, cbcd}
Knowledge of the first symbol of the lookahead string is sufficient to select the appropriate S rule.
26
![Page 27: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/27.jpg)
Lookahead Example Cont.We must consider derivations from all the left sentential forms of G1 that contain A, to construct the lookahead set for the variable A.There are only two such sentential forms:
Aabd and cAbcdThe lookahead sets consist of terminal strings derivable from Aabd and Abcd are:
LA(A a) = {aabd, abcd}LA(A b) = {babd, bbcd}LA(A ) = {abd, bcd}
The substring ab can be obtained by applying A a to Abcd and by applying A to Aabd.
27
![Page 28: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/28.jpg)
Length-Three Lookahead Looking ahead three symbols (length-three) in the input string provides sufficient information to discriminate between these rules. A top-down parser with a three-symbol lookahead can deterministically construct derivations in the grammar G1.The length-three lookahead sets for the rules of the grammar G1
G1: LA3(S Aabd) = {aab , bab , abd}
LA3(S cAbcd) = {cab , cbb , cbc}
LA3(A a) = {aab , abc }
LA3(A b) = {bab , bbc }
LA(A ) = {abd, bcd}
Since there is no string in common in the length three lookahead sets of the S rules or the A rules, a three symbol lookahead is sufficient to determine the appropriate rule of G1.
28
![Page 29: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/29.jpg)
Example 16.1.4 From (A. Sudkamp book – Language and Machine 2ed Ed.)
The language {ai abci | i > 0} is generated by each of the grammars G1, G2, and G3. The minimal length lookahead sets necessary for discriminating between alternative productions are given for these grammars.
29
Three symbol lookahead is required to determine the appropriate rule
S aSc and Saabc using (left factoring) technique to reduces the length of the lookahead needed to select the rules.
The recursive A rule generates an a while the nonrecursive rule terminates the derivation by generating a b.
Q2: Give an example to show the deference between lookahead sets?
![Page 30: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/30.jpg)
LL(1) Grammar Example Construct the parse table for the following LL(1) grammar.
This grammar is left-recursive, ambiguous and requires left-factoring. It needs to be modified before we build a predictive parser for it:
EE+EEE*EE(E)Eid
EE+TTT*FF(E)Fid
Remove ambiguity:
ETE'E'+TE'|TFT'T'*FT'|F(E)Fid
Remove left recursion:
![Page 31: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/31.jpg)
Compute FIRST(X) as follows:
31
◦ if X is a terminal, then FIRST(X)={X}◦ if X is a production, then add to FIRST(X)◦ if X is a non-terminal and XY1Y2...Yn is a production, add FIRST(Yi)
to FIRST(X) if the preceding Yjs contain in their FIRSTs
![Page 32: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/32.jpg)
Compute FOLLOW as follows:
32
◦ FOLLOW(S) contains EOF◦ For productions AB, everything in FIRST() except goes into
FOLLOW(B)◦ For productions AB or AB where FIRST() contains ,
FOLLOW(B) contains everything that is in FOLLOW(A)
![Page 33: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/33.jpg)
Building a parserThe grammar:
ETE'E'+TE'|TFT'T'*FT'|F(E)Fid
FIRST(E) = {(, id}FIRST(T) = {(, id}FIRST(F) = {(, id}FIRST(E') ={+, }FIRST(T') ={*, }FOLLOW(E) = {$, )}FOLLOW(E') = {$, )}FOLLOW(T) = {+, $, )}FOLLOW(T') = {+, $, )} FOLLOW(F) = {*, +, $, )}
(first = first terminal after arc, if not, non-terminal derivation )(follow= first (next), if null follow (non-terminal)
![Page 34: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/34.jpg)
Parsing table
EE'TT'F+*()id$
+
E'+TE'
T'
match
*
T'*FT'
match
(ETE'
TFT'
F(E)
match
)
E'
T'
match
idETE'
TFT'
Fid
match
$
E'
T'
accept
![Page 35: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/35.jpg)
Eliminating Useless VariablesContext-Free grammars can be badly designed, some variables that play no role in the derivation of any terminal string.
A symbol X is useful for Grammar G = {V, T, P, S}, if there is some derivation of the form S ═>* a X b ═>* w , where w є T*.
X є V or X є T.
The sentential form of a X b might be the first or last derivation.
If X is not useful, then X is useless .
35Q3) give example of useless symbols.
![Page 36: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/36.jpg)
Characteristics of useful symbols1. X is generating if X ═>* w for some terminal string w.
Every terminal is generating since w can be that terminal itself, which is derived by 0 steps.
2. X is reachable if there is a derivation
S ═>* a X b for some a and b.
A symbol which is useful is surely to be both generating and reachable.
![Page 37: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/37.jpg)
37
Removing All Useless VariablesStep 1: Remove Nullable Variables
Step 2: Remove Unit-Productions
Step 3: Remove Useless Variables
![Page 38: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/38.jpg)
38
Nullable Variables
:production AA
Basis: If there is a production A ε, then A is nullable.
Induction: If there is a production A , and all symbols of are nullable, then A is nullable.
TheoremIf L is a CFL, then L-{ε} has a CFG with no ε-productions.
![Page 39: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/39.jpg)
39
Removing Nullable Variables
Example Grammar:
MaMbM
aMbS
Nullable variable
![Page 40: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/40.jpg)
40
M
MaMbM
aMbS
abMaMbM
abSaMbS
Final Grammar
![Page 41: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/41.jpg)
41
Unit-Productions
BAUnit Production:
(a single variable in both sides)
![Page 42: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/42.jpg)
42
Removing Unit Productions
AA
Is removed immediately
Observation:
![Page 43: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/43.jpg)
43
Example Grammar:
bbBABBAaAaAS
![Page 44: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/44.jpg)
44
bbBABBAaAaAS
Substitute
BA
bbBBAB
aAaBaAS
|
|
![Page 45: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/45.jpg)
45
Remove
bbBBAB
aAaBaAS
|
|
bbBABaA
aBaAS
|
BB
![Page 46: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/46.jpg)
46
Substitute
AB bbBaA
aAaBaAS
||
bbBABaA
aBaAS
|
![Page 47: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/47.jpg)
47
Remove repeated productions
bbBaA
aBaAS
|
bbBaA
aAaBaAS
||
Final grammar
Q4) give example of removing Unit Productions .
![Page 48: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/48.jpg)
48
Useless Productions
aAAAS
SaSbS
aAaaaaAaAASSome derivations never terminate...
Useless Production
![Page 49: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/49.jpg)
49
In general:IF
wxAyS
then variable is usefulA
otherwise, variable is uselessA
)(GLw
contains only terminals
![Page 50: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/50.jpg)
50
A production is useless if any of its variables is useless
xA
DCCBaAAAS
SaSbS
Productionsuseless
useless
useless
useless
Variables
useless
useless
useless
![Page 51: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/51.jpg)
51
Removing Useless ProductionsExample Grammar:
aCbCaaBaA
CAaSS
||
![Page 52: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/52.jpg)
52
First: find all variables that can producestrings with only terminals
aCbCaaBaA
CAaSS
|| },{ BA
AS
},,{ SBA
Round 1:
Round 2:
![Page 53: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/53.jpg)
53
Keep only the variablesthat produce terminal symbols:
aCbCaaBaA
CAaSS
||
},,{ SBA
aaBaA
AaSS
|
(the rest variables are useless)
Remove useless productions
![Page 54: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/54.jpg)
54
Second: Find all variablesreachable from
aaBaA
AaSS
|
S A B
Use a Dependency Graph
notreachable
S
![Page 55: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/55.jpg)
55
Keep only the variablesreachable from S
aaBaA
AaSS
|
aAAaSS
|
Final Grammar
(the rest variables are useless)
Q5) give example of remove useless productions
![Page 56: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/56.jpg)
References Elaine A. Rich (2008) Automata, Computability, and Complexity: Theory and Applications, Pearson Prentice Hall.
T. A. Sudkamp, Languages and machines: an introduction to the theory of computer science. Reading, MA: Addison Wesley, 1994.
56
![Page 57: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/57.jpg)
57
![Page 58: Topics in Context-Free Grammar CFG’sDefinition: Context-Free Grammars Definition 3.1.1 (A. Sudkamp book – Language and Machine 2ed Ed.)A context-free grammar is a quadruple (V,](https://reader036.vdocument.in/reader036/viewer/2022062605/5fd62c2a3d8b09413a569bbd/html5/thumbnails/58.jpg)
58
Thank You