Documente Academic
Documente Profesional
Documente Cultură
Computing Theory
week 4
Parsing
Rule application
Given the string uAv and the rule A -> w, we obtain the string uwv.
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
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
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
D1 < D2
< D5 < D6
< D4
< D7 < D8
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
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
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?
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
Shift/Reduce Conflicts
If we reduced first:
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
25