Title: Grammars
1Grammars
2Grammars
- Grammars express languages
- Example the English language
3 4- A derivation of the dog walks
5- A derivation of a cat runs
6L a cat runs, a cat walks,
the cat runs, the cat walks,
a dog runs, a dog walks, the
dog runs, the dog walks
7Notation
Production Rules
Variable
Terminal
8Another Example
- Grammar
- Derivation of sentence
9- Grammar
- Derivation of sentence
10 11 12More Notation
Set of variables
Set of terminal symbols
Start variable
Set of Production rules
13Example
14More Notation
- Sentential Form
- A sentence that contains
- variables and terminals
- Example
Sentential Forms
sentence
15 16 17 18Example
Grammar
Derivations
19Example
Grammar
Derivations
20Another Grammar Example
Derivations
21More Derivations
22Language of a Grammar
- For a grammar
- with start variable
String of terminals
23Example
Since
24A Convenient Notation
25Linear Grammars
26Linear Grammars
- Grammars with
- at most one variable at the right side
- of a production
- Examples
-
27A Non-Linear Grammar
Grammar
Number of in string
28Another Linear Grammar
29Right-Linear Grammars
- All productions have form
- Example
or
string of terminals
30Left-Linear Grammars
- All productions have form
- Example
or
string of terminals
31Regular Grammars
32Regular Grammars
- A regular grammar is any
- right-linear or left-linear grammar
- Examples
33Observation
- Regular grammars generate regular languages
- Examples
34Regular Grammars GenerateRegular Languages
35Theorem
Languages Generated by Regular Grammars
Regular Languages
36Theorem - Part 1
Languages Generated by Regular Grammars
Regular Languages
Any regular grammar generates a regular language
37Theorem - Part 2
Languages Generated by Regular Grammars
Regular Languages
Any regular language is generated by a regular
grammar
38Proof Part 1
Languages Generated by Regular Grammars
Regular Languages
The language generated by any
regular grammar is regular
39The case of Right-Linear Grammars
- Let be a right-linear grammar
- We will prove is regular
- Proof idea We will construct NFA
- with
40Example
41- Construct NFA such that
- every state is a grammar variable
special final state
42- Add edges for each production
43 44 45 46 47 48NFA
Grammar
49In General
- A right-linear grammar
- has variables
- and productions
or
50- We construct the NFA such that
- each variable corresponds to a node
special final state
51- For each production
- we add transitions and intermediate nodes
52- For each production
- we add transitions and intermediate nodes
53- Resulting NFA looks like this
It holds that
54The case of Left-Linear Grammars
- Let be a left-linear grammar
- We will prove is regular
- Proof idea
- We will construct a right-linear
- grammar with
55 Since is left-linear grammar the
productions look like
56- Construct right-linear grammar
Left linear
Right linear
57Construct right-linear grammar
Left linear
Right linear
58- It is easy to see that
- Since is right-linear, we have
-
-
Regular Language
Regular Language
Regular Language
59Proof - Part 2
Languages Generated by Regular Grammars
Regular Languages
Any regular language is generated by
some regular grammar
60Any regular language is generated by
some regular grammar
Proof idea
Let be the NFA with .
Construct from
a regular grammar such that
61- Since is regular
- there is an NFA such that
Example
62- Convert to a right-linear grammar
63 64 65 66In General
For any transition
Add production
variable
terminal
variable
67For any final state
Add production
68- Since is right-linear grammar
-
- is also a regular grammar
- with