Transcript Grammars

Classification of grammars
Definition: A grammar G is said to be
1) Right-linear if each production in P is
of the form A  xB or A  x where A
and B are in V and x in T*.
2) Context-free if each production is of the
form A  , where A is in V and  is
in (VT)*.
3) Context-sensitive if each production is
of the form , where ||  ||.
4) A grammar with no restrictions as
above is called unrestricted.
Context-Free Grammars
•Language generated by a CFG
•Derivation sequence
•Leftmost derivation sequence
•Rightmost derivation sequence
•Parse tree (Derivation tree)
•Yield of a parse tree
•Recursive inference
Examples:
1) A  
A0
A1
A0A0
A1A1
2) L = {0n1n | n  0}
Leftmost derivation – always replace the the
leftmost variable (nonterminal) by a production
body (right side of the production).

lm
*

lm
Rightmost derivation – always replace the the
rightmost variable (nonterminal) by a
production body (right side of the production).

rm
*

rm
Example:
EE+T|T
TT*F|F
F  (E) | a
Parse tress – tree representation for
derivations
• Each interior node is labeled by a
nonterminal
• If an interior node is labeled by A and
its children (from left to right) are X1,
X2, …, Xk, then A → X1X2…Xk is a
production
• Leaf nodes are nonterminals, terminals,
or 
Example:
S → SS | (S) | 
Recursive inference:
S  w0X1w1X2w2 … Xkwk
wi in T* and Xj in V
Then w generated by S is the concatenation
wi and strings generated by Xj in the order
Inference, Derivations, and Parse
Trees
Given a grammar G = (V, T, P, S), the
following are equivalent:
1. The recursive inference procedure
determines that a terminal string w is in
the language of variable A.
2. A * w
3. There is a parse tree with root A and yield
w.
Leftmost
derivation
Parse tree
Rightmost
derivation
Derivation
Recursive
inference
Theorem 5.12 (From Inference to Tree)
Let G = (V, T, P, S) be a CFG. If the
recursive inference procedure tells us that
the string w is in the language of a
nonterminal A, then there is a parse tree
with root A and yield w.
Theorem 5.14 (Tree to leftmost derivation)
Let G = (V, T, P, S) be a CFG and thre be a
parse tree with root labeled A and yield w
where w is in T*. Then thre is a leftmost
derivation sequence A * w in G.
Theorem 5.18 (Derivation to inference)
Let G = (V, T, P, S) be a CFG and suppose
there is a derivation A * w, where w is in
T*. Then the recursive inference procedure
applied to G determines that w is in the
language of A.
Ambiguous Grammars: A CFG G is
ambiguous if there is at least one
sentence w in L(G) for which
there is more than one distinct
derivation tree with frontier w.
Inherent Ambiguity: A CFL L is said
to be inherently ambiguous if all
its grammars are ambiguous
Example:
(1) E  E + E | E * E | (E) | a
is ambiguous
(2) L = {aibjck | i = j or j = k}
is inherently ambiguous