Grammars

Grammars in Natural Language
• English speakers kno know English grammar intuitively • They rarely think explicitly about the structure of the language they speak • Only when we learn other y languages to we start to consider grammar explicitly

English vs. French vs
• The cat is on the mat • Le chat est sur la mat • The men watch the women • Les hommes regarent les femmes
Th words are different b t th sentence structure is similar d diff t but the t t t i i il The

The men watch the women
<sentence> <determiner> <noun> <verb> <determiner> <noun> <determiner> a This grammar <determiner> the <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> The man

<verb> watch

<determiner> the

<noun> women

Grammars are usually more complex
• • • • • • • noun sentence no n phrase + verb phrase erb noun phrase noun noun phrase h determiner + noun d t i 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 determine the noun man verb ate verb pras noun phra determin a noun 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 not natural g g ones

A Grammar
• A grammar G is a quadruple (T N P S) i d l (T, N, P, where
– N is the alphabet of symbols called nonterminal symbols – T i th alphabet of symbols called t is the l h b t f b l ll d terminal 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
• Th members of th set of productions The b f the t f 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
• • • • (T, N, P, GE = (T N P S) T = {i, +, -, *, /, (, ) } N = {E} S=E • P={
E -> E + E E -> E – E > E -> E * E E -> E / E E -> ( E ) E -> i }

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

(a + b) * ( c / d) (i+i)*(i/i)

Derivation
• We can sho the show derivation by listing the productions used step by step • (i+i)*(i/i)
• • • • • • • • • • E E*E (E)*E (E+E)*E (i+E)*E (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 the leftmost nonterminal is replaced • There is only ever one leftmost derivation. It is unique
id + id + id + id + id E E+E id + E id + E + E id + id + E id + id + E + E id + id + id + E + E id + id + id + id + E id + id + id + id + id

E E id + E id E + E id E + E id E + E id

Leftmost Derivation L ft t D i ti

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

Rightmost Derivation
E E E E id + + E id + E id

E + E id E id

A Derivation
E E + E id + E id E E + E id id E E id

Example Grammar 3
• Th l The language • Wh Where P i th set is the t {aMbN | M≥0, N≥0} of productions { is i generated b t d 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 • AB • S is the start s mbol symbol • Apply the production S -> AB to transform > the S into AB • Apply the production A -> aA to change A into aA • Apply the production A -> aA to change A g into aA :-)

• AB • aAB

Example Grammar 3 Derivation of aabbb
• aAB • aaAB AB • aaAB • aaB • Appl the production Apply prod ction A -> aA • Apply the production A -> aA :-) • Apply the production A -> € to get rid of the A • Apply the production A -> € to get rid of g the A :-)

Example Grammar 3 Derivation of aabbb
• aaB • aabB bB • aabbB • aabbbB • Appl the production B -> bB Apply prod ction > to transform the B into bB • Apply the production B -> bB > to transform the B into bB :-) • Apply the production B -> bB to transform the B into bB • Apply the production B -> bB > 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 AB aAB AB aaB aabB aabbB aabbb • Appl the production Apply prod ction B -> € to get rid of the B :-) :) • aabbb derived

S

> S -> AB A -> aA | € B -> bB|€ aabbb

A a a A € b

B B 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 of th fi t production id f the first d ti listed (usually S)