1 / 15

Regular Grammars

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.

Download Presentation

Regular Grammars

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Regular Grammars Reading: 3.3

  2. 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?

  3. 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.

  4. 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.

  5. 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!

  6. 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.

  7. Reg Grammar to NFA Step 1: Start variable is the initial node. S -> aD D -> abS D -> b S

  8. 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

  9. 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

  10. 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

  11. Example: Convert to a FSA • S -> aBS -> bSS -> λB -> aSB -> bB

  12. 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.

  13. Write a regular grammar

  14. Regular Language Wrap-up Regular expressions DFA or NFA Regular grammar

  15. Examples: • Find the grammar for the expression: aab*a • Find the regular expression for: S -> abS | bA A -> aA | b

More Related