Example 2.4 I The following CFG handles mathematical expressions G - - PowerPoint PPT Presentation

example 2 4 i
SMART_READER_LITE
LIVE PREVIEW

Example 2.4 I The following CFG handles mathematical expressions G - - PowerPoint PPT Presentation

Example 2.4 I The following CFG handles mathematical expressions G 4 = ( V , , R , expr ) V = { expr , term , factor } = { a , + , , ( , ) } R : expr expr + term | term


slide-1
SLIDE 1

Example 2.4 I

The following CFG handles mathematical expressions G4 = (V , Σ, R, expr) V = {expr, term, factor} Σ = {a, +, ×, (, )} R: expr → expr + term | term term → term × factor | factor factor → (expr) | a Fig 2.5: check a + a × a

November 26, 2020 1 / 12

slide-2
SLIDE 2

Example 2.4 II

E E T F a + T T F a × F a

check (a + a) × a

November 26, 2020 2 / 12

slide-3
SLIDE 3

Example 2.4 III

E T T F F E E T T F F a ( + a ) × a

November 26, 2020 3 / 12

slide-4
SLIDE 4

Design Grammars I

{0n1n | n ≥ 0} ∪ {1n0n | n ≥ 0} S1 → 0S11 | ǫ S2 → 1S20 | ǫ S → S1 | S2 CFG versus DFA For a CFG that is regular, it can be recognized by DFA

November 26, 2020 4 / 12

slide-5
SLIDE 5

Design Grammars II

Rules of CFG can be Ri → aRj if δ(qi, a) = qj Ri → ǫ if qi ∈ F We see the main difference between CFG and DFA CFG: a rule can be like Ri → aRjb

November 26, 2020 5 / 12

slide-6
SLIDE 6

Design Grammars III

DFA: a rule can only be Ri → aRj, where we treat each Ri as a state and let δ(Ri, a) = Rj

November 26, 2020 6 / 12

slide-7
SLIDE 7

Ambiguity I

The same string but obtained in different ways For the example of mathematical expressions discussed earlier, what if we consider the following rules? expr → expr + expr | expr × expr | (expr)|a We see the following ways to parse a + a × a

November 26, 2020 7 / 12

slide-8
SLIDE 8

Ambiguity II

Fig 2.6

E E E a + E a × E a E E a + E E a × E a

This CFG does not give the precedence relation We want that a × a is done first

November 26, 2020 8 / 12

slide-9
SLIDE 9

Ambiguity III

By the more complicated CFG earlier, the parsing is unambiguous Question: how to formally define the ambiguity? We need to define “leftmost derivation” first

November 26, 2020 9 / 12

slide-10
SLIDE 10

Leftmost derivation I

Even for an unambiguous CFG we may have the same parse tree, but different derivations For the CFG discussed earlier we can do expr ⇒ expr + term ⇒ expr + term × factor where the second part is expanded. On the other hand, we can handle the first one first. expr ⇒ expr + term ⇒ term + term

November 26, 2020 10 / 12

slide-11
SLIDE 11

Leftmost derivation II

This is not considered ambiguous Definition of leftmost derivation: at every step the leftmost remaining variable is the one replaced.

November 26, 2020 11 / 12

slide-12
SLIDE 12

Formal definition of ambiguity I

w ambiguous if there exist two leftmost derivations Some context-free languages can be generated by ambiguous & unambiguous grammars We say a CFG is inherently ambiguous if it only has ambiguous grammars See prob 2.29 in the textbook. Details not given here.

November 26, 2020 12 / 12