You are on page 1of 29

Chapter 2

Introduction to Grammars
Grammar and formal Languages
Definition of a Grammar
Sentence Derivation
Derivation from a grammar
Language generated by grammar
 Chomsky Classification of grammar
 Phrase Structure Grammar (PSG)
Grammar and Languages
English grammar tells us if a given combination of words is a valid sentence.
The syntax of a sentence concerns its form while the semantics concerns
its meaning.
e.g. the mouse wrote a poem
From a syntax point of view this is a valid sentence.
From a semantics point of view not so fast…perhaps in Disney land
Natural languages (English, French, Portguese, etc) have very complex rules of
syntax and not necessarily well-defined.
Cont…
Formal language – is specified by well-defined set of rules of syntax

We describe the sentences of a formal language using a grammar.

Two key questions:


1 - Is a combination of words a valid sentence in a formal language?
2 – How can we generate the valid sentences of a formal language?

Formal languages provide models for both natural languages and


programming languages.
Cont…
A formal grammar G is any compact, specific mathematical definition
of a language L.
As opposed to just a raw listing of all of the language’s legal sentences, or just
examples of them.
A grammar implies an algorithm that would generate all legal
sentences of the language.
Often, it takes the form of a set of recursive definitions.
A popular way to specify a grammar recursively is to specify it as a
phrase-structure grammar.
Definition of a Grammar
Grammar mainly consists of set of non terminals N, set of terminals T,
set of productions rules P and start symbol S.
•A Grammar is a 4 tuples G=(N,T,P,S)
where N is a finite set of non terminals.
T is a finite set of terminals.
P is a finite set of Productions rules.
S  N is the start symbols.
 
• Here you must realize that N  T = Ø. Non Terminals and Terminal are
disjoint. Above things is the definition of the grammar N.Chomsky
rule. P is Something like left hand side rewritten as right hand side.
Basic Definitions and Examples grammar

Take a simple sentence first.


The man ate the fruit. How can be sentence make parse(describe).
Starting with the sentence S, this can be dividing as Noun parse one NP1 and verb
parse VP, then VP is dividing into V and Noun Parse two NP2, Noun Parse one NP1 is
split into article and Noun one N1, article is the and Noun is man, verb is ate, Noun
parse two NP2 is split article and Noun two N2, article is the and Noun two is fruit. So
this is the parse tree for this sentence.
Cont…
There is a left hand side and right hand side. Left hand side is rewritten as
right hand side. So this single arrow read as rewritten as and double arrow
read as directly derives.
So <S> directly derives <NP1> and <VP1>, <NP1> directly derives <art>
and <N1>, so we can get something from something else is called sentential
form.
Noun describe one rewritten as <art> and <N1>. So this sentential form
directly derives this sentence. Next we have to use the single rule. Article
can be rewritten as the, noun one rewritten as man and verb parse remain as
it is as.
Note that only one rule at one stage. In the next step verb parse rewritten
as verb and noun parse two, verb rewritten ate and noun parse two remain as
it is as. In the next step noun parse two is rewritten article and noun two.
Article is rewritten as the and noun two rewritten as fruit.
Cont…
The man ate the fruit
<S>  <NP1> <VP>
 <Art> <N1> <VP>
 The <N1> <VP>
 The man <VP>
 The man <V> <NP2>
 The man ate <NP2>
 The man ate <art> <N2>
 The man ate the <N2>
S  The man ate the fruit.
This is called derivation. This double arrow is really a relationship between
sentential form. So if you have sentential form alpha you derive beta from
that, what does that mean, you get beta from alpha α  β by the
application of the single rule.
Cont…
So it’s represents a relationship between sentential forms.
Sentential form actually strings over non terminal and terminals. See here, you
know that there is difference between these words (the man ate the fruit) and
syntax categories like Art, NP1,VP, NP2, N1and N2.
These noun or Verbs or verb parse is rewritten as something else. There are
called non terminals.
Whereas once you get the leaves of the tree like the, man, ate and fruit. They
cannot be rewritten as so these are called Terminals.
Cont…
We will see another example The peacock is a beautiful bird.

<S>  <NP1> <VP>


<NP1>  <Art1> <N1>
<Art1>  The
<N1>  Peacock
<VP>  <V> <Art2> <Adj><N2>
<V>  is
<Art2>  a
<Adj>  Beautiful
<N2>  Bird.
So this single arrow can be rewritten
as
and double arrow directly derives.
Cont…
S  <NP1> <VP>
 <NP1><V> <ART2> <ADJ> <N2>
 <NP1><V> a <ADJ><N2>
 <NP1><V> a beautiful <N2>
 <ART1> <N1><V> a beautiful <N2>
 The <N1><V> a beautiful <N2>
 The <N1> is a beautiful <N2>
 The Peacock is a Beautiful <N2>
 The Peacock is a Beautiful Bird.
We will consider one more example venice is a beautiful city.
A Sample Sentence Derivation
(sentence)
(noun phrase) (verb phrase)
(article) (adj.) (noun) (verb phrase)
(art.) (adj.) (noun) (verb) (adverb)
On each step,
the (adj.) (noun) (verb) (adverb)
we apply a production
the large (noun) (verb) (adverb)
to a fragment of the
the large rabbit (verb) (adverb) previous sentence
the large rabbit hops (adverb) template to get a new
the large rabbit hops quickly sentence template.
Finally, we end up with
a sequence of
terminals (real words),
that is, a sentence of
our language L.
Another Example
Cont…
Derivation from a grammar
Cont…
d ! !
E n

Que s t io n ? ?

You might also like