Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing

Similar presentations


Presentation on theme: "Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing"— Presentation transcript:

1 Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing
CS 2130 Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing

2 Bottom Up Parsing Assume we have a group of tokens
Bottom up parsing tries to group tokens into things it can reduce Like our precedence rules make 1 + 2 * 3 Understood to be (1 + (2 * 3)) Same idea but with bottom up parsing we use special operators: Wirth-Weber

3 Wirth-Weber Operators
x < y y has higher precedence than x x = y x and y have equal precedence x > y x has higher precedence than y

4 Operation Given a b c d e f g h
Keep in mind that the letters represent tokens thus they can be operators, operands, special symbols, etc

5 Operation Given < a b c d e f g h > Assumed

6 Operation Given < a = b c d e f g h >
Parser moves through tokens comparing precedence

7 Operation Given < a = b > c d e f g h >
A series starting with < and ending with > is called a handle. When a handle is found the parser looks for a match with the right hand side of some production

8 Operation Given < a = b > c d e f g h >
Assume the following is in effect r ::= a b s ::= d e f t ::= r c s

9 Operation Given < r c d e f g h >
Assume the following is in effect r ::= a b s ::= d e f t ::= r c s

10 Operation Given < r c d e f g h > We continue
Assume the following is in effect r ::= a b s ::= d e f t ::= r c s

11 Operation Given < r = c d e f g h > We continue
Assume the following is in effect r ::= a b s ::= d e f t ::= r c s

12 Operation Given < r = c < d e f g h > We continue
Assume the following is in effect r ::= a b s ::= d e f t ::= r c s

13 Operation Given < r = c < d = e f g h > We continue
Assume the following is in effect r ::= a b s ::= d e f t ::= r c s

14 Operation Given < r = c < d = e = f g h > We continue
Assume the following is in effect r ::= a b s ::= d e f t ::= r c s

15 Operation Given < r = c < d = e = f > g h > We continue
Assume the following is in effect r ::= a b s ::= d e f t ::= r c s

16 Operation Given < r = c < d = e = f > g h > We continue
Assume the following is in effect r ::= a b s ::= d e f t ::= r c s

17 Operation Given < r = c s g h > We continue
Assume the following is in effect r ::= a b s ::= d e f t ::= r c s

18 Operation Given < r = c = s g h > We continue
Assume the following is in effect r ::= a b s ::= d e f t ::= r c s

19 Operation Given < r = c = s > g h > We continue
Assume the following is in effect r ::= a b s ::= d e f t ::= r c s

20 Operation Given < r = c = s > g h > We continue
Assume the following is in effect r ::= a b s ::= d e f t ::= r c s

21 Operation Given < t g h > We continue
Assume the following is in effect r ::= a b s ::= d e f t ::= r c s

22 Operation Given < t = g h > We continue
Assume the following is in effect r ::= a b s ::= d e f t ::= r c s

23 Operation Given < t = g = h >
If there is a rule for: "t g h" successful parse If not, syntax error

24 What kind of algorithm? Stack based
Known as semantic stack or shift/reduce algorithm

25 Example < a = b < c = d > e < f > g >
< a = b > x = e < f > g > < y < x = e = z = g > < y = m > < n > If this is the start symbol: Successful Parse!

26 < a = b < c = d > e < f > g >
Stack a, < < a = b < c = d > e < f > g > We process the tokens adding the token and the precedence operator on its left to the stack. Encountering a > indicates a handle has been found and thus will initiate special proceesing

27 Stack b, = a, < < a = b < c = d > e < f > g >

28 Stack c, < b, = a, < < a = b < c = d > e < f > g >

29 Stack d, = c, < b, = a, < < a = b < c = d > e < f > g >

30 Stack d, = c, < b, = a, < < a = b < c = d > e < f > g > < a = b > x = e < f > g > Finding a ">" between d and e, we trace back...

31 Stack x, > b, = a, < < a = b < c = d > e < f > g > < a = b > x = e < f > g > x is not really on stack because of the: >

32 Stack x, < y, < < a = b < c = d > e < f > g > < a = b > x = e < f > g > < y < x = e < f > g > We trace back to process the ab handle and then x goes on stack

33 Stack e, = x, < y, < < a = b < c = d > e < f > g > < a = b > x = e < f > g > < y < x = e < f > g > Continuing...

34 Stack f, < e, = x, < y, < < a = b < c = d > e < f > g > < a = b > x = e < f > g > < y < x = e < f > g > < y < x = e = z = g > Converting f to z

35 Stack g, = f, < e, = x, < y, < < a = b < c = d > e < f > g > < a = b > x = e < f > g > < y < x = e < f > g > < y < x = e = z = g >

