Lecture 9UofH - COSC 3340 - Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University...

15
Lecture 9 UofH - COSC 3340 - Dr. Ve rma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9

Transcript of Lecture 9UofH - COSC 3340 - Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University...

Page 1: Lecture 9UofH - COSC 3340 - Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.

Lecture 9 UofH - COSC 3340 - Dr. Verma1

COSC 3340: Introduction to Theory of Computation

University of Houston

Dr. Verma

Lecture 9

Page 2: Lecture 9UofH - COSC 3340 - Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.

Lecture 9 UofH - COSC 3340 - Dr. Verma2

Context Free Languages

Strictly bigger class than regular languages

regular languages

context-freelanguages

{arithmetic expressions} {wwR |w in {a,b}*}

{anbn | n >= 0}

Page 3: Lecture 9UofH - COSC 3340 - Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.

Lecture 9 UofH - COSC 3340 - Dr. Verma3

A simple grammar for some sentences

<Sentence> <noun> <verb> <object>

<noun> Alice | John

<verb> eats | eat | ate

<object> apple | orange | mango

The goal is to generate sentences in English over the English alphabet. Example:

<Sentence> <noun><verb><object> Alice <verb><object>

Alice eats<object> Alice eats apple

Page 4: Lecture 9UofH - COSC 3340 - Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.

Lecture 9 UofH - COSC 3340 - Dr. Verma4

Context-free grammars (CFGs)

Informally, a CFG is a finite set of rules. Each rule is of the form:

<nonterminal symbol> string over terminals and nonterminals. Terminals:- symbols that the desired strings should

contain. – Example: {a...z, ' ',...}

Nonterminals:- symbols to which rules can be applied.

– Example: {<noun>, <verb>, ...} A special nonterminal is called start symbol.

– Example: <Sentence>

Page 5: Lecture 9UofH - COSC 3340 - Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.

Lecture 9 UofH - COSC 3340 - Dr. Verma5

A grammar for arithmetic expressions

E E + E | E * E | (E) | x | y Start symbol - E. Terminals?

– Ans: {x, y, *, +, (, ), ' '} Nonterminals?

– Ans: {E} A derivation:

E E + E E + E * E x + E * E x + x * E x + x * y

Page 6: Lecture 9UofH - COSC 3340 - Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.

Lecture 9 UofH - COSC 3340 - Dr. Verma6

Another grammar for arithmetic expr’s

E E + T | TT T * F | FF (E) | x | yA derivation for x + x * y ?

E E + T T + T F + T x + T x + T * F x + F * F x + x * F x + x * y

Why two different grammars for arithmetic expressions?

Page 7: Lecture 9UofH - COSC 3340 - Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.

Lecture 9 UofH - COSC 3340 - Dr. Verma7

Context Free Grammar Definition

A CFG G = (V, T, P, S) where V T = , – V -- A finite set of symbols called nonterminals – T -- A finite set of symbols called terminals – P is a finite subset of V X (V T)* called

productions or rules– We write A w whenever (A, w) P.– S V -- start symbol

Page 8: Lecture 9UofH - COSC 3340 - Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.

Lecture 9 UofH - COSC 3340 - Dr. Verma8

Derivations and L(G)

One step derivation:– u v if u = xAy, v = xwy and A w in P

0 or more steps derivation:– u * v if u = u0 u1 .... un = v (n 0)

L(G) = { w in T* | S * w }. A language L is context-free if there is a CFG

G with L(G) = L

Page 9: Lecture 9UofH - COSC 3340 - Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.

Lecture 9 UofH - COSC 3340 - Dr. Verma9

Example:

S aSb | Derivation:

S aSb aaSbb aabb.

L(G) = ? Ans: {anbn | n 0 }

Page 10: Lecture 9UofH - COSC 3340 - Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.

Lecture 9 UofH - COSC 3340 - Dr. Verma10

Parse trees

All derivations can be shown in the form of trees. Order of rule application is lost.

S

a bS

a bS

Page 11: Lecture 9UofH - COSC 3340 - Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.

Lecture 9 UofH - COSC 3340 - Dr. Verma11

Parse trees [contd.]

In general, if we apply rule

A w0w1...wn, then we add nodes for wi as children of node labeled A

A

w0 w1 w2 wn

Page 12: Lecture 9UofH - COSC 3340 - Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.

Lecture 9 UofH - COSC 3340 - Dr. Verma12

E E + E E + E * E x + E * E x + x * E x + x * y

E

E E+

E * Ex

x y

Page 13: Lecture 9UofH - COSC 3340 - Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.

Lecture 9 UofH - COSC 3340 - Dr. Verma13

E E + T T + T F + T x + T x + T * F x + F * F x + x * F x + x * y

E

E T+

T *F

T

FF

x x

y

Page 14: Lecture 9UofH - COSC 3340 - Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.

Lecture 9 UofH - COSC 3340 - Dr. Verma14

Leftmost and Rightmost Derivations

Derivation is leftmost if the nonterminal replaced in every step is the leftmost nonterminal.

Consider E E + E E + x. – Is it leftmost derivation?

Derivation is rightmost if the nonterminal replaced in every step is the rightmost nonterminal.

Consider E E + E x + E.– Is it rightmost derivation?

Page 15: Lecture 9UofH - COSC 3340 - Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.

Lecture 9 UofH - COSC 3340 - Dr. Verma15

Ambiguity

A CFG is ambiguous if there is a string with at least two leftmost derivations– Example:

E E + E | E * E | (E) | x | y is ambiguous

A CFL is inherently ambiguous if every CFG that generates it is ambiguous.– Example:

{anbncm | n, m 0} {ambncn | n, m 0}