Compiler Construction
Lecture 7: Bottom-up parsing 2020-01-28 Michael Engel
Includes material by Jan Christian Meyer and Rich Maclin (UNM)
Compiler Construction Lecture 7: Bottom-up parsing 2020-01-28 - - PowerPoint PPT Presentation
Compiler Construction Lecture 7: Bottom-up parsing 2020-01-28 Michael Engel Includes material by Jan Christian Meyer and Rich Maclin (UNM) Overview Top-down parsing revisited Bottom-up parsing Comparison to top-down parsing
Includes material by Jan Christian Meyer and Rich Maclin (UNM)
Compiler Construction 07: Bottom-up parsing
2
Compiler Construction 07: Bottom-up parsing
3
Syntax analysis regular languages (type 3) context-free (type 2) context-sensitive (type 1) recursively enumerable (type 0)
Finite automata Stack machines
Compiler Construction 07: Bottom-up parsing
4
Syntax analysis
void parse_A() { switch (sym) { case 'x': add_tree(x,B); match(x); parse_B(); break; case 'y': add_tree(y,C); match(y); parse_C(); break; case EOF: error(); break; } return; }
void parse_B() { switch (sym): case 'x': add_tree(x,B); match(x); parse_B(); break; case 'y': error(); break; case EOF: return; return; } void parse_C() { switch (sym): case 'x': error(); break; case 'y': add_tree(y,C); match(y); parse_C(); break; case EOF: return; return; }
Compiler Construction 07: Bottom-up parsing
5
Syntax analysis
parse_A parse_A match(y) parse_A parse_C match(y) parse_A parse_A parse_C parse_A parse_C parse_A parse_C parse_C match(y) parse_A parse_C parse_C parse_C parse_A parse_C parse_C parse_C match(y)
parse_A parse_C parse_C parse_C match(y)
parse_A parse_C parse_C parse_C parse_A parse_C parse_C parse_A parse_C parse_A Call Call Call Call Call Call parse_A parse_C parse_C Return Return Return Call Call Return Return Return Return Finished
Compiler Construction 07: Bottom-up parsing
Syntax analysis
parse_A parse_A match(y) parse_A parse_C match(y) parse_A parse_A parse_C parse_A parse_C Call Call Call Call Call Return Return
Compiler Construction 07: Bottom-up parsing
7
Syntax analysis
initial part
token stream that is already derived input token stream remaining to be read
Part of the syntax tree that has already been derived 𝛃: current NT symbol
Compiler Construction 07: Bottom-up parsing
8
Syntax analysis
initial part already reduced input token stream remaining to be read
We try to guess a production 𝛃 → v1v2
Compiler Construction 07: Bottom-up parsing
9
Syntax analysis
Compiler Construction 07: Bottom-up parsing
10
Syntax analysis
+
Compiler Construction 07: Bottom-up parsing
11
Syntax analysis
I = input string repeat select a non-empty substring 𝛾 of I where X→𝛾 is a production in the grammar if no such 𝛾 exists, backtrack replace one 𝛾 by X in I until I == "S" /* start symbol */
Compiler Construction 07: Bottom-up parsing
12
Syntax analysis
I = input string repeat select a non-empty substring 𝛾 of I where X→𝛾 is a production in the grammar if no such 𝛾 exists, backtrack replace one 𝛾 by X in I until I == "S" /* start symbol */
Compiler Construction 07: Bottom-up parsing
13
Syntax analysis
Compiler Construction 07: Bottom-up parsing
14
Syntax analysis
Compiler Construction 07: Bottom-up parsing
15
Syntax analysis
Compiler Construction 07: Bottom-up parsing
16
Syntax analysis
Compiler Construction 07: Bottom-up parsing
17
Syntax analysis
Compiler Construction 07: Bottom-up parsing
18
Syntax analysis
↑ int × int + int shift … E × E ↑ + int reduce E → int × E E ↑ + int shift E + ↑ int shift E + int ↑ reduce E → int E + E ↑ reduce E → E + E E ↑ ↑ int × int + int shift … E × E ↑ + int shift E × E + ↑ int shift E × E + int ↑ reduce E → int E × E + E ↑ reduce E → E + E E × E ↑ reduce E → E × E E ↑
We can decide to either shift or reduce in this step
Compiler Construction 07: Bottom-up parsing
19
Syntax analysis
The term “precedence declaration” is misleading. These declarations do not define precedence; they define conflict resolutions
Compiler Construction 07: Bottom-up parsing
20
Syntax analysis