36 Stack #, > g, = z, < e, = x, < y, < < a = b < c = d > e < f > g > < a = b > x = e < f > g > < y < x = e < f > g > < y < x = e = z = g > # signifies end of input

37 Stack #, > m, = y, < < a = b < c = d > e < f > g > < a = b > x = e < f > g > < y < x = e < f > g > < y < x = e = z = g > < y = m > Reducing

38 Stack #, > n, < < a = b < c = d > e < f > g > < a = b > x = e < f > g > < y < x = e < f > g > < y < x = e = z = g > < y = m > < n > If n is our start symbol: successful parse!

39 Shift/Reduce Parsing x < y x = y Shift x > y ? Reduce
Error: No relationship  Syntax error Not allowed: More than one relationship Actually < = would be okay, >= or >< is called shift/reduce error

40 Shift/Reduce Parsing Reduce-Reduce Error No Right Hand Side Match
When trying to match, parser finds two rules which match No Right Hand Side Match Syntax error

41 Simple Precedence Only Possibilities x < y x = y x > y
No relationship exists (No others allowed)

42 Bottom-Up Parsing No issues regarding left-recursive versus right-recursive such as those found with Top-down parsing Note: There are grammars that will break a bottom-up parser.

43 Precedence Consider our simple grammar...
Between symbols there must be ? <expr> ::= <expr> <term> | <term> <term> ::= <term> * <factor> | <factor> <factor> ::= '(' <expr> ')' | num | id

44 Precedence Consider our simple grammar...
Between symbols there must be = <expr> ::= <expr> = + = <term> | <term> <term> ::= <term> = * = <factor> | <factor> <factor> ::= '(' = <expr> = ')' | num | id

45 Precedence To determine "end points" we must look at multiple rules to see how they interact... x c = d

46 Precedence To determine "end points" we must look at multiple rules to see how they interact... x c = d To determine what goes here...

47 Precedence To determine "end points" we must look at multiple rules to see how they interact... x c = d We look here.

48 Precedence <expr> ::= <expr> + <term> | <term>
<term> ::= <term> * <factor> | <factor> <factor> ::= '(' <expr> ')' | num | id + = <term> * = <factor> + < <factor> * < ( = <expr> = ) + < ( = <expr> = )

49 Precedence Table <expr> ::= <expr> + <term> | <term> <term> ::= <term> * <factor> | <factor> <factor> ::= '(' <expr> ')' | num | id L R <expr> <term> <factor> + * ( ) num id <expr> <term> <factor> + * ( ) num id

50 Precedence Table <expr> ::= <expr> + <term> | <term> <term> ::= <term> * <factor> | <factor> <factor> ::= '(' <expr> ')' | num | id L R <expr> <term> <factor> + * ( ) num id <expr> <term> <factor> + * ( ) num id

51 Precedence Table <expr> ::= <expr> + <term> | <term> <term> ::= <term> * <factor> | <factor> <factor> ::= '(' <expr> ')' | num | id L R <expr> <term> <factor> + * ( ) num id <expr> = = <term> > = > <factor> > > > + < < < < * = < < < ( < < < ) > > > num > > > id > > >

52 Precedence Table '(' <expr>... '(' < '(' <expr>...
<expr> ::= <expr> + <term> | <term> <term> ::= <term> * <factor> | <factor> <factor> ::= '(' <expr> ')' | num | id L R <expr> <term> <factor> + * ( ) num id <expr> = = <term> > = > <factor> > > > + < < < < * = < < < ( < < < '(' <expr>... '(' < '(' <expr>... ) > > > num > > > id > > >

53 Precedence Table <expr> ::= <expr> + <term> | <term> <term> ::= <term> * <factor> | <factor> <factor> ::= '(' <expr> ')' | num | id L R <expr> <term> <factor> + * ( ) num id <expr> = = <term> > = > <factor> > > > + = < < < < < * = < < < ( = < < < < ) > > > num > > > id > > >

54 Precedence Table '(' = <expr> ')' '(' < <expr> +
<expr> ::= <expr> + <term> | <term> <term> ::= <term> * <factor> | <factor> <factor> ::= '(' <expr> ')' | num | id L R <expr> <term> <factor> + * ( ) num id <expr> = = <term> > = > '(' = <expr> ')' <factor> > > > + = < < < < < * = < < < ( = < < < < ) > > > '(' < <expr> + num > > > id > > >

55 Resolving Ambiguity + = <term>
+ < <term> * <factor> Solve by lookahead: + = <term> + or ) + < <term> * Ambiguity can be resolved by increasing k in LR(k) but that's not the only way: We could rewrite grammar

56 Original Grammar <expr> ::= <expr> + <term> | <term> <term> ::= <term> * <factor> | <factor> <factor> ::= '(' <expr> ')' | num | id

