# How to convert a left linear grammar to

- Slides: 44

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 • A linear grammar is a context-free grammar that has at most one non-terminal symbol on the right hand side of each grammar rule. – A rule may have just terminal symbols on the right hand side (zero non-terminals). • Here is a linear grammar: S → a. A A → a. Bb B → Bb 5

Left linear grammar • A left linear grammar is a linear grammar in which the non-terminal symbol always occurs on the left side. • Here is a left 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 → aba. A 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 → abc. B • 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 A → ab S → aba. A A→ε Both grammars generate this language: {aba} 10

May need to make a new start symbol The algorithm on the following slides assume that the left linear grammar doesn’t have any rules with the start symbol on the right hand side. – If the left linear grammar has a rule with the start symbol S on the right hand side, simply add this rule: S 0 → S 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 → p. A 3) If the left linear grammar has a rule B → Ap, add the following rule to the right linear grammar: A → p. B 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 left linear right linear S → Aa A → ab S → ab. A 2) If the left linear grammar has this rule A → p, then add the following rule to the right linear grammar: S → p. A 15

Right hand side of S has non-terminal left linear right linear S → Aa A → ab S → ab. A A→a 4) If the left linear grammar has S → Ap, then add the following rule to the right linear grammar: A → p 16

Equivalent! left linear right linear S → Aa A → ab S → ab. A A→a Both grammars generate this language: {aba} 17

Convert this left linear grammar original grammar S → Ab S → Sb A → Aa A→a left linear make a new start symbol S 0 → S S → Ab S → Sb A → Aa A→a Convert this 18

Right hand side has terminals left linear right linear S 0 → S S → Ab S → Sb A → Aa A→a S 0 → a. A 2) If the left linear grammar has this rule A → p, then add the following rule to the right linear grammar: S → p. A 19

Right hand side has non-terminal left linear right linear S 0 → S S → Ab S → Sb A → Aa A→a S 0 → a. A A → b. S A → a. A S → b. S 3) If the left linear grammar has a rule B → Ap, add the following rule to the right linear grammar: A → p. B 20

Right hand side of start symbol has non-terminal left linear right linear S 0 → S S → Ab S → Sb A → Aa A→a S 0 → a. A A → b. S A → a. A S → b. S S→ε 4) If the left linear grammar has S → Ap, then add the following rule to the right linear grammar: A → p 21

Equivalent! left linear right linear S 0 → S S → Ab S → Sb A → Aa A→a S 0 → a. A A → b. S A → a. A S → b. S S→ε Both grammars generate this language: {a+b+} 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 Suppose the left linear grammar has this rule: S → p. Then the right linear grammar will have the same rule (see 1 below). So the right linear grammar will also produce p. Algorithm: 1) 2) 3) 4) If the left linear grammar contains S → p, then put that rule in the right linear grammar. If the left linear grammar contains A → p, then put this rule in the right linear grammar: S → p. A If the left linear grammar contains B → Ap, then put this rule in the right linear grammar: A → p. B 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 → A 1 p 1 → A 2 p 2 p 1 → A 3 p 3 p 2 p 1 →… → An-1 pn-1…p 3 p 2 p 1 → pnpn-1…p 3 p 2 p 1 p (p is composed of n symbols) 26

Case 2 (continued) S → A 1 p 1 → A 2 p 2 p 1 → A 3 p 3 p 2 p 1 →… → An-1 pn-1…p 3 p 2 p 1 → pnpn-1…p 3 p 2 p 1 Let’s see what right linear rules will be generated by the algorithm for the rules implied by this production sequence. 27

Algorithm inputs and outputs left linear rules algorithm right linear rules 28

Case 2 (continued) S → A 1 p 1 → A 2 p 2 p 1 → A 3 p 3 p 2 p 1 →… → An-1 pn-1…p 3 p 2 p 1 → pnpn-1…p 3 p 2 p 1 S → A 1 p 1 algorithm A 1 → p 1 (see 4 below) Algorithm: 1) 2) 3) 4) If the left linear grammar contains S → p, then put that rule in the right linear grammar. If the left linear grammar contains A → p, then put this rule in the right linear grammar: S → p. A If the left linear grammar contains B → Ap, then put this rule in the right linear grammar: A → p. B If the left linear grammar contains S → Ap, then put this rule in the right linear grammar: A → p 29

Case 2 (continued) S → A 1 p 1 → A 2 p 2 p 1 → A 3 p 3 p 2 p 1 →… → An-1 pn-1…p 3 p 2 p 1 → pnpn-1…p 3 p 2 p 1 A 1 → A 2 p 2 algorithm A 2 → p 2 A 1 (see 3 below) Algorithm: 1) 2) 3) 4) If the left linear grammar contains S → p, then put that rule in the right linear grammar. If the left linear grammar contains A → p, then put this rule in the right linear grammar: S → p. A If the left linear grammar contains B → Ap, then put this rule in the right linear grammar: A → p. B If the left linear grammar contains S → Ap, then put this rule in the right linear grammar: A → p 30

Case 2 (continued) S → A 1 p 1 → A 2 p 2 p 1 → A 3 p 3 p 2 p 1 →… → An-1 pn-1…p 3 p 2 p 1 → pnpn-1…p 3 p 2 p 1 S → A 1 p 1 A 1 → A 2 p 2 algorithm A 1 → p 1 A 2 → p 2 A 1 31

Case 2 (continued) S → A 1 p 1 A 1 → A 2 p 2 algorithm A 1 → p 1 A 2 → p 2 A 1 From A 2 we obtain p 2 p 1: A 2 → p 2 A 1 → p 2 p 1 32

