Sunteți pe pagina 1din 24

CONTEXT-FREE GRAMMARS

Syntactic analysis (Parsing)


S NP VP

AT

NNS

VBD

NP

the

children

ate

AT

NN

the

cake

NLE

Beyond regular languages: Context-Free Grammars

S NP VP NP Det Nominal Nominal Noun VP V

Det the Det a Noun flight V left

NLE

Derivations

A DERIVATION of a string is a sequence of rule applications

E.g., the string a flight can be derived from the grammar above and symbol NP by the (leftmost first) derivation

NP => Det Nominal => a Nominal => a Noun => a flight

Derivations can be visualized as PARSE TREES The LANGUAGE defined by a CFG is the set of strings derivable from the start symbol S (for Sentence)

NLE

Derivations and parse trees

NLE

A more formal definition

A CFG is a 4-tuple <N,,P, S> consisting of

NLE

What `context free means

NLE

Derivations and languages

The language LG GENERATED by a CFG grammar G is the set of strings of TERMINAL symbols that can be derived from the start symbol S using the production rules in G

LG = {w | w is in * and S derives w}

The strings in LG are called GRAMMATICAL The strings not in LG are called UNGRAMMATICAL
NLE

Grammar development

One of the most basic skills in NLE is the ability to write a CFG for some fragment of a language (e.g., the dates) Well briefly cover some of the issues to be addressed when writing small CFG grammars

10

NLE

An example lexicon

11

NLE

An example grammar

12

NLE

A simple parse tree

13

NLE

Basic types of phrases


Sentences Noun Phrases Verb phrases Prepositional phrases

14

NLE

Basic types of sentences

15

NLE

Noun phases: premodifiers


NP (Det) (Card) (Ord) (Quant) (AP) Nominal Det: Determiners

a flight Optional: Im looking for flights to Denver

Card: Cardinal numbers (one stop) Ord: Ordinal numbers (the first flight) Quantifiers: most flights to Denver leave in the morning AP (Adjectives): three very expensive seats

16

NLE

Noun phases: postmodifiers


Nominal Noun Nominal Nominal PP (PP) (PP) Nominal Nominal GerundVP Nominal Nominal RelClause

17

NLE

Types of postnominal modifiers

18

NLE

Recursion

Nominal Nominal PP (PP) (PP)

Is an example of RECURSIVE rule NP NP PP VP VP PP

Other examples:

Recursion a powerful device, but could have bad consequences (see lectures on parsing)

19

NLE

Recursion and VP attachment

20

NLE

Coordination

NP NP and NP

John and Mary left

VP VP and VP

John talks softly and carries a big stick


Kim is a lawyer but Sandy is reading medicine.

S S and / but / S

In fact, probably English has a


XP XP and XP rule

21

NLE

Agreement

This dog Those dogs *This dogs *Those dogs This dog is smart *This dog are smart *Those dogs is smart
NLE

22

CFGs vs Regular languages


For many applications, finite state languages (the languages defined by FA) are appropriate Limitation of FAs: cannot count

I.e., cannot check A n B n

Example of construction showing that English is CF: long-distance dependencies

Which film did Kim say the director who we just met _ recommended _?
NLE

24

The Chomsky Hierarchy


Finite-state languages (type 3)

A bC | Cb (a single NT on the right)

Context-free languages (type 2)

A BB
CAC BB Every language that can be specified by a finite algorithm

Context-sensitive languages (type 1)

Recursively enumerable languages

25

NLE

Readings

Jurafsky and Martin, chapter 9 The chapters on context-free languages in

The Free Dictionary: http://encyclopedia.thefreedictionary.com/Contextfree%20language Wikipedia: http://en.wikipedia.org/wiki/Context-free_grammar

26

NLE

S-ar putea să vă placă și