57 Sources of Ambiguity <expr> ::= <expr> + <term> | <term> <term> ::= <term> * <factor> | <factor> <factor> ::= '(' <expr> ')' | num | id

58 Add 2 New Rules <expr> ::= <expr> + <term> | <term> <term> ::= <term> * <factor> | <factor> <factor> ::= '(' <expr> ')' | num | id <e> ::= <expr> <t> ::= <term>

59 Modify <expr> ::= <expr> + <t> | <term>
<term> ::= <term> * <factor> | <factor> <factor> ::= '(' <e> ')' | num | id <e> ::= <expr> <t> ::= <term>

60 Original Grammar Rewritten Grammar
<expr> ::= <expr> + <term> | <term> <term> ::= <term> * <factor> | <factor> <factor> ::= '(' <expr> ')' | num | id Rewritten Grammar <expr> ::= <expr> + <t> | <term> <term> ::= <term> * <factor> | <factor> <factor> ::= '(' <e> ')' | num | id <e> ::= <expr> <t> ::= <term>

61 Performance Size of table is O(n2)
If we use operator precedence then it only uses terminals thus the table size is not affected by adding non-terminals

62 Question 1 a < b = c = d > e a ? x e
What happens if there is no relationship here?

63 Question 2 Where do precedence relationships come from?
Make a table by hand Write a program to make table How such a program works or how to write it are topics beyond the scope of this course.

64 Example

65 < num > + 1 + 2 * 3 Tokenized: num + num * num

66 < <factor> > +
< num > + < <factor> > + 1 + 2 * 3 Tokenized: num + num * num

67 < <factor> > + < <term> > +
< num > + < <factor> > + < <term> > + 1 + 2 * 3 Tokenized: num + num * num

68 < <factor> > + < <term> > +
< num > + < <factor> > + < <term> > + < <expr> = + 1 + 2 * 3 Tokenized: num + num * num

69 < <factor> > + < <term> > +
< num > + < <factor> > + < <term> > + < <expr> = + < <expr> = + < num 1 + 2 * 3 Tokenized: num + num * num

70 < <factor> > + < <term> > +
< num > + < <factor> > + < <term> > + < <expr> = + < <expr> = + < num < <expr> = + < num > * 1 + 2 * 3 Tokenized: num + num * num

71 < <factor> > + < <term> > +
< num > + < <factor> > + < <term> > + < <expr> = + < <expr> = + < num < <expr> = + < num > * < <expr> = + < <factor> > * 1 + 2 * 3 Tokenized: num + num * num

72 < <factor> > + < <term> > +
< num > + < <factor> > + < <term> > + < <expr> = + < <expr> = + < num < <expr> = + < num > * < <expr> = + < <factor> > * < <expr> = + < <term> = * 1 + 2 * 3 Tokenized: num + num * num

73 < <factor> > + < <term> > +
< num > + < <factor> > + < <term> > + < <expr> = + < <expr> = + < num < <expr> = + < num > * < <expr> = + < <factor> > * < <expr> = + < <term> = * < <expr> = + < <term> = * < num 1 + 2 * 3 Tokenized: num + num * num

74 < <factor> > + < <term> > +
< num > + < <factor> > + < <term> > + < <expr> = + < <expr> = + < num < <expr> = + < num > * < <expr> = + < <factor> > * < <expr> = + < <term> = * < <expr> = + < <term> = * < num < <expr> = + < <term> = * < num > 1 + 2 * 3 Tokenized: num + num * num

75 < <factor> > + < <term> > +
< num > + < <factor> > + < <term> > + < <expr> = + < <expr> = + < num < <expr> = + < num > * < <expr> = + < <factor> > * < <expr> = + < <term> = * < <expr> = + < <term> = * < num < <expr> = + < <term> = * < num > < <expr> = + < <term> = * = <factor> > 1 + 2 * 3 Tokenized: num + num * num

76 < <factor> > + < <term> > +
< num > + < <factor> > + < <term> > + < <expr> = + < <expr> = + < num < <expr> = + < num > * < <expr> = + < <factor> > * < <expr> = + < <term> = * < <expr> = + < <term> = * < num < <expr> = + < <term> = * < num > < <expr> = + < <term> = * = <factor> > < <expr> = + = <term> > 1 + 2 * 3 Tokenized: num + num * num

77 < <factor> > + < <term> > +
< num > + < <factor> > + < <term> > + < <expr> = + < <expr> = + < num < <expr> = + < num > * < <expr> = + < <factor> > * < <expr> = + < <term> = * < <expr> = + < <term> = * < num < <expr> = + < <term> = * < num > < <expr> = + < <term> = * = <factor> > < <expr> = + = <term> > < <expr> > 1 + 2 * 3 Tokenized: num + num * num

78 Questions?

79


Download ppt "Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing"

Similar presentations


Ads by Google