Professional Documents
Culture Documents
Francisco Cantú
September 13, 2022
Grammars
Part 2
Grammar
rule
• A → λ is λ-rule
Some Rules
• A → aAb
• A → aA
• A → Ab
• A → AA
Interpretation of Rules
• A → aAb
Generates any number of as followed by the
same number of bs. Example left-right paren-
theses
• A → aAbb
Generates any number of as followed by the
twice the number of bs.
• A → aA
Generates any number of as preceding the vari-
able A.
• A → AA
Produces an additional A each time.
Exercise
if Σ = {a, b} and
∗ ∗ S∞
Σ = {a, b} = i=0{a, b}i
if Σ = {a, b} and
S∞
Σ∗ = {a, b}∗ = i=0 {a, b}i
S → aS
S → bS
S →a
S →b
(a + b)∗
Derivation
• G = (V, Σ, P, S)
• V = {S, A}
• Σ = {a, b}
• P: S → AA
A → AAA
A → bA
A → Ab
A→a
These four rules are written as:
A → AAA|bA|Ab|a
Derivations of the string ababaa
Grammars Define Languages
Let G = (V, Σ, P, S) be a context-free grammar
• S → aSa | aBa
• B → bB | b
It recognizes elements of the language
• S → aSdd | A
• A → bAc | bc
It recognizes elements of the language
• S → aSb | aSbb
• S→λ
It recognizes elements of the language
• S → abScB | λ
• B → bB | b
The S rule generates equal number of abs and
cBs. The B Rule generates b+
• S → AbAbA
• A → aA | λ
The grammar generates strings over {a, b} that
contain exactly two bs
• S → AbAbA
• A → aA | bA | λ
The grammar generates strings over {a, b} that
contain at least two bs
• S → aB |bA | λ
• A → aC | bS
• B → aS | bC
• C → aA | bB
The grammar generates strings over {a, b} that
contain an even number of as and an even number
of bs
More on Derivation Trees
Derivation Trees
Let G = (V, Σ, P, S) be a context-free grammar
and let S ⇒ ∗ w be a derivation of in G. The
derivation tree DT of S ⇒ ∗ w is an ordered tree
that can be built interatively as follows: