Computer Basics

REGULAR
GRAMMARS
Module 04.1
COP4020 –
Programming Language
Concepts
Dr. Manuel E. Bermudez
TOPICS


Regular Grammars
Grammar to State
Diagram (FSA)
REGULAR GRAMMARS
Definition: A grammar G = (Φ, Σ, P, S) is regular iff
either (but not both):
Every production is of the form
A→
or A → B (right linear)
Every production is of the form
A→
or A → B (left linear),
where   Σ*, and
A, B  Φ.
REGULAR GRAMMARS
Examples:
G1: S → a
R → abaU
→ bU
→U
→ bR
U→b
Regular? Why?
→ aS
G2: S → a
→ Ub
→ Rb
R → Uaba
U →b
→ aS
Regular? Why?
REGULAR GRAMMARS
Let’s devise a machine that accepts L(G1).
S => a
=>
bU => bb
bR
baS …
G1: S→ a
→ bU
→ bR
=>
R →
→
U →
→
abaU
U
b
aS
babaU
1. All sentential forms (except sentences) have ONE nonterminal.
2. The nonterminal occurs in the right-most position.
3. Applicable productions depend only on that nonterminal.
REGULAR GRAMMARS
Encode possible derivation sequences with a relation
⊢ (moves-to) on pairs of the form (q, ), where
q – current state
 – remaining string to accept
So, S → bU implies
(S, bβ) ⊢ (U, β)
State “sentential
“moves
form ends in S”
to”
state “sentential
form ends in U”
REGULAR GRAMMARS

Define a graph,

one node per nonterminal,

actions on each sentential form.
S → bU implies
R → U implies
S → a implies
S
R
S
b

a
U
,
U
, and
F
.
REGULAR GRAMMAR TO TRANSITION
DIAGRAM
G1: S→ a
R → abaU
→ bU
→ bR
S
a
F
b
b
b
a
R
ε
aba
U
→U
U →b
→ aS
Advantage of
diagram:
Easier to visualize
REGULAR GRAMMAR TO TRANSITION DIAGRAM
In general, conversion from right-linear grammar
G=(Φ, Σ, P, S) to transition diagram:
1.
Nodes: Φ  {F}, F  Φ
2.
A
3.
A
4.
S
α
α
B
if A →  B
F
if A → 
ADVANTAGE OF TRANSITION
DIAGRAM: GOOD FOR PARSING
Example: Is “babaa” in L(G)?
Node
Input
Derivation
S
babaa
S =>
U
abaa
bU =>
S
baa
baS =>
U
aa
babU =>
S
a
babaS =>
F
babaa
Note: Use of graph is
non-deterministic.
Will fix later.
SUMMARY



Defined Regular Grammar
Conversion from Right-Linear
Grammar to Transition
Diagram (Graph)
Advantages of Graph:


Easier to Visualize
Can actually parse (nondeterministically)