context free language

18
by: Er. Sukhwinder kaur

Upload: harriet-guerra

Post on 03-Jan-2016

70 views

Category:

Documents


0 download

DESCRIPTION

CONTEXT FREE LANGUAGE. by: Er. Sukhwinder kaur. Topics to be discussed…. Context Free Language Parse Tree Chomsky Normal Form (CNF) Greibech normal form(GNF) Removing null production Unit Productions Pushdown Automata: a preview Pumping Lemma. Context Free Language. Facts: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CONTEXT FREE LANGUAGE

by: Er. Sukhwinder kaur

Page 2: CONTEXT FREE LANGUAGE

Context Free Language Parse Tree Chomsky Normal Form (CNF) Greibech normal form(GNF) Removing null production Unit Productions Pushdown Automata: a preview Pumping Lemma

Page 3: CONTEXT FREE LANGUAGE

Facts:1. each non terminal symbol can derive many different strings.2. Every string in a derivation is called a sentential form.3. Every sentential form containing no non terminal symbols is called a sentence.4. The language L(G) generated by a CFG G is the set of sentences derivable from a distinguished non terminal called the start symbol of G. (eg. <stmt> )5. A language is said to be context free (or a context free language (CFL)) if it can be generated by a CFG. A sentence may have many different derivations; a grammar is called unambiguous if this cannot happen(eg: previous grammar is unambiguous)

Page 4: CONTEXT FREE LANGUAGE

a CFG is a quadruple G = (N,,P,S) whereN is a finite set (of non terminal symbols) is a finite set (of terminal symbols) disjoint from N.S N is the start symbol.P is a a finite subset of N x (N )* (The productions)

Conventions:Non terminals: A,B,C,…terminals: a,b,c,… strings in (N )* : ,…Each (A,) P is called a production rule and is usually written as: A .A set of rules with the same LHS: A A A can be abbreviated as A | | .

back

Page 5: CONTEXT FREE LANGUAGE

S

S

S S

S

( () )

Features of the parse tree:1. The root node is [labeled by] the start symbol: S2. The left to right traversal of all leaves corresponds to the input string : ( ) ( ).3. If X is an internal node and Y1 Y2 … YK

are an left-to-right listing of all its children in the tree, then X --> Y1Y2… Yk is a rule of G.4. Every step of derivation corresponds to one-level growth of an internal node

Page 6: CONTEXT FREE LANGUAGE

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

Page 7: CONTEXT FREE LANGUAGE

Left recursion: A A Right recursion: A A

Most algorithms have trouble with one,

In recursive descent, avoid left recursion.

back

Page 8: CONTEXT FREE LANGUAGE

• Let G be a CFG for some L-{}• Definition: • G is said to be in Chomsky Normal Form if all its

productions are in one of the following two forms:• A BC where A,B,C are variables, or• A a where a is a terminal

• G has no useless symbols• G has no unit productions• G has no -productions

Page 9: CONTEXT FREE LANGUAGE

•Is this grammar in CNF?G1:1. E E+T | T*F | (E) | Ia | Ib | I0 | I12. T T*F | (E) | Ia | Ib | I0 | I13. F (E) | Ia | Ib | I0 | I1 4. I a | b | Ia | Ib | I0 | I1

Checklist:• G has no -productions• G has no unit productions• G has no useless symbols• But…

• the normal form for productions is violated

So, the grammar is not in CNF back

Page 10: CONTEXT FREE LANGUAGE

A CFG is in Greibach normal form if each rule has one these forms:

i. A aA1A2…An

ii. A aiii. S where a and Ai V – {S} for i = 1, 2,…, n

back

Page 11: CONTEXT FREE LANGUAGE

Remove all productions:

(1) If there is a rule P  Q and Q is nullable,

Then: Add the rule P .

(2) Delete all rules Q .

Removing -Productions

back

Page 12: CONTEXT FREE LANGUAGE

A unit production is a rule whose right-hand side consists of a single nonterminal symbol. Example:

S X YX AA B | aB bY TT Y | c

Page 13: CONTEXT FREE LANGUAGE

removeUnits(G) = 1. Let G = G. 2. Until no unit productions remain in G do:

2.1 Choose some unit production X Y. 2.2 Remove it from G. 2.3 Consider only rules that still remain. For every rule Y ,

where V*, do: Add to G the rule X unless it is a rule that has

already been removed once. 3. Return G.

Removing Unit Productions

Example: S X YX AA B | aB bY TT Y | c

S X YA a | bB bT cX a | bY c back

Page 14: CONTEXT FREE LANGUAGE

•FAs recognize regular languages.•What kinds of machines recognize CFLs ?• ===> Pushdown automata (PDAs)•PDA:

•Like FAs but with an additional stack as working memory.•Actions of a PDA• 1. Move right one tape cell (as usual FAs)• 2. push a symbol onto stack• 3. pop a symbol from the stack.•Actions of a PDA depend on• 1. current state 2. currently scanned I/P symbol• 3. current top stack symbol.•A string x is accepted by a PDA if it can enter a final state (or clear all stack symbols) after scanning the entire input.•More details defer to later chapters.

back

Page 15: CONTEXT FREE LANGUAGE

If a language L is accepted by a DFA M with m states, then any string x in L with |x| > m can be written as x = uvw such that (1) v ≠ε, and (2) uv*w is a subset of L (i.e., for any n> 0, uv w in L).

Page 16: CONTEXT FREE LANGUAGE

Consider the path associated with x (|x| > m).

x

Since |x| > m, # of nodes on the path is At least m+1. Therefore, there is a stateAppearing twice.

Page 17: CONTEXT FREE LANGUAGE

u

v

w

v ≠ ε because M is DFA

uw in L because there is a path associated withuw from initial state to a final state.

uv w in Ln due to the same reason as above

back

Page 18: CONTEXT FREE LANGUAGE

Thank You