Sunteți pe pagina 1din 25

Grammars cont.

Computing Theory
week 4

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


2

Parsing
e.g: S ==> AA ==> A bA ==> abA, so S =*=>abA.

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

Parsing
Derivations S -> aMb M -> A M -> B A -> aA A -> B -> bB B ->
(1) (2) (3) (4) (5) (6) (7)

Parsing

aaab is derivable from S: S ==> aMb ==> aAb ==> aaAb ==> aaaAb ==> aaab S =*=>aaab.
The length of this derivation is 5

Parse Trees and Derivations

Derivations can be written in a graphical form as a parse 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 rightmost derivations Grammars with strings having 2 or more parse trees are ambiguous Some ambiguous grammars can be rewritten as equivalent unambiguous grammars
6

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

Parse Trees and Derivations

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 several derivations, e.g

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))()==>(())()

10

Equivalent Derivations

D1 = D2 except steps 4 and 5 reversed. Derivation where leftmost nonterminals replaced 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

11

Equivalent Derivations

12

Derivations
Leftmost/Rightmost Derivations

The 10 derivations above are related by < as shown below: < D3

D1 < D2

< D5 < D6

< D4
< D7 < D8

< D9 < D10

13

Derivations

Derivations, D and D' are similar if they can be transformed into each other via switching the order in which the rules are applied CFG always has 1 leftmost derivation and 1 rightmost derivation (D1 leftmost, D10 rightmost, above)
To get leftmost derivation, always replace leftmost non-terminal in current string

14

Ambiguous Grammars

Recall the grammar G = V, S,R,S where V = *, +, (, ),E S = *, +, (, ) R= S -> E E -> E + E | E * E | (E) | id


The string id + id * id can be generated by this grammar according to two different parse trees.

15

Ambiguous Grammars

Only one of these (a) corresponds to the natural semantics of id + id * id, where * takes precedence over +.

16

Ambiguous Languages

Many ambiguous grammars (such as the one on the previous slide) can easily be converted to an unambiguous grammar representing the same language.
Some context free languages have the property that all grammars that generate them are ambiguous. Such languages are inherently ambiguous.

Inherently ambiguous languages are not useful for programming languages, formatting languages, or other languages which must be parsed automatically
17

Parsing

Given a context-free grammar G and input w determine 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 a parsing algorithm or parser.
Some grammars allow deterministic parsing, i.e. each sentential form has at most one derivation

18

Ambiguity and Parsing

A grammar is unambiguous if at each step in a leftmost derivation there is only one rule which can lead to the desired string. Deterministic parsing is based upon determining which rule to apply. top-down parsing From start symbol generate a leftmost derivation of w guided by w bottom-up parsing From w generate in reverse order a rightmost derivation of w <=*=S guided by rules of G

19

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 we are about to read an ELSE.

20

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 currently read to ifstmt Or do we shift again and get the ELSE?

This is a shift/reduce conflict: both shifting and reducing is valid

21

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 an ELSE then we know we have to shift.

22

Shift/Reduce Conflicts

Or we could allow the ambiguous grammar, but have a rule to determine how to handle shift/reduce conflicts. 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 reduce it only when the else is read
23

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.

24

Reduce/Reduce Conflicts

Occur when it isnt clear to the parser which rule should be used for reducing
Trivial example: A -> T B -> T

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

25

S-ar putea să vă placă și