150 likes | 301 Views
Regular Grammars. Reading: 3.3. What we know so far…. FSA = Regular Language Regular Expression describes a Regular Language Every Regular Language has a Regular Expression Now, what about grammars?. Right Linear Grammars.
E N D
Regular Grammars Reading: 3.3
What we know so far… • FSA = Regular Language • Regular Expression describes a Regular Language • Every Regular Language has a Regular Expression • Now, what about grammars?
Right Linear Grammars • Right-linear: at most one variable on the right side of the production, must be the right-most symbol. A -> xBA -> x x is any string of terminals.
Left Linear Grammars • Left-linear: at most one variable on the right side of the production, must be the left-most symbol. A -> BxA -> x x is any string of terminals.
Regular Grammars • A grammar is regular if it is either left- or right-linear. • A linear grammar may have a mix of left and right productions. Its only restriction is that there is at most one symbol on the right. • All regular grammars are linear, but not all linear grammars are regular!
Prove: All right-linear grammars describe regular languages • Idea: Find a way to convert any right-linear grammar into a FSA. • Terminals before a non-terminal represent arcs • Non-terminals represent non-final states. • Terminals without non-terminals following represent final states.
Reg Grammar to NFA Step 1: Start variable is the initial node. S -> aD D -> abS D -> b S
Reg Grammar to NFA Step 2: Each rule ending in a non-terminal is a chain of arcs followed by a non-final node. S -> aD D -> abS D -> b a D S
Reg Grammar to NFA Step 2: Each rule ending in a non-terminal is a chain of arcs followed by a non-final node. S -> aD D -> abS D -> b a D S b a
Reg Grammar to NFA Step 3: Each rule ending in a terminal leads to a final state. S -> aD D -> abS D -> b b a D S b a
Example: Convert to a FSA • S -> aBS -> bSS -> λB -> aSB -> bB
Does every regular language have a regular grammar that describes it? • Yes! Reverse the algorithm for constructing the FSA from the grammar • Every rule in δis a line in the grammar.
Regular Language Wrap-up Regular expressions DFA or NFA Regular grammar
Examples: • Find the grammar for the expression: aab*a • Find the regular expression for: S -> abS | bA A -> aA | b