Context-free A derivation tree or parse tree in G Grammars and - - PowerPoint PPT Presentation

context free
SMART_READER_LITE
LIVE PREVIEW

Context-free A derivation tree or parse tree in G Grammars and - - PowerPoint PPT Presentation

Context-free A derivation tree or parse tree in G Grammars and Languages Example strings in L(G) Another string in L(G) (start or sentence) (rules) 1 2 n e o l p i t m a (only; produces or may be


slide-1
SLIDE 1

Context-free Grammars and Languages

1

Example strings in L(G) (“start” or “sentence”) (“rules”) Another string in L(G) A derivation tree or parse tree in G

2

i.e., “context-free” (only; “produces” or “may be rewritten as”)

:

(reflexive, transitive closure

  • f ⇒ ; “0 or more steps”)

= = =

k≥0

1 1

N

  • t

a t i

  • n

3

Note that L(G) is non-regular E x a m p l e ⋮ ε

4

slide-2
SLIDE 2

We’ll see later that Ltwo={ww|w ∈Σ*} is not context free. At first glance, you might think that adding a new start symbol S’ and a rule S’→SS to G2 would generate Ltwo, but it doesn’t; it generates all strings in Ltwo plus many others, since derivations from the two S’s are not coordinated. (Why not? It’s context-free; what happens to

  • ne S can’t influence the other.)

E x a m p l e

5

:

E x a m p l e

6

Trees, Derivations and Ambiguity

7

A tree A grammar

3 derivations correspond to same tree (same rules being used in the same places, just written in different orders in the linear derivation) 1) E => P+E => a+E => a+P => a+a 2) E => P+E => P+P => a+P => a+a 3) E => P+E => P+P => P+a => a+a But only one leftmost derivation corresponds to it (and vice versa). (more in HW?)

8

slide-3
SLIDE 3

Another grammar for the same language: E → E+E | E*E | (E) | a This grammar is ambiguous: there is a string in L(G5) with two different parse trees, or, equivalently, with 2 different leftmost

  • derivations. Note the pragmatic difference:

in general, (a+a)*a != a+(a*a); which is “right”? Fig 2.6: Two parse trees for a+a!a in grammar G5

9

Fig 2.6: Two parse trees for a+a!a in grammar G5

10

The “E, P” grammar again

This grammar is unambiguous.

(Why? Very informally, the 3 E rules generate P((‘+’∪’*’)P)* and only via a parse tree that “hangs to the right”, as shown.)

But it has another undesirable feature: Parse tree structure does not reflect the usual precedence of * over +. E.g., tree at lower right suggests “a * a + a == a * (a + a)”

11

A more complex grammar, again the same language. This one is unambiguous and its parse trees reflect usual precedence/associativity of plus and times.

12

slide-4
SLIDE 4

G is ambiguous L is inherently ambiguous, meaning every G for L is ambiguous

Can we always tweak the grammar to make it unambiguous? No! Language L is a CFL; grammar at left. Easy to see this G is ambiguous–strings of the form anbncn can come from the i=j (AC) or j=k (DB)

  • path. Hard to prove, but true, that every G for

this L is also ambiguous. Intuitively, a grammar can only match a’s & b’s or b’s & c’s, not both. As a related point, { anbncn | n>0 } is not CFL.

G

13

Some closure results for CFLs

14

Theorem:

The set of context-free languages is closed under ∪, •, and *

Corollary:

All regular languages are CFLs Proof Sketch: Directly give simple CFLs for ∅, {ε}, and {a} for each a ∈ Σ. Combine them using the above theorem.

(Aside: We’ll later prove that CFLs are not closed under intersection or complementation.)

15

V2

G1 G2 G G G

Proof: Closure under Concatenation

16

slide-5
SLIDE 5

G G G

Then, for some x, y ε Σ* A key issue in this direction of the proof is that, since V1 ∩ V2 = ∅, there is no “crosstalk” between the two sub-grammars: any derivation in G from S1 is also a derivation in G1, and likewise S2/G2, so derivation (*) above in G can be split into (**) in G1 & G2.

17