Compact L5 Final 1
Compact L5 Final 1
Grammars Grammars
Definition Example
Grammars Grammars
Definition
5 6
1
Grammars Right-Linear Grammars
Example
Definition
Given the grammar:
A Grammar G= (V, T ,P,S) is called right-
linear grammar if every production is of
G = {{S },{a , b},{S → λ , S → aSb}, S ) the form A à xB, or A à x where
A,B ∈ V, x ∈ T*
The language generated by this grammar is: Example: The grammar
x à 0x | 1y
y à 0x | 1z
L(G ) = {a n b n | n ≥ 0} z à 0x | 1z | ?
Is a right-linear grammar.
7
Definition Definition
A Grammar G= (V, T ,P,S) is called left- A Grammar G= (V, T ,P,S) is called regular
linear grammar if every production is of grammar if its is left- or right-linear
the form A à Bx, or A à x where
A,B ∈ V, x ∈ T*
Example: The grammar Example: The grammar
x à x0 | y1 x à 0x | 1y
Example: The grammar
y à x0 | z1 y à 0x | 1z
x à x0 | y1
z à x0 | z1 | ? z à 0x | 1z | ?
y à x0 | z1
Is a left-linear grammar, Is a right-linear grammar,
z à x0 | z1 | ?
hence is Regular Grammar Hence is Regular Grammar
Is a left-linear grammar.
Grammars Grammars
Example 1
L = { w ∈ { a , b }* | length ( w ) is EVEN }
Write a grammar that generate the language:
E → λ E → λ | aO | bO L = {w ∈{a , b, c}* | w does not contain abc}
| aaE | abE
| baE | bbE O→ aE | bE reset → b reset | c reset
| a seenA |λ
Example 2
seenA → a seenA | c reset
Write a grammar that generate the language:
| b seenAB |λ
L = {w ∈ {a , b}* | w has EVEN number of b' s}
seenAB → a seenA
E → λ | aE | bO
| b reset |λ
O → aO | bE
2
Context-free Grammars
Definition CFG = (V, T, P, S)
zOne-step Derivation
zContext
Context--freeness: An A-rule can be
uAv A ⇒
→ω
uωv
applied whenever A occurs in a string,
zw is derivable from v in CFG, if there is a
irrespective of the context (that is, non-
finite sequence of rule applications such
terminals and terminals around A).
that:
v ⇒ w1 ⇒ ... ⇒ wn = w
In this case we can write this derivation as v *w
Rightmost derivation: if in every step the rightmost zIn general S ⇒….⇒ wwR for
variable is selected for reduction
w∈{a,b}*.
L(G)={wwR : w∈{a,b}*}
3
Context-free Grammars Context-free Grammars
Example 3
Example 2 G = ({S , A, B},{a , b},
Take the CFG S → 0 | 1 | ¬(S) | (S)∨(S) | (S)∧(S), which
{S → AB , A → aA | λ , generates all proper Boolean formulas that use
B → Bb | λ}, “0”, “1”, “¬”, “∨”, “∧”, “(“ and “)”.
Example 5
Example 4
Consider the CFG G:
S → aSa | aBa
Consider the CFG:
G = {{ S }, { a , b }, { S → λ , S → aSb }, S )
B → bB | b
zDerivation of aabb is
L( B) = {b m | m > 0}
S ⇒ aSb ⇒ aaSbb ⇒ aabb
L( S ) = {a n bm a n | n > 0 ∧ m > 0}
L(G)= L(S)
B → bB | λ
The language generated by G1 is:
B → bB | λ
L (G1) = {a nb m a n | n ≥ 0 ∧ m ≥ 0}
The language generated by G1 and G 2 is:
Consider the CFG G2: S → abSc | λ L(G1) = L(G 2) = L(S)
4
Context-free Grammars Context-free Grammars
Example 8 Exercise
Exercise 3 Example
Consider the CFG G=({S,Z},{0,1},S,P) with
P: S → 0S1 | 0Z1
Z → 0Z | ?
What is the CFG ({S},{(,)},S,P) that produces What is the language generated by G?
the language of correct parentheses like (), (()), or ()(())?
Answer: L(G) = {0i 1j | i≥j }
27 28
n Can you make Context Free Grammars for the following? For a CFG G=(V,T,S,P) a derivation
tree has the following properties: Example
a) { 0n1n : n=
n=0}
b) { 0n1m : n,m=
n,m =0} 1) The root is labeled S S
2) Each leaf is from T∪{?}
c) Arithmetic a,b,c formulas like a+b×c+a (without ())
a+b× 3) Each interior node is from V
4) If node has label A∈V and ( S ) ∨ ( S )
n Answers: its children a1…an (from L to R),
a) S ? 0S1 | ? then P must have the rule
b) S ? 0S | R and R ? 1R | ? A? a1…an (with aj∈V∪T∪{?}) 0 ( S ) ∧ ( S )
5) A leaf labeled ? is a single
c) S×S
S ? a | b | c | S+S | S× child (has no siblings). 1
0
For partial derivation trees we have:
29 2a) Each leaf is from V∪T∪{?} 30
5
Context-free Grammars Context-free Grammars
Derivation Tree: Example Derivation Tree: Notes
Take the CFG S → 0 | 1 | ¬(S) | (S)∨(S) | (S)∧(S), which Application of a production rule A ? x is
generates all proper Boolean formulas that use “0”, “1”, represented by node A with children x.
“¬”, “∨”, “∧”, “(“ and “)”. (Note that the tree is ordered:
the ordering of the nodes matters.)
S
The derivation S ⇒* (0)∨((0)∧(1)) can be expressed by
the following derivation tree: S The root has variable S.
0 1 31
0 1 32
G = {{ S }, { a , b }, { S → λ , S → aSb }, S )
n Looking
at a tree you see the derivation without the
unnecessary information about its order.
zThe derivation of aabb is:
n Theorem
Theorem:: Let G be a CFG. We have w∈L(G L(G)) if and S ⇒ aSb ⇒ aaSbb ⇒ aabb
only if there exists a derivation tree of G with yield w.
S
n Also, y is a sentential form of G if and only if there exists
a partial derivation tree for G. zDerivation tree is S
n Remember: the root always has to be S. a b
a λ b
33
A
<EXPR>
B <EXPR>
<EXPR> <EXPR>
0 0 # 1 1
35 36
a + a * a
6
Context-free Grammars
Exercise
37