Transcript powerpoint

Grammars
1
Grammars
Grammars express languages
Example:
the English language
sentence  noun _ phrase
noun _ phrase  article
predicate
noun
predicate  verb
2
article  a
article  the
noun  boy
noun  dog
verb  runs
verb  walks
3
A derivation of “the boy walks”:
sentence  noun _ phrase
predicate
 noun _ phrase
verb
 article
verb
 the noun
noun
verb
 the boy verb
 the boy walks
4
A derivation of “a dog runs”:
sentence  noun _ phrase
predicate
 noun _ phrase
verb
 article
noun
verb
 a noun
verb
 a dog verb
 a dog runs
5
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” }
6
Notation
noun  boy
noun  dog
Variable
or
Non-terminal
Production
rule
Terminal
7
Another Example
Grammar:
S  aSb
S 
Derivation of sentence ab :
S  aSb  ab
S  aSb
S 
8
Derivation of sentence aabb :
S  aSb  aaSbb  aabb
S  aSb
S 
9
Other derivations:
S  aSb  aaSbb  aaaSbbb  aaabbb
S  aSb  aaSbb  aaaSbbb
 aaaaSbbbb  aaaabbbb
10
Language of the grammar
S  aSb
S 


L  a b :n  0
n n
11
More Notation
Grammar
G  V , T , S , P 
V
Set of variables
T
Set of terminal symbols
S
Start variable
P
Set of Production rules
12
Example
Grammar G :
S  aSb
S 
G  V , T , S , P 
V  {S}
T  {a, b}
P  {S  aSb, S  }
13
More Notation
Sentential Form:
A sentence that contains
Variables and terminals
Example:
S  aSb  aaSbb  aaaSbbb  aaabbb
Sentential Forms
sentence
14
We write:
*
S  aaabbb
Instead of:
S  aSb  aaSbb  aaaSbbb  aaabbb
15
In general we write:
*
w1  wn
If:
w1  w2  w3    wn
16
By default:
*
w  w
17
Examples
Grammar: S  aSb
S 
*
S 
*
S  ab
*
S  aabb

S  aaSbb

aaSbb  aaaaaSbbbbb
*
S  aaabbb
18
Another Example
Grammar G:
S  Ab
A  aAb
A
Derivations:
S  Ab  b
S  Ab  aAbb  abb
S  aAbb  aaAbbb  aabbb
19
S  Ab  aAbb  aaAbbb  aaaAbbbb
 aaaaAbbbbb  aaaabbbbb

S  aaaabbbbb

S  aaaaaabbbbbbb

S a b b
n n
20
Language of a Grammar
For a grammar G
with start variable S

L(G )  {w : S  w}
21
Example
For grammar G:
S  Ab
A  aAb
A
L(G)  {a b b : n  0}
n n

Since: S  a b b
n n
22
A Convenient Notation
A  aAb
A
article  a
article  the
A  aAb | 
article  a | the
23
Linear Grammars
24
A Linear Grammar
Grammars with
at most one variable on the right side
of a production
Examples:
S  aSb
S 
S  Ab
A  aAb
A
25
A Non-Linear Grammar
Grammar G :
S  SS
S 
S  aSb
S  bSa
L(G )  {w : na ( w)  nb ( w)}
26
Another Linear Grammar
Grammar G :
SA
A  aB | 
B  Ab
L(G)  {a b : n  0}
n n
27
Right-Linear Grammars
All productions have the form:
A  xB
or
A x
Example:
S  abS
S a
28
Left-Linear Grammars
All productions have form:
A  Bx
or
A x
Example:
S  Aab
A  Aab | B
Ba
29
Regular Grammars
30
Regular Grammars
Definition:
A regular grammar
is any
right-linear or left-linear grammar
Examples:
S  abS
S a
S  Aab
A  Aab | B
Ba
31
Observation
Regular grammars generate
regular languages
Examples:
Grammar G:
S  abS
S a
L(G)  (ab) * a
Grammar G:
S  Aab
A  Aab | B
Ba
L(G)  aab(ab) *
32
Theorem
A Language L is regular
if and only if
there is a grammar G such that L  L(G)
33