Case 2 (continued) S → A 1 p 1 → A 2 p 2 p 1 → A 3 p 3 p 2 p 1 →… → An-1 pn-1…p 3 p 2 p 1 → pnpn-1…p 3 p 2 p 1 A 2 → A 3 p 3 algorithm A 3 → p 3 A 2 (see 3 below) Algorithm: 1) 2) 3) 4) If the left linear grammar contains S → p, then put that rule in the right linear grammar. If the left linear grammar contains A → p, then put this rule in the right linear grammar: S → p. A If the left linear grammar contains B → Ap, then put this rule in the right linear grammar: A → p. B If the left linear grammar contains S → Ap, then put this rule in the right linear grammar: A → p 33

Case 2 (continued) S → A 1 p 1 → A 2 p 2 p 1 → A 3 p 3 p 2 p 1 →… → An-1 pn-1…p 3 p 2 p 1 → pnpn-1…p 3 p 2 p 1 S → A 1 p 1 A 1 → A 2 p 2 A 2 → A 3 p 3 algorithm A 1 → p 1 A 2 → p 2 A 1 A 3 → p 3 A 2 34

Case 2 (continued) S → A 1 p 1 A 1 → A 2 p 2 A 2 → A 3 p 3 algorithm A 1 → p 1 A 2 → p 2 A 1 A 3 → p 3 A 2 From A 3 we obtain p 3 p 2 p 1: A 3 → p 3 A 2 → p 3 p 2 A 1 → p 3 p 2 p 1 35

Case 2 (continued) S → A 1 p 1 → A 2 p 2 p 1 → A 3 p 3 p 2 p 1 →… → An-1 pn-1…p 3 p 2 p 1 → pnpn-1…p 3 p 2 p 1 An-1 → pn algorithm S → pn. An-1 (see 2 below) Algorithm: 1) 2) 3) 4) If the left linear grammar contains S → p, then put that rule in the right linear grammar. If the left linear grammar contains A → p, then put this rule in the right linear grammar: S → p. A If the left linear grammar contains B → Ap, then put this rule in the right linear grammar: A → p. B If the left linear grammar contains S → Ap, then put this rule in the right linear grammar: A → p 36

Case 2 (concluded) S → A 1 p 1 A 1 → A 2 p 2 A 2 → A 3 p 3 … An-1 → pn algorithm A 1 → p 1 A 2 → p 2 A 1 A 3 → p 3 A 2 … An-1 → pn-1 An-2 S → pn. An-1 From S we obtain pn…p 2 p 1: S → pn. An-1 → pnpn-1 An-2 →… → pnpn-1…p 3 A 2 → pnpn-1…p 3 p 2 A 1 → pn…p 3 pn-1 p 2 p 1 (this is the desired string, p) 37

We have shown that for any string p generated by the left linear grammar, the right linear grammar produced by the algorithm will also generate p. 38

How we understand the algorithm S → A 1 p 1 A 1 → A 2 p 2 A 2 → A 3 p 3 … An-1 → pn These rules descend through the non-terminals until reaching a rule with terminals on the RHS, the terminals are output, then we unwind from the descent and output the terminals. algorithm A 1 → p 1 A 2 → p 2 A 1 A 3 → p 3 A 2 … An-1 → pn-1 An-2 S → pn. An-1 Make the rule with terminals on the RHS the starting rule and output its terminals. Ascend through the other rules. 39

How we understand the algorithm S → A 1 p 1 A 1 → A 2 p 2 A 2 → A 3 p 3 … An-1 → pn algorithm If the left linear grammar contains A → p, then put this rule in the right linear grammar: S → p. A A 1 → p 1 A 2 → p 2 A 1 A 3 → p 3 A 2 … An-1 → pn-1 An-2 S → pn. An-1 40

How we understand the algorithm S → A 1 p 1 A 1 → A 2 p 2 A 2 → A 3 p 3 … An-1 → pn algorithm If the left linear grammar contains B → Ap, then put this rule in the right linear grammar: A → p. B A 1 → p 1 A 2 → p 2 A 1 A 3 → p 3 A 2 … An-1 → pn-1 An-2 S → pn. An-1 41

How we understand the algorithm S → A 1 p 1 A 1 → A 2 p 2 A 2 → A 3 p 3 … An-1 → pn If the left linear grammar contains S → Ap, then put this rule in the right linear grammar: A→p algorithm A 1 → p 1 A 2 → p 2 A 1 A 3 → p 3 A 2 … An-1 → pn-1 An-2 S → pn. An-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

- Right linear grammar
- Right linear grammar
- How to convert polar form to rectangular form
- Board feet to square feet
- Left left right right go turn around
- Stage right and stage left
- Go the building and turn left
- Muscle energy technique
- Left left right right go go go
- For top-down parsing left recursion removal is
- You put your right foot in
- Grammar vs linguistics
- Chomsky hierarchy
- Which grammar generates regular language
- Linear grammar
- Regular language examples
- Language r
- Simple and multiple linear regression
- Contoh soal metode biseksi dan penyelesaiannya
- Example of linear text
- Non linear plot diagram
- Contoh persamaan non linear
- Linear pipeline processors
- Linear multimedia
- Perbedaan fungsi linier dan non linier
- Apa itu fungsi non linier
- Conditions for linear independence
- Linear algebra 1
- Cara penggal lereng
- Linear impulse equation
- Persamaan linier simultan
- Linear vs nonlinear graphs
- Linear or non linear
- Difference between linear and nonlinear equation
- Nonlinear video editing
- Metode gauss seidel
- Texto narrativo sobre romance
- Write whether each
- How to convert from vertex form to standard form
- Rectangular to polar form
- Quotient property of logarithms examples
- Convert to trigonometric form -2i
- Convert to trigonometric form 5i 3
- Invers transformasi z
- Convert from mass to moles