2 Grammars
2 Grammars
Grammars
• G = (VN, VT, P, S)
– VN : nonterminal alphabet or vocabulary
– VT : terminal alphabet or vocabulary
– V = VN VT
– S VN : a particular element of VN called axiom or initial
(Start) symbol
– P VN+ V* : set of rewriting rules, or productions
P = {(, ) | VN+ V*}
for ease of notation, write (, ) as
to emphasize the action of rewriting
5
Example
• VN = {S, A, B, C, D}
• VT = {a,b,c}
• S
• P = {S AB, BA cCD, CBS ab, A e}
6
L(G) = {x VT* | S * x}
A first example
G1 = ({S, A, B}, {a, b, 0},
{S aA, A aS, S bB, B bS, S 0}, S)
example derivations:
S0
S aA aaS aa0
S bB bbS bb0
S aA aaS aabB aabbS aabb0
Second example
some derivations:
S ab
S aSb aabb
S aSb aaSbb aaabbb
Some answers
Classes of grammars
– Regular grammars are also context free, but not vice versa
– G1 above is regular, not so G2.
– (For the empty string, we also need the rule S → e)
15
Monotonic Grammars:
●
Every rule has the form , where || ≤ ||
●
As before, we need S → for the empty string, but S cannot occur
in right parts of rules
●
Regular grammars are clearly monotonic
●
while CF grammars appear to be in general not monotonic,
because they allow rules A → .
●
On the other hand, we can easily remove such rules:
●
e.g.: a rule B → abaAcDA can be replaced by the rules
B → abaAcDA | abacDA | abaAcD | abacD
●
Note: languages defined by monotonic grammars are called
contextual and are defined by Linear Automata (i.e. TM where the
tape length is bounded)
16
Monotonic g. (type 1)
CF grammars (type 2)
Regular g. (type 3)
17
L3 L2 L1 L0
, S / b a
, S / b S a
q0
, Z0 / Z0 S q1
, Z0 /
q2
a, a /
b, b /
21
S aSb | ab
S aSb aabb
a a b b a a b b a a b b
a
S S
S b b
Z0 Z0 Z0
a a b b a a b b … a a b b
a
b b …
b
Z0 b
Z0 Z0
22
a B A C b a B A’ C b
q q’