You are on page 1of 26

Grammars

Grammars in Natural
Language
• English speakers kno
know
English grammar
intuitively
• They rarely think explicitly
about the structure of the
language they speak
• Onlyy when we learn other
languages to we start to
consider grammar
explicitly
English vs.
vs French
• The cat is on the mat
• Le chat est sur la mat

• The men watch the women


• Les hommes regarent les femmes

Th words
The d are different
diff tbbutt th t
the sentence t t
structure i similar
is i il
The men watch the women
<sentence> <determiner> <noun> <verb> <determiner> <noun>
<determiner> a
<determiner> the This grammar
<noun> girl allows us to
<noun> girls generate English
<noun> boy sentences
<noun> boys
<verb> watch terminals
<verb> watches
A parse tree can be used to show
the derivation of a sentence
The men watch the women

<Sentence>

<determiner> <noun> <verb> <determiner> <noun>

The man watch the women


Grammars are usually more
complex
• sentence noun
no n phrase + verb
erb phrase
• noun phrase noun
• noun phrase
h d t
determiner
i + noun
• verb phrase verb + noun phrase
• noun boy | man | car | bike | lamp | cake
• verb ate | drank | hit | saw
• determiner a | the
The man ate a cake
Sentence

noun phra verb pras

determine noun verb noun phra

the man ate determin noun

a cake
Grammars can be used to
define a language
• Grammar can specif
specify
the rules for a language
• First we need a formal
definition of a grammar
• We are interested in
specifying computer
languages
g g not natural
ones
A Grammar
• A grammar G is
i a quadruple
d l (T (T, N
N, P
P, S)
where
– N is the alphabet of symbols called
nonterminal symbols
– T is
i th
the alphabet
l h b t off symbols
b l called
ll d terminal
t i l
symbols
– P is a set of production rules
– S is the start symbol of G. S is an element
of N
Production Rules
• The
Th membersb off th
the sett off productions
d ti
take the form x y
• Where x is an element of (N U T)+
• And y is an element of ((N U T)* )
• By convention all terminal symbols are
denoted by small letters and
nonterminals by capital letters
Example 1: Expression
• GE = (T,
(T N N, PP, S) • P={
• T = {i, +, -, *, /, (, ) } E -> E + E
• N = {E} E ->
>E–E
E -> E * E
• S=E
E -> E / E
E -> ( E )
E -> i }
E
(a + b) * ( c / d)
E * E

(i+i)*(i/i)
( E ) ( E )

E + E E / E

i i i i
Derivation
• We can sho
show the • E
derivation by listing • E*E
the productions • (E)*E
• (E+E)*E
used step by step
• (i+E)*E
• (i+i)*(i/i) • (i+i) *E
• (i+i)*(E)
• (i+i)*(E/E)
• (i+i)*(i/E)
• (i+i)*(i/i)
Leftmost Derivation
• In a leftmost derivation
deri ation id + id + id + id + id
the leftmost non- E
terminal is replaced E+E
id + E
• There is only ever one id + E + E
leftmost derivation. It id + id + E
is unique id + id + E + E
id + id + id + E + E
id + id + id + id + E
id + id + id + id + id
E

L ft
Leftmost
t Derivation
D i ti
E + E

id E + E

id E + E

id E + E

id id
Rightmost Derivation
id + id + id + id + id
• In a rightmost E
E+E
derivation the
E + id
rightmost non
non-terminal
terminal
E + E + id
is replaced
E + id + id
• These is only ever one E + E + id + id
rightmost derivation. It E + id + id + id
is unique E + E + id + id + id
E + id + id + id + id
id + id + id + id +id
E
Rightmost Derivation
E + E

E + E id

E + E id

E + E id

id id
A Derivation
E

E + E

E + E E E

id id E + E id

id id
Example Grammar 3
• The
Th llanguage • Where
Wh P iis th
the sett
{aMbN | M≥0, N≥0} of productions {
i generated
is t db
by S -> AB
the grammar G2 A -> aA
where
h A -> €
G2 = ( {a,b}, B -> bB
{S,A,B}, P, S) B -> € }
Example Grammar 3
Derivation of aabbb
• S • S is the start ssymbol
mbol
• AB • Apply the production
S ->
> AB to transform
the S into AB
• AB • Apply the production
A -> aA to change A
into aA
• aAB • Apply the production
A -> aA to change g A
into aA :-)
Example Grammar 3
Derivation of aabbb
• aAB • Apply
Appl the production
prod ction
A -> aA
• aaAB
AB • Apply the production
A -> aA :-)
• aaAB • Apply the production
A -> € to get rid of
the A
• aaB • Apply the production
A -> € to g
get rid of
the A :-)
Example Grammar 3
Derivation of aabbb
• aaB • Apply
Appl the production
prod ction B ->
> bB
to transform the B into bB
• aabB
bB • Apply the production B ->> bB
to transform the B into bB :-)
• aabbB • Apply the production B -> bB
to transform the B into bB
• Apply the production B ->> bB
• aabbbB to transform the B into bB :-)
Example Grammar 3
Derivation of aabbb
• aabbbB • Apply the production B -
> € to get rid of the B
• aabbb derived
Example Grammar 3
Derivation of aabbb
• S • Apply
Appl the production
prod ction
• AB B -> € to get rid of
• aAB
AB the B :-)
:)
• aaB • aabbb derived
• aabB
• aabbB
• aabbb
S

S ->
> AB A B
A -> aA | €
B -> bB|€ a A b B

aabbb a € b B

b €
Shorthand
• From here on we will just
describe a grammar by listing
the set of productions The
start symbol will be the first
nonterminal on the left hand
side
id off th
the fi
firstt production
d ti
listed (usually S)

You might also like