Download presentation
Presentation is loading. Please wait.
Published byRaymond Stanley Modified over 8 years ago
1
Regular Grammars Chapter 7 1
2
Regular Grammars A regular grammar G is a quadruple (V, , R, S), where: ● V is the rule alphabet, which contains nonterminals and terminals, ● (the set of terminals) is a subset of V, ● R (the set of rules) is a finite set of rules of the form: X Y, X Y, ● S (the start symbol) is a nonterminal. 2
3
Regular Grammars In a regular grammar, all rules in R must: ● have a left hand side that is a single nonterminal ● have a right hand side that is: ● , or ● a single terminal, or ● a single terminal followed by a single nonterminal. Legal: S a, S , and T a S Not legal: S a S a and a S a T 3
4
L = {w { a, b }* : |w| is even} Regular expression: (( aa ) ( ab ) ( ba ) ( bb ))* FSM: Regular Grammar Example 4
5
L = {w { a, b }* : |w| is even} Regular expression: (( aa ) ( ab ) ( ba ) ( bb ))* FSM: Regular grammar:G = (V, , R, S), where V = {S, T, a, b}, = {a, b}, R = { S S a T S b T T a S T b S } Regular Grammar Example 5
6
Regular Languages and Regular Grammars Theorem: The class of languages that can be defined with regular grammars is exactly the regular languages. Proof: By two constructions. 6
7
Regular Languages and Regular Grammars Regular grammar FSM: grammartofsm(G = (V, , R, S)) = 1. Create in M a separate state for each nonterminal in V. 2. Start state is the state corresponding to S. 3. If there are any rules in R of the form X w, for some w , create a new state labeled #. 4. For each rule of the form X w Y, add a transition from X to Y labeled w. 5. For each rule of the form X w, add a transition from X to # labeled w. 6. For each rule of the form X , mark state X as accepting. 7. Mark state # as accepting. FSM Regular grammar: Similarly. 7
8
Example 1 - Even Length Strings S S S aTS aT S bTS bT T aST aS T bST bS 8
9
S S S aTS aT S bTS bT T aST aS T bST bS 9
10
Strings that End with aaaa L = {w { a, b }* : w ends with the pattern aaaa }. S aSS aS S bSS bS S aBS aB B aCB aC C aDC aD D aD a 10
11
Strings that End with aaaa L = {w { a, b }* : w ends with the pattern aaaa }. S aSS aS S bSS bS S aBS aB B aCB aC C aDC aD D aD a 11
12
Example 2 – One Character Missing S A bAC aCS A bAC aC S aBA cAC bCS aBA cAC bC S aC A C S aC A C S bA B aBS bA B aB S bC B cBS bC B cB S cA B S cA B S cBS cB 12
13
Example 2 – One Character Missing S A bAC aCS A bAC aC S aBA cAC bCS aBA cAC bC S aC A C S aC A C S bA B aBS bA B aB S bC B cBS bC B cB S cA B S cA B S cBS cB 13
14
Regular Languages and Regular Grammars FSM Regular grammar: Show by construction that, for every FSM M there exists a regular grammar G such that L(G) = L(M). 1.Make M deterministic M’ = (K, , , s, A). Construct G = (V, , R, S) from M’. 2.Create a nonterminal for each state in the M’. V = K . 3.The start state becomes the starting nonterminal. S = s. 4.For each transition (T, a) = U, make a rule of the form T aU. 5.For each accepting state T, make a rule of the form T . 14
15
Strings that End with aaaa L = {w { a, b }* : w ends with the pattern aaaa }. 15
16
Strings that End with aaaa L = {w { a, b }* : w ends with the pattern aaaa }. S aSS aS S bSS bS S aBS aB B aCB aC C aDC aD D aD a 16
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.