How to convert a left linear grammar to a right linear grammar

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

Repaso: Unidad 1 Lección 2
1 A B C
Simplifications of Context-Free Grammars
Variations of the Turing Machine
AP STUDY SESSION 2.
1
& dding ubtracting ractions.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
Objectives: Generate and describe sequences. Vocabulary:
UNITED NATIONS Shipment Details Report – January 2006.
By John E. Hopcroft, Rajeev Motwani and Jeffrey D. Ullman
David Burdett May 11, 2004 Package Binding for WS CDL.
We need a common denominator to add these fractions.
1 RA I Sub-Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Casablanca, Morocco, 20 – 22 December 2005 Status of observing programmes in RA I.
Properties of Real Numbers CommutativeAssociativeDistributive Identity + × Inverse + ×
Custom Statutory Programs Chapter 3. Customary Statutory Programs and Titles 3-2 Objectives Add Local Statutory Programs Create Customer Application For.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt BlendsDigraphsShort.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
1 Click here to End Presentation Software: Installation and Updates Internet Download CD release NACIS Updates.
CSCI 3130: Formal Languages and Automata Theory Tutorial 5
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
Break Time Remaining 10:00.
Turing Machines.
Table 12.1: Cash Flows to a Cash and Carry Trading Strategy.
PP Test Review Sections 6-1 to 6-6
1 The Blue Café by Chris Rea My world is miles of endless roads.
Bright Futures Guidelines Priorities and Screening Tables
EIS Bridge Tool and Staging Tables September 1, 2009 Instructor: Way Poteat Slide: 1.
Bellwork Do the following problem on a ½ sheet of paper and turn in.
CS 6143 COMPUTER ARCHITECTURE II SPRING 2014 ACM Principles and Practice of Parallel Programming, PPoPP, 2006 Panel Presentations Parallel Processing is.
Exarte Bezoek aan de Mediacampus Bachelor in de grafische en digitale media April 2014.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
1 RA III - Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Buenos Aires, Argentina, 25 – 27 October 2006 Status of observing programmes in RA.
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
CONTROL VISION Set-up. Step 1 Step 2 Step 3 Step 5 Step 4.
© 2012 National Heart Foundation of Australia. Slide 2.
Adding Up In Chunks.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
Note to the teacher: Was 28. A. to B. you C. said D. on Note to the teacher: Make this slide correct answer be C and sound to be “said”. to said you on.
25 seconds left…...
Subtraction: Adding UP
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Formal Language, chapter 10, slide 1Copyright © 2007 by Adam Webber Chapter Ten: Grammars.
Analyzing Genes and Genomes
1 Let’s Recapitulate. 2 Regular Languages DFAs NFAs Regular Expressions Regular Grammars.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Essential Cell Biology
Converting a Fraction to %
Clock will move after 1 minute
Intracellular Compartments and Transport
PSSA Preparation.
Chapter Eleven: Non-Regular Languages
Essential Cell Biology
Immunobiology: The Immune System in Health & Disease Sixth Edition
Physics for Scientists & Engineers, 3rd Edition
Energy Generation in Mitochondria and Chlorplasts
Select a time to count down from the clock above
Murach’s OS/390 and z/OS JCLChapter 16, Slide 1 © 2002, Mike Murach & Associates, Inc.
Completing the Square Topic
9. Two Functions of Two Random Variables
How to create Magic Squares
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
The Pumping Lemma for CFL’s
Presentation transcript:

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: What is a linear grammar? What is a left linear grammar? What is a right linear grammar?

Objective This mini-tutorial will answer these questions: What is a linear grammar? What is a left linear grammar? What is a right linear grammar? Left linear grammars are evil – why?

Objective This mini-tutorial will answer these questions: What is a linear grammar? What is a left linear grammar? What is a right linear grammar? Left linear grammars are evil – why? What algorithm can be used to convert a left linear grammar to a right linear grammar?

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 → aA A → aBb B → Bb

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

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 → ε

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.

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.

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 → abaA A → ε Both grammars generate this language: {aba}

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: S0 → S

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

Algorithm If the left linear grammar has a rule S → p, then make that a rule in the right linear grammar If the left linear grammar has a rule A → p, then add the following rule to the right linear grammar: S → pA If the left linear grammar has a rule B → Ap, add the following rule to the right linear grammar: A → pB If the left linear grammar has a rule S → Ap, then add the following rule to the right linear grammar: A → p

Convert this left linear grammar S → Aa A → ab

Right hand side has terminals left linear right linear S → Aa A → ab S → abA 2) If the left linear grammar has this rule A → p, then add the following rule to the right linear grammar: S → pA

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

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

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

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

Right hand side has non-terminal left linear right linear S0 → S S → Ab S → Sb A → Aa A → a S0 → aA A → bS A → aA S → bS 3) If the left linear grammar has a rule B → Ap, add the following rule to the right linear grammar: A → pB

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

Equivalent! S0 → S S0 → aA S → Ab A → bS S → Sb A → aA A → Aa S → bS left linear right linear S0 → S S → Ab S → Sb A → Aa A → a S0 → aA A → bS A → aA S → bS S → ε Both grammars generate this language: {a+b+}

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.

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.

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: 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 → pA If the left linear grammar contains B → Ap, then put this rule in the right linear grammar: A → pB If the left linear grammar contains S → Ap, then put this rule in the right linear grammar: A → p

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)

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

Algorithm inputs and outputs left linear rules algorithm right linear rules

Case 2 (continued) S → A1p1 → A2p2p1 → A3p3p2p1 → … → An-1pn-1…p3p2p1 → pnpn-1…p3p2p1 S → A1p1 algorithm A1 → p1 (see 4 below) Algorithm: 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 → pA If the left linear grammar contains B → Ap, then put this rule in the right linear grammar: A → pB If the left linear grammar contains S → Ap, then put this rule in the right linear grammar: A → p

Case 2 (continued) S → A1p1 → A2p2p1 → A3p3p2p1 → … → An-1pn-1…p3p2p1 → pnpn-1…p3p2p1 A1 → A2p2 algorithm A2 → p2A1 (see 3 below) Algorithm: 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 → pA If the left linear grammar contains B → Ap, then put this rule in the right linear grammar: A → pB If the left linear grammar contains S → Ap, then put this rule in the right linear grammar: A → p

Case 2 (continued) S → A1p1 A1 → A2p2 S → A1p1 → A2p2p1 → A3p3p2p1 → … → An-1pn-1…p3p2p1 → pnpn-1…p3p2p1 algorithm A1 → p1 A2 → p2A1

Case 2 (continued) S → A1p1 A1 → A2p2 algorithm A1 → p1 From A2 we obtain p2p1: A2 → p2A1 → p2p1

Case 2 (continued) S → A1p1 → A2p2p1 → A3p3p2p1 → … → An-1pn-1…p3p2p1 → pnpn-1…p3p2p1 A2 → A3p3 algorithm A3 → p3A2 (see 3 below) Algorithm: 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 → pA If the left linear grammar contains B → Ap, then put this rule in the right linear grammar: A → pB If the left linear grammar contains S → Ap, then put this rule in the right linear grammar: A → p

Case 2 (continued) S → A1p1 A1 → A2p2 A2 → A3p3 S → A1p1 → A2p2p1 → A3p3p2p1 → … → An-1pn-1…p3p2p1 → pnpn-1…p3p2p1 algorithm A1 → p1 A2 → p2A1 A3 → p3A2

Case 2 (continued) S → A1p1 A1 → A2p2 A2 → A3p3 algorithm A1 → p1 From A3 we obtain p3p2p1: A3 → p3A2 → p3p2A1 → p3p2p1

Case 2 (continued) S → A1p1 → A2p2p1 → A3p3p2p1 → … → An-1pn-1…p3p2p1 → pnpn-1…p3p2p1 An-1 → pn algorithm S → pnAn-1 (see 2 below) Algorithm: 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 → pA If the left linear grammar contains B → Ap, then put this rule in the right linear grammar: A → pB If the left linear grammar contains S → Ap, then put this rule in the right linear grammar: A → p

Case 2 (concluded) S → A1p1 A1 → A2p2 A2 → A3p3 … An-1 → pn algorithm An-1 → pn-1An-2 S → pnAn-1 From S we obtain pn…p2p1: S → pnAn-1 → pnpn-1An-2 → … → pnpn-1…p3A2 → pnpn-1…p3p2A1 → pn…p3pn-1p2p1 (this is the desired string, p)

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.

How we understand the algorithm S → A1p1 A1 → A2p2 A2 → A3p3 … 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 A1 → p1 A2 → p2A1 A3 → p3A2 … An-1 → pn-1An-2 S → pnAn-1 Make the rule with terminals on the RHS the starting rule and output its terminals. Ascend through the other rules.

How we understand the algorithm S → A1p1 A1 → A2p2 A2 → A3p3 … An-1 → pn algorithm If the left linear grammar contains A → p, then put this rule in the right linear grammar: S → pA A1 → p1 A2 → p2A1 A3 → p3A2 … An-1 → pn-1An-2 S → pnAn-1

How we understand the algorithm S → A1p1 A1 → A2p2 A2 → A3p3 … An-1 → pn algorithm If the left linear grammar contains B → Ap, then put this rule in the right linear grammar: A → pB A1 → p1 A2 → p2A1 A3 → p3A2 … An-1 → pn-1An-2 S → pnAn-1

How we understand the algorithm S → A1p1 A1 → A2p2 A2 → A3p3 … An-1 → pn If the left linear grammar contains S → Ap, then put this rule in the right linear grammar: A → p algorithm A1 → p1 A2 → p2A1 A3 → p3A2 … An-1 → pn-1An-2 S → pnAn-1

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.

Acknowledgement The algorithm shown in these slides comes from the wonderful book: Introduction to Formal Languages by Gyorgy Revesz