780 likes | 797 Views
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?.
E N D
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?
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
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.
Grammars • Grammars express languages • Example: the English language
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” }
Notation Variable or Non-terminal Terminal Production rule
Another Example • Grammar: • Derivation of sentence :
Grammar: • Derivation of sentence :
More Notation • Grammar Set of variables Set of terminal symbols Start variable Set of Production rules
Example • Grammar :
More Notation • Sentential Form: • A sentence that contains • variables and terminals • Example: Sentential Forms sentence
We write: • Instead of:
In general we write: • If:
Example Grammar Derivations
Example Grammar Derivations
Another Grammar Example • Grammar : Derivations:
Language of a Grammar • For a grammar • with start variable : String of terminals
Example • For grammar : Since:
Linear Grammars • Grammars with • at most one variable at the right side • of a production • Examples:
A Non-Linear Grammar Grammar :
Another Linear Grammar • Grammar :
Right-Linear Grammars • All productions have form: • Example: or
Left-Linear Grammars • All productions have form: • Example: or
Regular Grammars • A regular grammaris any • right-linear or left-linear grammar • Examples:
Observation • Regular grammars generate regular languages • Examples:
Theorem Languages Generated by Regular Grammars Regular Languages
Theorem - Part 1 Languages Generated by Regular Grammars Regular Languages Any regular grammar generates a regular language
Theorem - Part 2 Languages Generated by Regular Grammars Regular Languages Any regular language is generated by a regular grammar
Proof – Part 1 Languages Generated by Regular Grammars Regular Languages The language generated by any regular grammar is regular
The case of Right-Linear Grammars • Let be a right-linear grammar • We will prove: is regular • Proof idea: We will construct NFA • with
Grammar is right-linear Example:
Construct NFA such that • every state is a grammar variable: special final state