Professional Documents
Culture Documents
H5 Context Free Grammar
H5 Context Free Grammar
H5 Context Free Grammar
The first three rules (1-3) forms the BASIS. They tell us
Many languages cannot be REGULAR. Thus we need to that the class of palindromes includes the strings , 0
consider larger classes of languages called “Context- and 1. None of the right sides of these rules (the
Free Languages (CFL's)” portions following the arrows) contains a variable,
which is why they form a basis for the definition.
CONTEXT-FREE GRAMMAR (CFG) – another notation
for describing languages; formal notation for expressing The last two rules (4-5) form the INDUCTIVE part of the
recursive definition definition. For instance, rule 4 says that if we take any
string from the class P, then 00 is also in class P.
The natural, recursive notations of CFL have:
Rule 5 likewise tells us that 11 is also in P.
P is in this grammar also the START SYMBOL. where A is a variable and (V T)*
S is a designated variable called the start
1-5 are PRODUCTIONS (or rules)
symbol.
Limitations:
I represents identifiers
E represents expressions (combination of Parse tree for a palindrome
values and operators that will be evaluated) (Yield – 0110)
Language:
(a + b)(a + b + 0 + 1)*
Two approaches:
a*(a + b00)
YIELD – aabbccdd
DERIVATION
TWO TYPES:
Example A:
A language that generates strings of 0’s and 1’s such A. Recursive Inference:
that the strings starts with either 0 or 1 followed by
101. String Lang Prod String(s)
used
Language: (0 + 1) 101
i 0 I 5 -
Strings included: 0101, 1101
ii 1 I 6 -
Formal definition: G = ({E, I}, {0, 1}, A, E} iii 10 I 8 ii
(0 + 1)101
E * E
( E ) Rightmost:
I E E E E I E I1 E I01
(2) (4) (7) (9)
E + E
EXAMPLE B:
DETERMINE IF CERTAIN STRINGS ARE IN THE
LANGUAGE OF A CERTAIN VARIABLE. Given a context-free language with:
Formal definition: G = ({S, A, B}, {0, 1, }, A, S} Formal definition: G = ({S, A, B}, {a, b, }, A, S}
2. A -> 0A | 2. S -> AB
3. A -> a
3. B -> 0B | 1B |
4. B -> b
S S
A 1 B A S B
S
0 A
A 0 B
a A 0
B
b
1 B
0 A
a b
Yield = aabb
DERIVATIONS: DERIVATIONS:
S => A1B => 0A1B => 00A1B => 001B => 0010B S => ASB => aSB => aABB => aaBB => aabB
(2) (2) (2) (3) (3) (2) (3) (4)
Rightmost:
Rightmost: S => ASB => ASb => AABb => AAbb => Aabb
S => A1B => A10B => A101B => A101 => 0A101 (4) (2) (4) (3)
(3) (3) (3) (2)
=> aabb (yield)
=> 00A101 => 00101 (yield)
(2)
EXAMPLE C:
Example A:
Given a context-free language with:
Language: (g + h) + (10g + h)
E
LEFTMOST DERIVATION:
E + E
( E ) ( E )
E + E
E + E
I I
I I
NOTE:
g h I g
Why such grammars are called `context free'?
h
I 0 Because all rules contain only one symbol on the left hand side
--- and wherever we see that symbol while doing a derivation,
we are free to replace it with the stuff on the right hand side.
That is, the `context' in which a symbol on the left hand side
1
of a rule occurs is unimportant --- we can always use the rule
to make the rewrite while doing a derivation.
PARSE TREE:
LEFTMOST DERIVATION:
E
E * E
( E )
I
E + E
I h
E + E
I 1
RIGHTMOST DERIVATION:
I
I
g h
I
I 0 I 1
g g
D. Recursive Inference: