1 / 78

Why do we care about regular languages?

Why do we care about regular languages?. Programs are composed of tokens: Identifier Number Keyword Special symbols Each of these can be defined by regular grammars . Problem: How do we handle multiple symbol operators (e.g., ++ in C, =+ in C, := in Pascal)? ?? -multiple final states?.

Download Presentation

Why do we care about regular languages?

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. Why do we care about regular languages? • Programs are composed of tokens: • Identifier • Number • Keyword • Special symbols • Each of these can be defined by regular grammars. • Problem: How do we handle multiple symbol operators (e.g., ++ in C, =+ in C, := in Pascal)? • ?? -multiple final states?

  2. Sample token classes

  3. Scanner for a language turns out to be a giant NFA for grammar.(i.e., have -rules going from start state to the start state of each token-type on previous slide). integer   identifier  keyword  symbol

  4. Grammars • Grammar: generative description of a language • Automaton: analytical description • Example: programming languages are defined by a grammar (BNF),but recognized with an analytical description (the parser of a compiler), • Language theory establishes links between analytical and generative language descriptions.

  5. Grammars • Grammars express languages • Example: the English language

  6. A derivation of “the boy walks”:

  7. A derivation of “a dog runs”:

  8. Language of the grammar: L = { “a boy runs”, “a boy walks”, “the boy runs”, “the boy walks”, “a dog runs”, “a dog walks”, “the dog runs”, “the dog walks” }

  9. Notation Variable or Non-terminal Terminal Production rule

  10. Another Example • Grammar: • Derivation of sentence :

  11. Grammar: • Derivation of sentence :

  12. Other derivations:

  13. Language of the grammar

  14. More Notation • Grammar Set of variables Set of terminal symbols Start variable Set of Production rules

  15. Example • Grammar :

  16. More Notation • Sentential Form: • A sentence that contains • variables and terminals • Example: Sentential Forms sentence

  17. We write: • Instead of:

  18. In general we write: • If:

  19. By default:

  20. Example Grammar Derivations

  21. Example Grammar Derivations

  22. Another Grammar Example • Grammar : Derivations:

  23. More Derivations

  24. Language of a Grammar • For a grammar • with start variable : String of terminals

  25. Example • For grammar : Since:

  26. A Convenient Notation

  27. Linear Grammars

  28. Linear Grammars • Grammars with • at most one variable at the right side • of a production • Examples:

  29. A Non-Linear Grammar Grammar :

  30. Another Linear Grammar • Grammar :

  31. Right-Linear Grammars • All productions have form: • Example: or

  32. Left-Linear Grammars • All productions have form: • Example: or

  33. Regular Grammars

  34. Regular Grammars • A regular grammaris any • right-linear or left-linear grammar • Examples:

  35. Observation • Regular grammars generate regular languages • Examples:

  36. Regular Grammars GenerateRegular Languages

  37. Theorem Languages Generated by Regular Grammars Regular Languages

  38. Theorem - Part 1 Languages Generated by Regular Grammars Regular Languages Any regular grammar generates a regular language

  39. Theorem - Part 2 Languages Generated by Regular Grammars Regular Languages Any regular language is generated by a regular grammar

  40. Proof – Part 1 Languages Generated by Regular Grammars Regular Languages The language generated by any regular grammar is regular

  41. The case of Right-Linear Grammars • Let be a right-linear grammar • We will prove: is regular • Proof idea: We will construct NFA • with

  42. Grammar is right-linear Example:

  43. Construct NFA such that • every state is a grammar variable: special final state

  44. Add edges for each production:

More Related