Context Free Grammars Introduction Finite Automata accepts only

  • Slides: 20
Download presentation
Context Free Grammars

Context Free Grammars

Introduction • Finite Automata accepts only regular languages • Many simple languages are non

Introduction • Finite Automata accepts only regular languages • Many simple languages are non regular: - {anbn : n = 0, 1, 2, …} - {w : w a is palindrome} and there is no finite automata that accepts them. • context-free languages are a larger class of languages that encompasses all regular languages and many others, including the two above.

Context-Free Grammars • Languages that are generated by context-free grammars are context-free languages •

Context-Free Grammars • Languages that are generated by context-free grammars are context-free languages • Context-free grammars are more expressive than finite automata: if a language L is accepted by a finite automata then L can be generated by a context-free grammar • Beware: The converse is NOT true

Context-Free Grammar Definition. A context-free grammar is a 4 -tuple G=(V, S, P, S)

Context-Free Grammar Definition. A context-free grammar is a 4 -tuple G=(V, S, P, S) where: V = a finite set of variables S = a finite set of alphabet or terminals P = a finite set of productions S = start variable Productions of the form, A a where A V, a (V S)*: S V

Derivations Definition. v is one-step derivable from u, written u v, Definition. v is

Derivations Definition. v is one-step derivable from u, written u v, Definition. v is derivable from u, written u * v, if: There is a chain of one-derivations of the form: u u 1 u 2 … v

Context-Free Languages Definition. Given a context-free grammar G = ( , NT, R, S),

Context-Free Languages Definition. Given a context-free grammar G = ( , NT, R, S), the language generated or derived from G is the set: L(G) = {w : S * w } Definition. A language L is context-free if there is a context-free grammar G = (V, T, P, S), such that L is generated from G

CFGs & CFLs: Example 1 {an bn | n 0} all strings of balanced

CFGs & CFLs: Example 1 {an bn | n 0} all strings of balanced parentheses Formally: G = ({S}, {a, b}, {S , S a S b}, S)

CFGs & CFLs: Lessons {an bn cn | n 0} {am bn cm+n |

CFGs & CFLs: Lessons {an bn cn | n 0} {am bn cm+n | m, n 0} {W WR | W Є {a, b}*}:

CFGs & CFLs: Non-Example Can’t be done; CFL pumping lemma later. Intuition: Can count

CFGs & CFLs: Non-Example Can’t be done; CFL pumping lemma later. Intuition: Can count to n, then can count down from n, but forgetting n. n n I. e. , a stack as a counter. Will see this when using a machine corresponding to CFGs.

Parse Tree A parse tree of a derivation is a tree in which: •

Parse Tree A parse tree of a derivation is a tree in which: • Each internal node is labeled with a nonterminal • If a rule A A 1 A 2…An occurs in the derivation then A is a parent node of nodes labeled A 1, A 2, …, An S a S S e b

Parse Trees S A|AB A |a|Ab|AA B b|bc|Bc|b. B Sample derivations: S AB AAB

Parse Trees S A|AB A |a|Ab|AA B b|bc|Bc|b. B Sample derivations: S AB AAB aa. B aabb S AB Abb Aabb aabb These two derivations use same productions, but in different orders. This ordering difference is often uninteresting. Derivation trees give way to abstract away ordering differences. S A Root label = start node. Each interior label = variable. B A A b B a a b Each parent/child relation = derivation step. Each leaf label = terminal or . All leaf labels together = derived string = yield.

Leftmost, Rightmost Derivations Definition. A left-most derivation of a sentential form is one in

Leftmost, Rightmost Derivations Definition. A left-most derivation of a sentential form is one in which rules transforming the left-most nonterminal are always applied Definition. A right-most derivation of a sentential form is one in which rules transforming the right-most nonterminal are always applied

Leftmost & Rightmost Derivations S A|AB A |a|Ab|AA B b|bc|Bc|b. B S A S

Leftmost & Rightmost Derivations S A|AB A |a|Ab|AA B b|bc|Bc|b. B S A S AB AAB aa. B aabb S AB Abb Aabb aabb These two derivations are special. B 1 st derivation is leftmost. A A b B a a Sample derivations: b Always picks leftmost variable. 2 nd derivation is rightmost. Always picks rightmost variable.

Derivation Trees S A|AB A |a|Ab|AA B b|bc|Bc|b. B w = aabb S A

Derivation Trees S A|AB A |a|Ab|AA B b|bc|Bc|b. B w = aabb S A B A b A a A B A A b a ? S S A A b B a a Other derivation trees for this string? b A A A a A b a b ? Infinitely many others possible.

Ambiguous Grammar Definition. A grammar G is ambiguous if there is a word w

Ambiguous Grammar Definition. A grammar G is ambiguous if there is a word w L(G) having are least two different parse trees S A S B S AB A a. A B b. B A e B e Notice that a has at least two left-most derivations

Ambiguity CFG ambiguous any of following equivalent statements: n n n A string w

Ambiguity CFG ambiguous any of following equivalent statements: n n n A string w with multiple derivation trees. A string w with multiple leftmost derivations. A string w with multiple rightmost derivations. Defining ambiguity of grammar, not language.

CFG Simplification Can’t always eliminate ambiguity. But, CFG simplification & restriction still useful theoretically

CFG Simplification Can’t always eliminate ambiguity. But, CFG simplification & restriction still useful theoretically & pragmatically. n n Simpler grammars are easier to understand. Simpler grammars can lead to faster parsing. Restricted forms useful for some parsing algorithms. Restricted forms can give you more knowledge about derivations.

CFG Simplification: Example How can the following be simplified? ? S AB S ACD

CFG Simplification: Example How can the following be simplified? ? S AB S ACD A Aa A a. A A a C D d. D D E E e. Ae F ff ? 1) Delete: B useless because nothing derivable from B. 2) Delete either A Aa or A a. A. 3) Delete one of the idential productions. 4) Delete & also replace S ACD with S AD. 5) Replace with D e. Ae. 6) Delete: E useless after change #5. 7) Delete: F useless because not derivable from S.

CFG Simplification Eliminate ambiguity. Eliminate “useless” variables. Eliminate -productions: A . Eliminate unit productions:

CFG Simplification Eliminate ambiguity. Eliminate “useless” variables. Eliminate -productions: A . Eliminate unit productions: A B. Eliminate redundant productions. Trade left- & right-recursion.

Trading Left- & Right-Recursion Left recursion: A Aa Right recursion: A a A Most

Trading Left- & Right-Recursion Left recursion: A Aa Right recursion: A a A Most algorithms have trouble with one, In recursive descent, avoid left recursion.