Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 7 Syntax Analysis (5) Operator-Precedence Parsing

Similar presentations


Presentation on theme: "Lecture 7 Syntax Analysis (5) Operator-Precedence Parsing"— Presentation transcript:

1 Lecture 7 Syntax Analysis (5) Operator-Precedence Parsing
Compiler Design Lecture 7 Syntax Analysis (5) Operator-Precedence Parsing

2 Bottom-Up Parsing: Problems
Two or more rules have the same handle Ambiguous grammars Operator-Precedence

3 Bottom-Up Parsers Operator-precedence parser LR parser
the least powerful, but simple & easy-to-make LR parser the most powerful, but complex and hard-to-make

4 Operator-Precedence Parser
Grammar must have three conditions: No two distinct non-terminals have the same handle No ε-rules No two adjacent non-terminals in any RHS Grammar: E ::= E A E | (E) | -E | id A ::= + | - | * | / Is not “Operator grammar” since it does not comply with the 3ed condition Can be converted to Operator grammar as follow: E ::= E + E | E – E | E * E | E / E | (E) | -E | id E ::= E + T | E - T | T T ::= T * F | T / F | F F ::= num | id

5 Precedence Relations a < b a has less precedence than b
a = b a has the same precedence as b a > b a has higher precedence than b

6 Precedence Relations E ::= E + T | E - T | T T ::= T * F | T / F | F
F F id(y) id(x) num(2) E ::= E + T | E - T | T T ::= T * F | T / F | F F ::= num | id 6

7 Idea Behind Operator Precedence Parsing
Insert precedence relations in the input string between every adjacent terminals: $ id1 + id2 * id3 $ => $ < id1 > + < id2 > * < id3 > $ To find the handle: Scan the input left to right until first > scan right to left until first < The handle is between < > In the example above: the handle is < id1 > Then substitute the LHS non-terminal instead of the handle => $ E+ id2 * id3 $ And so on ..

8 Idea Behind Operator Precedence Parsing (cont)
If the string is reduced to E1 + E2 * E3, we have two handles: E1 + E2 and E2 * E3 Which of them to choose? Reduce the string to $ + * $ by removing non-terminals Insert precedence relations: $ < + < * > $ The handle is: * Insert non-terminals around it E2 * E3

9 Precedence Table Let G= (N, T, P, S) be CFG, where T= {a1, a2, …, an}

10 Operator-Precedence Parser: Algorithm

11 Idea of the Algorithm Shift action causes the input operator to be evaluated before the stack operator Up in the stack Reduce soon Down at the parse tree Reduce action causes the stack operator to evaluated before the input operator

12 Operator-Precedence Parser: Example

13 Operator-Precedence Parsing: Example

14 Construction of Precedence Table
Two methods to assign precedence relations between every two terminals in the grammar: Method 1: Use some rule of thumbs based on precedence and associativity of the language operators Method 2: Construct equivalent unambiguous grammar to reflect precedence and associativity Induce the precedence relations from it (as before from parse trees)

15 References Compilers: Principles, Techniques and Tools, Alfred V. Aho, Ravi Sethi, and Jeffrey D. Ullman. 1st Edition, Addison-Wesley, 1986 Teaching Materials of: Elements of Compiler Design, Alexander Meduna ,Taylor & Francis, New York, 2007


Download ppt "Lecture 7 Syntax Analysis (5) Operator-Precedence Parsing"

Similar presentations


Ads by Google