2
GrammarsGrammars express languages
Example: the English language
verbpredicate
nounarticlephrasenoun
predicatephrasenounsentence
_
_
3
walksverb
runsverb
dognoun
boynoun
thearticle
aarticle
4
A derivation of “the boy walks”:
walksboythe
verbboythe
verbnounthe
verbnounarticle
verbphrasenoun
predicatephrasenounsentence
_
_
5
A derivation of “a dog runs”:
runsdoga
verbdoga
verbnouna
verbnounarticle
verbphrasenoun
predicatephrasenounsentence
_
_
6
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” }
7
Notation
dognoun
boynoun
Variable orNon-terminal
TerminalProductionrule
8
Another ExampleGrammar:
Derivation of sentence :
S
aSbS
abaSbS
ab
aSbS S
9
Derivation of sentence :aabb
aabbaaSbbaSbS
aSbS S
10
Other derivations:
aaabbbaaaSbbbaaSbbaSbS
aaaabbbbaaaaSbbbb
aaaSbbbaaSbbaSbS
11
Language of the grammar
S
aSbS
0: nbaL nn
12
More Notation
Grammar PSTVG ,,,
V
T
S
P
Set of variables
Set of terminal symbols
Start variable
Set of Production rules
13
ExampleGrammar :
S
aSbSG
PSTVG ,,,
}{SV
},{ baT
},{ SaSbSP
14
More NotationSentential Form: A sentence that contains Variables and terminals
Example:
aaabbbaaaSbbbaaSbbaSbS
Sentential Forms sentence
15
We write:
Instead of:
aaabbbS*
aaabbbaaaSbbbaaSbbaSbS
16
In general we write:
If:
nww*
1
nwwww 321
17
By default:
ww*
18
Examples
S
aSbS
aaabbbS
aabbS
abS
S
*
*
*
*
Grammar:
baaaaaSbbbbaaSbb
aaSbbS
19
Another ExampleGrammar G:
A
aAbA
AbS
Derivations:
aabbbaaAbbbaAbbS
abbaAbbAbS
bAbS
20
aaaabbbbbaaaaAbbbbb
aaaAbbbbaaAbbbaAbbAbS
bbaS
bbbaaaaaabbbbS
aaaabbbbbS
nn
21
Language of a Grammar
For a grammar with start variable
GS
}:{)( wSwGL
22
ExampleFor grammar G:
A
aAbA
AbS
}0:{)( nbbaGL nn
Since: bbaS nn
23
A Convenient Notation
A
aAbA|aAbA
thearticle
aarticle
theaarticle |
24
Linear Grammars
25
A Linear GrammarGrammars with at most one variable on the right sideof a production
Examples:
A
aAbA
AbS
S
aSbS
26
A Non-Linear Grammar
bSaS
aSbS
S
SSS
Grammar :G
)}()(:{)( wnwnwGL ba
27
Another Linear GrammarGrammar
AbB
aBA
AS
|
}0:{)( nbaGL nn
:G
28
Right-Linear GrammarsAll productions have the form:
Example:
xBA
xAor
aS
abSS
29
Left-Linear GrammarsAll productions have form:
Example:
BxA
xA
or
aB
BAabA
AabS
|
30
Regular Grammars
31
Regular GrammarsDefinition: A regular grammar is any right-linear or left-linear grammar
Examples:
aS
abSS
aB
BAabA
AabS
|
32
ObservationRegular grammars generate regular languages
Examples:
aS
abSS
Grammar G:
aabGL *)()(
Grammar G:
aB
BAabA
AabS
|
*)()( abaabGL
33
Theorem
A Language is regular if and only ifthere is a grammar such that
L
G )(GLL