Applied Computer Science II Chapter 2 Contextfree languages

  • Slides: 48
Download presentation
Applied Computer Science II Chapter 2 : Context-free languages Prof. Dr. Luc De Raedt

Applied Computer Science II Chapter 2 : Context-free languages Prof. Dr. Luc De Raedt Institut für Informatik Albert-Ludwigs Universität Freiburg Germany

Overview • • Context free grammars Pushdown Automata Equivalence of PDAs and CFGs Non-context

Overview • • Context free grammars Pushdown Automata Equivalence of PDAs and CFGs Non-context free grammars – Pumping lemma

Context free languages • Extend regular languages • First studied for natural languages •

Context free languages • Extend regular languages • First studied for natural languages • Often used in computer languages – Compilers – Parsers • Pushdown automata

Key Concepts

Key Concepts

Definition Context free grammar

Definition Context free grammar

 • Parsing – Construct meaning (parse tree)

• Parsing – Construct meaning (parse tree)

Constructing CFGs • As the union of simpler CFGs

Constructing CFGs • As the union of simpler CFGs

Constructing CFGs • When given a DFA

Constructing CFGs • When given a DFA

Constructing CFGs • Languages consisting of “linked” strings

Constructing CFGs • Languages consisting of “linked” strings

Constructing CFGs • Strings that may contain structures that appear recursively as part of

Constructing CFGs • Strings that may contain structures that appear recursively as part of other (or the same) structures

Ambiguity • If generates the same string in several wasy, then the grammar is

Ambiguity • If generates the same string in several wasy, then the grammar is ambiguous • E. g. • Grammar does not capture usual precedence relations • One of the main problems in natural language processing • “the boy touches the girl with the flower”

Defining ambiguity • Leftmost derivation : – At every step in the derivation the

Defining ambiguity • Leftmost derivation : – At every step in the derivation the leftmost variable is replaced • A string is derived ambiguously in a CFG if it has two or more different leftmost derivations • A grammar is ambiguous if it generates some string ambiguously • Some context free languages are inherently ambiguous, ie. every grammar for the language is ambiguous

Chomsky Normal Form

Chomsky Normal Form

Pushdown automata • Schema of a finite automaton

Pushdown automata • Schema of a finite automaton

Pushdown automaton • Includes a stack – – Push something on top of stack

Pushdown automaton • Includes a stack – – Push something on top of stack Pop something from top of stack Last in first out principle As in cafeteria – tray

An example PDA

An example PDA

Formal definition

Formal definition

Computation with PDAs

Computation with PDAs

Another example • Non determinism essential for this language

Another example • Non determinism essential for this language

Another example

Another example

 • A CFL accepts a string if there exists a derivation of the

• A CFL accepts a string if there exists a derivation of the string • Involves intermediate strings • Represent intermediate strings on PDA

 • Substitute variables by strings • Replace top variable by string

• Substitute variables by strings • Replace top variable by string

Resulting PDA

Resulting PDA

 • A construction to substitute variable by a string

• A construction to substitute variable by a string

Pumping lemma

Pumping lemma

Overview • • Context free grammars Pushdown Automata Equivalence of PDAs and CFGs Non-context

Overview • • Context free grammars Pushdown Automata Equivalence of PDAs and CFGs Non-context free grammars – Pumping lemma