820 likes | 832 Views
Learn about context-free grammars, derivation trees, ambiguity, and how context-free grammars describe natural languages. Understand the notation and language of context-free grammars.
E N D
Context-Free Languages Hinrich Schütze CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch
Take-away • Definition context-free grammar • Definition context-free language • Derivation, sentential form, sentence • Derivation trees • Ambiguity • Context-free grammars for natural language
Terminology • For our purposes in this class: • Context free grammar • = • Constituency grammar • = • Phrase structure grammar
Grammars • Grammars express languages • Example: the English language
Language of the grammar: L = { “a cat runs”, “a cat walks”, “the cat runs”, “the cat walks”, “a dog runs”, “a dog walks”, “the dog runs”, “the dog walks” }
Notation Production Rules Variable Terminal
Another Example • Grammar: • Derivation of sentence :
Grammar: • Derivation of sentence :
More Notation • Grammar Set of variables Set of terminal symbols Start variable Set of Production rules
Example • Grammar :
More Notation • Sentential Form: • A sentence that contains • variables and terminals • Example: Sentential Forms sentence
We write: • Instead of:
In general we write: • If:
Example Grammar Derivations
Example Grammar Derivations
Another Grammar Example • Grammar :
Grammar : Derivations:
Language of a Grammar • For a grammar • with start variable : String of terminals
Example • For grammar : Since:
Revisit first grammar A context-free grammar : A derivation:
A context-free grammar : Another derivation:
(((( )))) Describes parentheses:
Example A context-free grammar :
A context-free grammar : Another derivation:
Example A context-free grammar :
A context-free grammar : Two derivations:
Describes matched parentheses: () ((( ))) (( ))
Definition: Context-Free Grammars Grammar Variables Terminal symbols Start variable Productions of the form: Variable String of variables and terminals
Definition: Context-Free Languages • A language is context-free • if and only if • there is a context-freegrammar • with
Leftmost derivation: Rightmost derivation: Derivation Order
Leftmost derivation: Rightmost derivation: