25
1 Grammars cont. Computing Theory week 4

Computing Theory Lecture 4

Embed Size (px)

Citation preview

Page 1: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 1/25

1

Grammars cont.

Computing Theory week 4

Page 2: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 2/25

2

Parsing

Rule application

Given the string uAv and the rule A -> w, we

obtain the string uwv. We denote this as uAv ==> uwv.

A string w is derivable from v if there is a finite

sequence of rule applications from v to w. v ==> w1 ==> w2 ==> ... ==> wn = w

Usually written as v =*=>w

Page 3: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 3/25

3

Parsing

e.g:

S ==> AA ==> A bA ==> abA,

so S =*=>abA.

The length of a derivation v =*=>w is thenumber of rule applications in the derivation

Page 4: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 4/25

4

Parsing

Derivations

S -> aMb (1)

M -> A (2)

M -> B (3)

A -> aA (4)

A -> (5)

B -> bB (6) B -> (7)

Page 5: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 5/25

5

Parsing

aaab is derivable from S:

S ==> aMb ==> aAb ==> aaAb ==> aaaAb ==> aaab

S =*=>aaab.

The length of this derivation is 5

Page 6: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 6/25

6

Parse Trees and Derivations

Derivations can be written in a graphical form as aparse tree

Given a grammar and a string, there may be different

derivations to get the same string Equivalent derivations (same meaning) have the

same parse tree

Any parse tree has unique leftmost and rightmostderivations

Grammars with strings having 2 or more parse treesare ambiguous

Some ambiguous grammars can be rewritten asequivalent unambiguous grammars

Page 7: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 7/257

Parse Trees and Derivations

Example Derivation as Parse Tree

S -> AS | SB |

A -> aB | bA |

B -> bS | c |

S ==> AS ==> bAS ==> baBS ==> bacS ==> bac

A parse tree of S =*=> w is obtained as follows:

The parse tree has root S If S -> AS is the rule applied to S, then add A and S as

children of S. A -> bA, then add b and A as children of A ...

If A -> is the rule applied to A, then add as the only child of A

Page 8: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 8/258

Parse Trees and Derivations

Page 9: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 9/259

Equivalent Derivations

Consider the simple grammar

G = (V, S,R,S) where V = {S, (, )},

S = {(, )},R = {S -> SS | (S) | }

The string (())() can be derived from S by severalderivations, e.g…

Page 10: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 10/2510

Equivalent Derivations

(D1) S==>SS==>(S)S==>((S))S==>(())S==>(())(S)==>(())()

(D2) S==>SS==>(S)S==>((S))S==>((S))(S)==>(())(S)==>(())()

(D3) S==>SS==>(S)S==>((S))S==>((S))(S)==>((S))()==>(())()

(D4) S==>SS==>(S)S==>(S)(S)==>((S))(S)==>(())(S)==>(())()

(D5) S==>SS==>(S)S==>(S)(S)==>((S))(S)==>((S))()==>(())()

(D6) S==>SS==>(S)S==>(S)(S)==>(S)()==>((S))()==>(())()

(D7) S==>SS==>(S)S==>(S)(S)==>((S))(S)==>(())(S)==>(())()

(D8) S==>SS==>(S)S==>(S)(S)==>((S))(S)==>((S))()==>(())()

(D9) S==>SS==>(S)S==>(S)(S)==>(S)(S)==>((S))()==>(())()

(D10) S==>SS==>S(S)==>S()==>(S)()==>((S))()==>(())()

Page 11: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 11/2511

Equivalent Derivations

D1 = D2 except steps 4 and 5 reversed.Derivation where leftmost nonterminalsreplaced first precedes (written <) the other.

D1 < D2 and D2 < D3 However it is not the case that D1 < D3

These all have the same parse tree

Page 12: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 12/2512

Equivalent Derivations

Page 13: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 13/25

13

Derivations

Leftmost/Rightmost Derivations

The 10 derivations above are related by < as shownbelow:

< D3

D1 < D2 < D5 < D6

< D4 < D9 < D10< D7 < D8

Page 14: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 14/25

14

Derivations

Derivations, D and D' are similar if they can betransformed into each other via switching the order inwhich the rules are applied

CFG always has 1 leftmost derivation and 1 rightmostderivation (D1 leftmost, D10 rightmost, above)

To get leftmost derivation, always replace leftmostnon-terminal in current string

Page 15: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 15/25

Page 16: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 16/25

16

Ambiguous Grammars

Only one of these (a) corresponds to the“natural” semantics of id + id * id, where *

takes precedence over +.

Page 17: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 17/25

17

Ambiguous Languages

Many ambiguous grammars (such as the one on theprevious slide) can easily be converted to anunambiguous grammar representing the samelanguage.

Some context free languages have the property thatall grammars that generate them are ambiguous.Such languages are inherently ambiguous.

Inherently ambiguous languages are not useful forprogramming languages, formatting languages, orother languages which must be parsed automatically

Page 18: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 18/25

18

Parsing

Given a context-free grammar G and input wdetermine if w L(G).

How do we determine this for all possible strings?

Multiple derivations may exist

Must also discover when no derivation exists

A procedure to perform this function is called aparsing algorithm or parser.

Some grammars allow deterministic parsing, i.e. eachsentential form has at most one derivation

Page 19: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 19/25

19

Ambiguity and Parsing

A grammar is unambiguous if at each step in aleftmost derivation there is only one rule which canlead to the desired string.

Deterministic parsing is based upon determiningwhich rule to apply.

top-down parsing From start symbol generate aleftmost derivation of w guided by w

bottom-up parsing From w generate in reverse ordera rightmost derivation of w <=*=S guided by rules ofG

Page 20: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 20/25

20

Shift/Reduce Conflicts

Assume the following grammar:

ifstmt: IF expr THEN stmt

| IF expr THEN stmt ELSE stmt

i.e an if statement may or may not contain an else 

part.

Suppose we are part way through our input, and weare about to read an ELSE.

Page 21: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 21/25

21

Shift/Reduce Conflicts

Assume the following grammar:

ifstmt: IF expr THEN stmt

| IF expr THEN stmt ELSE stmt

Do we immediately reduce what we have currentlyread to ifstmt

Or do we shift again and get the ELSE?

This is a shift/reduce conflict: both shifting andreducing is valid

Page 22: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 22/25

22

Shift/Reduce Conflicts

One alternative is to change the grammar:

ifstmt: IF expr THEN stmt END

| IF expr THEN stmt ELSE stmt END

We only reduce when we get an END. If we get anELSE then we know we have to shift.

Page 23: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 23/25

23

Shift/Reduce Conflicts

Or we could allow the ambiguous grammar, but havea rule to determine how to handle shift/reduceconflicts.

Bison does this, and chooses to shift 

This prevents the „dangling else‟ problem

if A then if B then C else D

would be interpreted as

if A then (if B then C else D)

since we keep shifting the 2nd if statement and reduceit only when the else is read

Page 24: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 24/25

24

Shift/Reduce Conflicts

If we reduced first:

if A then if B then C else D

would be interpreted as

if A then (if B then C) else D

since we immediately reduce the 2nd if statement. The„else‟ then attaches to the 1st if statement.

Page 25: Computing Theory Lecture 4

8/3/2019 Computing Theory Lecture 4

http://slidepdf.com/reader/full/computing-theory-lecture-4 25/25

25

Reduce/Reduce Conflicts

Occur when it isn‟t clear to the parser which ruleshould be used for reducing

Trivial example:

A -> TB -> T

and we are trying to reduce T. Which rule do we fire?