Professional Documents
Culture Documents
How To Convert A Left Linear Grammar To A Right Linear Grammar
How To Convert A Left Linear Grammar To A Right Linear Grammar
Roger L. Costello
May 28, 2014
Objective
This mini-tutorial will answer these questions:
1. What is a linear grammar? What is a left linear
grammar? What is a right linear grammar?
2
Objective
This mini-tutorial will answer these questions:
1. What is a linear grammar? What is a left linear
grammar? What is a right linear grammar?
2. Left linear grammars are evil – why?
3
Objective
This mini-tutorial will answer these questions:
1. What is a linear grammar? What is a left linear
grammar? What is a right linear grammar?
2. Left linear grammars are evil – why?
3. What algorithm can be used to convert a left
linear grammar to a right linear grammar?
4
Linear grammar
S → Aa
A → ab
6
Right linear grammar
• A right linear grammar is a linear grammar in
which the non-terminal symbol always occurs
on the right side.
• Here is a right linear grammar:
S → abaA
A→ε
7
Left linear grammars are evil
• Consider this rule from a left linear grammar:
A → Babc
• Can that rule be used to recognize this string:
abbabc
• We need to check the rule for B:
B → Cb | D
• Now we need to check the rules for C and D.
• This is very complicated.
• Left linear grammars require complex parsers.
8
Right linear grammars are good
• Consider this rule from a right linear grammar:
A → abcB
• Can that rule be used to recognize this string:
abcabb
• We immediately see that the first part of the string –
abc – matches the first part of the rule. Thus, the
problem simplifies to this: can the rule for B be used
to recognize this string :
abb
• Parsers for right linear grammars are much simpler.
9
Convert left linear to right linear
Now we will see an algorithm for converting any
left linear grammar to its equivalent right linear
grammar.
left linear right linear
S → Aa S → abaA
A → ab A→ε
Both grammars generate this language: {aba}
10
May need to make a new start symbol
11
Symbols used by the algorithm
• Let S denote the start symbol
• Let A, B denote non-terminal symbols
• Let p denote zero or more terminal symbols
• Let ε denote the empty symbol
12
Algorithm
1) If the left linear grammar has a rule S → p, then
make that a rule in the right linear grammar
2) If the left linear grammar has a rule A → p, then
add the following rule to the right linear grammar:
S → pA
3) If the left linear grammar has a rule B → Ap, add
the following rule to the right linear grammar:
A → pB
4) If the left linear grammar has a rule S → Ap, then
add the following rule to the right linear grammar:
A → p 13
Convert this left linear grammar
left linear
S → Aa
A → ab
14
Right hand side has terminals
S → Aa S → abA
A → ab
15
Right hand side of S has non-terminal
S → Aa S → abA
A → ab A→a
16
Equivalent!
S → Aa S → abA
A → ab A→a
17
Convert this left linear grammar
S → Ab S0 → S
S → Sb S → Ab
A → Aa make a new
start symbol S → Sb
A→a A → Aa
A→a
Convert this
18
Right hand side has terminals
S0 → S S0 → aA
S → Ab
S → Sb
A → Aa
A→a
S0 → S S0 → aA
S → Ab A → bS
S → Sb A → aA
A → Aa S → bS
A→a
3) If the left linear grammar has a rule B → Ap, add the
following rule to the right linear grammar: A → pB
20
Right hand side of start symbol has non-
terminal
S0 → S S0 → aA
S → Ab A → bS
S → Sb A → aA
A → Aa S → bS
A→a S→ε
S0 → S S0 → aA
S → Ab A → bS
S → Sb A → aA
A → Aa S → bS
A→a S→ε
22
Will the algorithm always work?
• We have seen two examples where the
algorithm creates a right linear grammar that
is equivalent to the left linear grammar.
• But will the algorithm always produce an
equivalent grammar?
• Yes! The following slide shows why.
23
Generate string p
• Let p = a string generated by the left linear
grammar.
• We will show that the grammar generated by
the algorithm also produces p.
24
Case 1: the start symbol produces p
Algorithm:
1) If the left linear grammar contains S → p, then put that rule in the right linear grammar.
2) If the left linear grammar contains A → p, then put this rule in the right linear grammar: S → pA
3) If the left linear grammar contains B → Ap, then put this rule in the right linear grammar: A → pB
4) If the left linear grammar contains S → Ap, then put this rule in the right linear grammar: A → p
25
Case 2: multiple rules needed
to produce p
Suppose p is produced by a sequence of n
production rules:
S → A1p1
→ A2p2p1
→ A3p3p2p1
→…
→ An-1pn-1…p3p2p1
→ pnpn-1…p3p2p1 p (p is composed of n symbols)
26
Case 2 (continued)
S → A1p1
→ A2p2p1
→ A3p3p2p1 Let’s see what right linear rules will be generated
→… by the algorithm for the rules implied by this
→ An-1pn-1…p3p2p1 production sequence.
→ pnpn-1…p3p2p1
27
Algorithm inputs and outputs
algorithm
28
Case 2 (continued)
S → A1p1 S → A1 p 1
→ A2p2p1
→ A3p3p2p1
→… algorithm
→ An-1pn-1…p3p2p1
→ pnpn-1…p3p2p1
A1 → p1 (see 4 below)
Algorithm:
1) If the left linear grammar contains S → p, then put that rule in the right linear grammar.
2) If the left linear grammar contains A → p, then put this rule in the right linear grammar: S → pA
3) If the left linear grammar contains B → Ap, then put this rule in the right linear grammar: A → pB
4) If the left linear grammar contains S → Ap, then put this rule in the right linear grammar: A → p
29
Case 2 (continued)
S → A1p1 A1 → A2p2
→ A2p2p1
→ A3p3p2p1
→… algorithm
→ An-1pn-1…p3p2p1
→ pnpn-1…p3p2p1
A2 → p2A1 (see 3 below)
Algorithm:
1) If the left linear grammar contains S → p, then put that rule in the right linear grammar.
2) If the left linear grammar contains A → p, then put this rule in the right linear grammar: S → pA
3) If the left linear grammar contains B → Ap, then put this rule in the right linear grammar: A → pB
4) If the left linear grammar contains S → Ap, then put this rule in the right linear grammar: A → p
30
Case 2 (continued)
S → A1p1
S → A1p1 A1 → A2p2
→ A2p2p1
→ A3p3p2p1
→… algorithm
→ An-1pn-1…p3p2p1
→ pnpn-1…p3p2p1
A1 → p1
A2 → p2A1
31
Case 2 (continued)
S → A1p1
A1 → A2p2
algorithm
32
Case 2 (continued)
S → A1p1 A2 → A3p3
→ A2p2p1
→ A3p3p2p1
→… algorithm
→ An-1pn-1…p3p2p1
→ pnpn-1…p3p2p1
A3 → p3A2 (see 3 below)
Algorithm:
1) If the left linear grammar contains S → p, then put that rule in the right linear grammar.
2) If the left linear grammar contains A → p, then put this rule in the right linear grammar: S → pA
3) If the left linear grammar contains B → Ap, then put this rule in the right linear grammar: A → pB
4) If the left linear grammar contains S → Ap, then put this rule in the right linear grammar: A → p
33
Case 2 (continued)
S → A1p1
A1 → A2p2
S → A1p1 A2 → A3p3
→ A2p2p1
→ A3p3p2p1
→… algorithm
→ An-1pn-1…p3p2p1
→ pnpn-1…p3p2p1
A1 → p1
A2 → p2A1
A3 → p3A2
34
Case 2 (continued)
S → A1p1
A1 → A2p2
A2 → A3p3
algorithm
35
Case 2 (continued)
S → A1p1 An-1 → pn
→ A2p2p1
→ A3p3p2p1
→… algorithm
→ An-1pn-1…p3p2p1
→ pnpn-1…p3p2p1
S → pnAn-1 (see 2 below)
Algorithm:
1) If the left linear grammar contains S → p, then put that rule in the right linear grammar.
2) If the left linear grammar contains A → p, then put this rule in the right linear grammar: S → pA
3) If the left linear grammar contains B → Ap, then put this rule in the right linear grammar: A → pB
4) If the left linear grammar contains S → Ap, then put this rule in the right linear grammar: A → p
36
Case 2 (concluded)
S → A1p1
A1 → A2p2
A2 → A3p3
…
An-1 → pn
algorithm
38
How we understand the algorithm
S → A1p1 These rules descend through the non-terminals until
A1 → A2p2 reaching a rule with terminals on the RHS, the terminals
A2 → A3p3 are output, then we unwind from the descent and output
…
the terminals.
An-1 → pn
algorithm
A 1 → p1
A2 → p2A1
Make the rule with terminals on the RHS the starting rule
A3 → p3A2
and output its terminals. Ascend through the other rules.
…
An-1 → pn-1An-2
S → pnAn-1
39
How we understand the algorithm
S → A1p1
A1 → A2p2
A2 → A3p3
…
An-1 → pn
40
How we understand the algorithm
S → A1p1
A1 → A2p2
A2 → A3p3
…
An-1 → pn
41
How we understand the algorithm
S → A1p1
A1 → A2p2
A2 → A3p3
…
If the left linear grammar contains S → Ap,
An-1 → pn
then put this rule in the right linear grammar:
A→p
algorithm
A 1 → p1
A2 → p2A1
A3 → p3A2
…
An-1 → pn-1An-2
S → pnAn-1
42
Left-linear grammars generate
Type 3 languages
• Every left-linear grammar can be converted to
an equivalent right-linear grammar.
– “Equivalent right-linear grammar” means the
grammar generate the same language.
• Right-linear grammars generate Type 3
languages.
• Therefore, every left-linear grammar
generates a Type 3 language.
43
Acknowledgement
The algorithm shown in these slides comes from
the wonderful book:
Introduction to Formal Languages by Gyorgy Revesz
44