SLIDE 1
SEMANTIC ANALYSIS
PRINCIPLES OF PROGRAMMING LANGUAGES
Norbert Zeh Winter 2018
Dalhousie University 1/28
SLIDE 2 PROGRAM TRANSLATION FLOW CHART
Scanner (lexical analysis) Parser (syntactic analysis) Semantic analysis and code generation Source program (Character stream) Token stream Parse tree Front end Machine-independent code improvement Target code generation Machine-specific code improvement Modified intermediate form Target language (e.g., assembly) Modified target language Back end Abstract syntax tree or
Symbol table 2/28
SLIDE 3 ROAD MAP
- Syntax, semantics, and semantic analysis
- Attribute grammars
- Action routines
- Abstract syntax trees
3/28
SLIDE 4 ROAD MAP
- Syntax, semantics, and semantic analysis
- Attribute grammars
- Action routines
- Abstract syntax trees
3/28
SLIDE 5 SYNTAX AND SEMANTICS
Syntax
- Describes form of a valid program
- Can be described by a context-free grammar
Semantics
- Describes meaning of a program
- Cannot be described by a context-free grammar
Some constraints that may appear syntactic are enforced by semantic analysis! Example: Use of identifier only after its declaration
4/28
SLIDE 6 SYNTAX AND SEMANTICS
Syntax
- Describes form of a valid program
- Can be described by a context-free grammar
Semantics
- Describes meaning of a program
- Cannot be described by a context-free grammar
Some constraints that may appear syntactic are enforced by semantic analysis! Example: Use of identifier only after its declaration
4/28
SLIDE 7 SYNTAX AND SEMANTICS
Syntax
- Describes form of a valid program
- Can be described by a context-free grammar
Semantics
- Describes meaning of a program
- Cannot be described by a context-free grammar
Some constraints that may appear syntactic are enforced by semantic analysis! Example: Use of identifier only after its declaration
4/28
SLIDE 8 SEMANTIC ANALYSIS
Semantic analysis
- Enforces semantic rules
- Builds intermediate representation (e.g., abstract syntax tree)
- Fills symbol table
- Passes results to intermediate code generator
Two approaches
- Interleaved with syntactic analysis
- As a separate phase
Formal mechanism
5/28
SLIDE 9 SEMANTIC ANALYSIS
Semantic analysis
- Enforces semantic rules
- Builds intermediate representation (e.g., abstract syntax tree)
- Fills symbol table
- Passes results to intermediate code generator
Two approaches
- Interleaved with syntactic analysis
- As a separate phase
Formal mechanism
5/28
SLIDE 10 SEMANTIC ANALYSIS
Semantic analysis
- Enforces semantic rules
- Builds intermediate representation (e.g., abstract syntax tree)
- Fills symbol table
- Passes results to intermediate code generator
Two approaches
- Interleaved with syntactic analysis
- As a separate phase
Formal mechanism
5/28
SLIDE 11 ENFORCING SEMANTIC RULES
Static semantic rules
- Enforced by compiler at compile time
- Example: Do not use undeclared variable.
Dynamic semantic rules
- Compiler generates code for enforcement at runtime.
- Examples: Division by zero, array index out of bounds
- Some compilers allow these checks to be disabled.
6/28
SLIDE 12 ENFORCING SEMANTIC RULES
Static semantic rules
- Enforced by compiler at compile time
- Example: Do not use undeclared variable.
Dynamic semantic rules
- Compiler generates code for enforcement at runtime.
- Examples: Division by zero, array index out of bounds
- Some compilers allow these checks to be disabled.
6/28
SLIDE 13 ROAD MAP
- Syntax, semantics, and semantic analysis
- Attribute grammars
- Action routines
- Abstract syntax trees
7/28
SLIDE 14 ROAD MAP
- Syntax, semantics, and semantic analysis
- Attribute grammars
- Action routines
- Abstract syntax trees
7/28
SLIDE 15 ATTRIBUTE GRAMMARS
Attribute grammar An augmented context-free grammar:
- Each symbol in a production has a number of attributes.
- Each production is augmented with semantic rules that
- Copy attribute values between symbols,
- Evaluate attribute values using semantic functions,
- Enforce constraints on attribute values.
8/28
SLIDE 16
ATTRIBUTE GRAMMAR: EXAMPLE
E → E + T E → E − T E → T T → T ∗ F T → T / F T → F F → − F F → ( E ) F → const E1 E2 T E1 val add E2 val T val E1 E2 T E1 val sub E2 val T val E T E val T val T1 T2 F T1 val mul T2 val F val T1 T2 F T1 val div T2 val F val T F T val F val F1 F2 F1 val neg F2 val F E F val E val F const F val const val
9/28
SLIDE 17
ATTRIBUTE GRAMMAR: EXAMPLE
E → E + T E → E − T E → T T → T ∗ F T → T / F T → F F → − F F → ( E ) F → const E1 → E2 + T { E1.val = add(E2.val, T.val) } E1 → E2 − T { E1.val = sub(E2.val, T.val) } E → T { E.val = T.val } T1 → T2 ∗ F { T1.val = mul(T2.val, F.val) } T1 → T2 / F { T1.val = div(T2.val, F.val) } T → F { T.val = F.val } F1 → − F2 { F1.val = neg(F2.val) } F → ( E ) { F.val = E.val } F → const { F.val = const.val }
9/28
SLIDE 18 SYNTHESIZED AND INHERITED ATTRIBUTES
Synthesized attributes Attributes of LHS of production are computed from attributes of RHS of production. Inherited attributes Attributes flow from left to right:
- From LHS to RHS,
- From symbols on RHS to symbols later on the RHS.
10/28
SLIDE 19 SYNTHESIZED AND INHERITED ATTRIBUTES
Synthesized attributes Attributes of LHS of production are computed from attributes of RHS of production. Inherited attributes Attributes flow from left to right:
- From LHS to RHS,
- From symbols on RHS to symbols later on the RHS.
10/28
SLIDE 20
SYNTHESIZED ATTRIBUTES: EXAMPLE
The language L = {anbncn | n > 0} = {abc, aabbcc, aaabbbccc, . . .} is not context-free but can be defined using an attribute grammar: S A B C Condition: A count B count C count A a A count 1 A1 A2 a A1 count A2 count 1 B b B count 1 B1 B2 b B1 count B2 count 1 C c C count 1 C1 C2 c C1 count C2 count 1
11/28
SLIDE 21
SYNTHESIZED ATTRIBUTES: EXAMPLE
The language L = {anbncn | n > 0} = {abc, aabbcc, aaabbbccc, . . .} is not context-free but can be defined using an attribute grammar: S → A B C {Condition: A.count = B.count = C.count} A → a {A.count = 1} A1 → A2 a {A1.count = A2.count + 1} B → b {B.count = 1} B1 → B2 b {B1.count = B2.count + 1} C → c {C.count = 1} C1 → C2 c {C1.count = C2.count + 1}
11/28
SLIDE 22
SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (1)
Input: aaabbbccc Parse tree: A a a A a A B b b B b B C c c C c C S 1 2 3 1 2 3 1 2 3 3 3 3
12/28
SLIDE 23
SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (1)
Input: aaabbbccc Parse tree: A a a A a A B b b B b B C c c C c C S 1 2 3 1 2 3 1 2 3 3 3 3
12/28
SLIDE 24
SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (1)
Input: aaabbbccc Parse tree: A a a A a A B b b B b B C c c C c C S 1 2 3 1 2 3 1 2 3 3 3 3
12/28
SLIDE 25
SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (1)
Input: aaabbbccc Parse tree: A a a A a A B b b B b B C c c C c C S 1 2 3 1 2 3 1 2 3 3 = 3 = 3
12/28
SLIDE 26
SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (2)
Input: aaabbccc Parse tree: A a a A a A B b b B C c c C c C S 1 2 3 1 2 1 2 3 3 2 3
13/28
SLIDE 27
SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (2)
Input: aaabbccc Parse tree: A a a A a A B b b B C c c C c C S 1 2 3 1 2 1 2 3 3 2 3
13/28
SLIDE 28
SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (2)
Input: aaabbccc Parse tree: A a a A a A B b b B C c c C c C S 1 2 3 1 2 1 2 3 3 2 3
13/28
SLIDE 29
SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (2)
Input: aaabbccc Parse tree: A a a A a A B b b B C c c C c C S 1 2 3 1 2 1 2 3 3 ̸= 2 ̸= 3
13/28
SLIDE 30
INHERITED ATTRIBUTES: EXAMPLE
Again, we consider the language L = {anbncn | n > 0} = {abc, aabbcc, aaabbbccc, . . .}. S A B C B inhCount A count C inhCount A count A a A count 1 A1 A2 a A1 count A2 count 1 B b Condition B inhCount 1 B1 B2 b B2 inhCount B1 inhCount 1 C c Condition C inhCount 1 C1 C2 c C2 inhCount C1 inhCount 1
14/28
SLIDE 31
INHERITED ATTRIBUTES: EXAMPLE
Again, we consider the language L = {anbncn | n > 0} = {abc, aabbcc, aaabbbccc, . . .}. S → A B C {B.inhCount = A.count; C.inhCount = A.count} A → a {A.count = 1} A1 → A2 a {A1.count = A2.count + 1} B → b {Condition : B.inhCount = 1} B1 → B2 b {B2.inhCount = B1.inhCount − 1} C → c {Condition : C.inhCount = 1} C1 → C2 c {C2.inhCount = C1.inhCount − 1}
14/28
SLIDE 32
INHERITED ATTRIBUTES: PARSE TREE DECORATION (1)
Input: aaabbbccc Parse tree: A a a A a A B b b B b B C c c C c C S 1 2 3 3 3 2 2 1 1 1 1 1 1
15/28
SLIDE 33
INHERITED ATTRIBUTES: PARSE TREE DECORATION (1)
Input: aaabbbccc Parse tree: A a a A a A B b b B b B C c c C c C S 1 2 3 3 3 2 2 1 1 1 1 1 1
15/28
SLIDE 34
INHERITED ATTRIBUTES: PARSE TREE DECORATION (1)
Input: aaabbbccc Parse tree: A a a A a A B b b B b B C c c C c C S 1 2 3 3 3 2 2 1 1 1 1 1 1
15/28
SLIDE 35
INHERITED ATTRIBUTES: PARSE TREE DECORATION (1)
Input: aaabbbccc Parse tree: A a a A a A B b b B b B C c c C c C S 1 2 3 3 3 2 2 1 1 1 1 1 1
15/28
SLIDE 36
INHERITED ATTRIBUTES: PARSE TREE DECORATION (1)
Input: aaabbbccc Parse tree: A a a A a A B b b B b B C c c C c C S 1 2 3 3 3 2 2 1 1 1 1 1 1
15/28
SLIDE 37
INHERITED ATTRIBUTES: PARSE TREE DECORATION (1)
Input: aaabbbccc Parse tree: A a a A a A B b b B b B C c c C c C S 1 2 3 3 3 2 2 1 1 1 = 1 1 = 1
15/28
SLIDE 38
INHERITED ATTRIBUTES: PARSE TREE DECORATION (2)
Input: aaabbccc Parse tree: A a a A a A B b b B C c c C c C S 1 2 3 3 3 2 2 1 2 1 1 1
16/28
SLIDE 39
INHERITED ATTRIBUTES: PARSE TREE DECORATION (2)
Input: aaabbccc Parse tree: A a a A a A B b b B C c c C c C S 1 2 3 3 3 2 2 1 2 1 1 1
16/28
SLIDE 40
INHERITED ATTRIBUTES: PARSE TREE DECORATION (2)
Input: aaabbccc Parse tree: A a a A a A B b b B C c c C c C S 1 2 3 3 3 2 2 1 2 1 1 1
16/28
SLIDE 41
INHERITED ATTRIBUTES: PARSE TREE DECORATION (2)
Input: aaabbccc Parse tree: A a a A a A B b b B C c c C c C S 1 2 3 3 3 2 2 1 2 1 1 1
16/28
SLIDE 42
INHERITED ATTRIBUTES: PARSE TREE DECORATION (2)
Input: aaabbccc Parse tree: A a a A a A B b b B C c c C c C S 1 2 3 3 3 2 2 1 2 1 1 1
16/28
SLIDE 43
INHERITED ATTRIBUTES: PARSE TREE DECORATION (2)
Input: aaabbccc Parse tree: A a a A a A B b b B C c c C c C S 1 2 3 3 3 2 2 1 2 ̸= 1 1 = 1
16/28
SLIDE 44 ATTRIBUTE FLOW
Annotation or decoration of the parse tree:
- Process of evaluating attributes
Synthesized attributes:
- Attributes of LHS of each production are computed from attributes of
symbols on the RHS of the production.
- Attributes flow bottom-up in the parse tree.
Inherited attributes:
- Attributes for symbols in the RHS of each production are computed from
attributes of symbols to their left in the production.
- Attributes flow top-down in the parse tree.
17/28
SLIDE 45 ATTRIBUTE FLOW
Annotation or decoration of the parse tree:
- Process of evaluating attributes
Synthesized attributes:
- Attributes of LHS of each production are computed from attributes of
symbols on the RHS of the production.
- Attributes flow bottom-up in the parse tree.
Inherited attributes:
- Attributes for symbols in the RHS of each production are computed from
attributes of symbols to their left in the production.
- Attributes flow top-down in the parse tree.
17/28
SLIDE 46 ATTRIBUTE FLOW
Annotation or decoration of the parse tree:
- Process of evaluating attributes
Synthesized attributes:
- Attributes of LHS of each production are computed from attributes of
symbols on the RHS of the production.
- Attributes flow bottom-up in the parse tree.
Inherited attributes:
- Attributes for symbols in the RHS of each production are computed from
attributes of symbols to their left in the production.
- Attributes flow top-down in the parse tree.
17/28
SLIDE 47 S-ATTRIBUTED AND L-ATTRIBUTED GRAMMARS
S-attributed grammar
- All attributes are synthesized.
- Attributes flow bottom-up.
L-attributed grammar For each production X Y1Y2 Yk,
- X syn depends on X inh and all attributes of Y1 Y2
Yk.
i k, Yi inh depends on X inh and all attributes of Y1 Y2 Yi
1.
S-attributed grammars are a special case of L-attributed grammars.
18/28
SLIDE 48 S-ATTRIBUTED AND L-ATTRIBUTED GRAMMARS
S-attributed grammar
- All attributes are synthesized.
- Attributes flow bottom-up.
L-attributed grammar For each production X → Y1Y2 . . . Yk,
- X.syn depends on X.inh and all attributes of Y1, Y2, . . . , Yk.
- For all 1 ≤ i ≤ k, Yi.inh depends on X.inh and all attributes of Y1, Y2, . . . , Yi−1.
S-attributed grammars are a special case of L-attributed grammars.
18/28
SLIDE 49 S-ATTRIBUTED AND L-ATTRIBUTED GRAMMARS
S-attributed grammar
- All attributes are synthesized.
- Attributes flow bottom-up.
L-attributed grammar For each production X → Y1Y2 . . . Yk,
- X.syn depends on X.inh and all attributes of Y1, Y2, . . . , Yk.
- For all 1 ≤ i ≤ k, Yi.inh depends on X.inh and all attributes of Y1, Y2, . . . , Yi−1.
S-attributed grammars are a special case of L-attributed grammars.
18/28
SLIDE 50
LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (1)
A simple grammar for arithmetic expressions using addition and subtraction: E → T E → EAT T → n A → + A → − 10 3 5 10 3 5 12 10 3 5 10 3 5 2 10 T E 3 A T 5 E A T E 10 10 10 sub sub 3 3 add add 5 5 7 12 Rule R PREDICT R E T n E EAT n T n n A A This grammar captures left-associativity. It is not LL(1)!
19/28
SLIDE 51
LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (1)
A simple grammar for arithmetic expressions using addition and subtraction: E → T E → EAT T → n A → + A → − 10 − 3 + 5 10 3 5 12 10 3 5 10 3 5 2 10 T E − 3 A T + 5 E A T E 10 10 10 sub sub 3 3 add add 5 5 7 12 Rule R PREDICT R E T n E EAT n T n n A A This grammar captures left-associativity. It is not LL(1)!
19/28
SLIDE 52
LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (1)
A simple grammar for arithmetic expressions using addition and subtraction: E → T E → EAT T → n A → + A → − 10 − 3 + 5 = (10 − 3) + 5 = 12 10 − 3 + 5 ̸= 10 − (3 + 5) = 2 10 T E − 3 A T + 5 E A T E 10 10 10 sub sub 3 3 add add 5 5 7 12 Rule R PREDICT R E T n E EAT n T n n A A This grammar captures left-associativity. It is not LL(1)!
19/28
SLIDE 53
LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (1)
A simple grammar for arithmetic expressions using addition and subtraction: E → T E → EAT T → n A → + A → − 10 − 3 + 5 = (10 − 3) + 5 = 12 10 − 3 + 5 ̸= 10 − (3 + 5) = 2 10 T E − 3 A T + 5 E A T E 10 10 10 sub sub 3 3 add add 5 5 7 12 Rule R PREDICT R E T n E EAT n T n n A A This grammar captures left-associativity. It is not LL(1)!
19/28
SLIDE 54
LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (1)
A simple grammar for arithmetic expressions using addition and subtraction: E → T E → EAT T → n A → + A → − 10 − 3 + 5 = (10 − 3) + 5 = 12 10 − 3 + 5 ̸= 10 − (3 + 5) = 2 10 T E − 3 A T + 5 E A T E 10 10 10 sub sub 3 3 add add 5 5 7 12 Rule R PREDICT R E T n E EAT n T n n A A This grammar captures left-associativity. It is not LL(1)!
19/28
SLIDE 55
LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (1)
A simple grammar for arithmetic expressions using addition and subtraction: E → T E → EAT T → n A → + A → − 10 − 3 + 5 = (10 − 3) + 5 = 12 10 − 3 + 5 ̸= 10 − (3 + 5) = 2 10 T E − 3 A T + 5 E A T E 10 10 10 sub sub 3 3 add add 5 5 7 12 Rule R PREDICT( ( (R) ) ) E → T {n} E → EAT {n} T → n {n} A → + {+} A → − {−} This grammar captures left-associativity. It is not LL(1)!
19/28
SLIDE 56
LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (1)
A simple grammar for arithmetic expressions using addition and subtraction: E → T E → EAT T → n A → + A → − 10 − 3 + 5 = (10 − 3) + 5 = 12 10 − 3 + 5 ̸= 10 − (3 + 5) = 2 10 T E − 3 A T + 5 E A T E 10 10 10 sub sub 3 3 add add 5 5 7 12 Rule R PREDICT( ( (R) ) ) E → T {n} E → EAT {n} T → n {n} A → + {+} A → − {−} This grammar captures left-associativity. It is not LL(1)!
19/28
SLIDE 57
LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)
An LL(1) grammar for the same language: PREDICT E → TE′$ {n} E′ → ϵ {$} E′ → ATE′ {+, −} T → n {n} A → + {+} A → − {−} A 5 T E E 3 T A 10 E T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12
20/28
SLIDE 58
LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)
An LL(1) grammar for the same language: PREDICT E → TE′$ {n} E′ → ϵ {$} E′ → ATE′ {+, −} T → n {n} A → + {+} A → − {−} + A 5 ϵ T E′ E′ 3 T − A 10 E′ T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12
20/28
SLIDE 59
LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)
An LL(1) grammar for the same language: PREDICT E → TE′$ {n} E′ → ϵ {$} E′ → ATE′ {+, −} T → n {n} A → + {+} A → − {−} + A 5 ϵ T E′ E′ 3 T − A 10 E′ T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12
20/28
SLIDE 60
LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)
An LL(1) grammar for the same language: PREDICT E → TE′$ {n} E′ → ϵ {$} E′ → ATE′ {+, −} T → n {n} A → + {+} A → − {−} + A 5 ϵ T E′ E′ 3 T − A 10 E′ T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12
20/28
SLIDE 61
LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)
An LL(1) grammar for the same language: PREDICT E → TE′$ {n} E′ → ϵ {$} E′ → ATE′ {+, −} T → n {n} A → + {+} A → − {−} + A 5 ϵ T E′ E′ 3 T − A 10 E′ T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12
20/28
SLIDE 62
LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)
An LL(1) grammar for the same language: PREDICT E → TE′$ {n} E′ → ϵ {$} E′ → ATE′ {+, −} T → n {n} A → + {+} A → − {−} + A 5 ϵ T E′ E′ 3 T − A 10 E′ T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12
20/28
SLIDE 63
LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)
An LL(1) grammar for the same language: PREDICT E → TE′$ {n} E′ → ϵ {$} E′ → ATE′ {+, −} T → n {n} A → + {+} A → − {−} + A 5 ϵ T E′ E′ 3 T − A 10 E′ T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12
20/28
SLIDE 64
LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)
An LL(1) grammar for the same language: PREDICT E → TE′$ {n} E′ → ϵ {$} E′ → ATE′ {+, −} T → n {n} A → + {+} A → − {−} + A 5 ϵ T E′ E′ 3 T − A 10 E′ T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12
20/28
SLIDE 65
LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)
An LL(1) grammar for the same language: PREDICT E → TE′$ {n} E′ → ϵ {$} E′ → ATE′ {+, −} T → n {n} A → + {+} A → − {−} + A 5 ϵ T E′ E′ 3 T − A 10 E′ T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12
20/28
SLIDE 66
LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)
An LL(1) grammar for the same language: PREDICT E → TE′$ {n} E′ → ϵ {$} E′ → ATE′ {+, −} T → n {n} A → + {+} A → − {−} + A 5 ϵ T E′ E′ 3 T − A 10 E′ T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12
20/28
SLIDE 67
LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)
An LL(1) grammar for the same language: PREDICT E → TE′$ {n} E′ → ϵ {$} E′ → ATE′ {+, −} T → n {n} A → + {+} A → − {−} + A 5 ϵ T E′ E′ 3 T − A 10 E′ T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12
20/28
SLIDE 68
LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)
An LL(1) grammar for the same language: PREDICT E → TE′$ {E′.in = T.val; E.val = E′.val} E′ → ϵ {E′.val = E′.in} E′
1
E → ATE′
2
E {E′
2.in = A.fun(E′ 1.in, T.val); E′ 1.val = E′ 2.val}
T → n {T.val = n.val} A → + {T.fun = add} A → − {T.fun = sub} A 5 T E E 3 T A 10 E T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12
20/28
SLIDE 69 ROAD MAP
- Syntax, semantics, and semantic analysis
- Attribute grammars
- Action routines
- Abstract syntax trees
21/28
SLIDE 70 ROAD MAP
- Syntax, semantics, and semantic analysis
- Attribute grammars
- Action routines
- Abstract syntax trees
21/28
SLIDE 71
ACTION ROUTINES
Action routines are instructions for ad-hoc translation interleaved with parsing. Parser generators (e.g., bison or yacc) allow programmers to specify action routines in the grammar. Action routines can appear anywhere in a rule (as long as the grammar is LL(1)).
22/28
SLIDE 72
ACTION ROUTINES: EXAMPLE
Example: E′ → AT {$3.in = $1.fun($0.in, $2.val)} E′ {$0.val = $3.val} Corresponding parse function in recursive descent parser:
def parseEE(node0): node1 = ParseTreeNode() node2 = ParseTreeNode() node3 = ParseTreeNode() parseA(node1) parseT(node2) node3.op = node1.fun(node0.in, node2.val) parseEE(node3) node0.val = node3.val
23/28
SLIDE 73
ACTION ROUTINES: EXAMPLE
Example: E′ → AT {$3.in = $1.fun($0.in, $2.val)} E′ {$0.val = $3.val} Corresponding parse function in recursive descent parser:
def parseEE(node0): node1 = ParseTreeNode() node2 = ParseTreeNode() node3 = ParseTreeNode() parseA(node1) parseT(node2) node3.op = node1.fun(node0.in, node2.val) parseEE(node3) node0.val = node3.val
23/28
SLIDE 74 ROAD MAP
- Syntax, semantics, and semantic analysis
- Attribute grammars
- Action routines
- Abstract syntax trees
24/28
SLIDE 75 ROAD MAP
- Syntax, semantics, and semantic analysis
- Attribute grammars
- Action routines
- Abstract syntax trees
24/28
SLIDE 76 ABSTRACT SYNTAX TREES
Problem with parse trees:
- They represent the full derivation of the program using grammar rules.
- Some grammar variables are there only to aid in parsing (e.g., to eliminate
left-recursion or common prefixes).
- Code generator is easier to implement if the output of the parser is as
compact as possible. Abstract syntax tree (AST) A compressed parse tree that represents the program structure rather than the parsing process.
25/28
SLIDE 77
ABSTRACT SYNTAX TREE: EXAMPLE (1)
Fun → fun id Stmts . Stmts → ϵ Stmts → Stmt Stmts Stmt → . . . fun foo swap drop + . AST: Fun id Stmts Stmt (drop) Stmt (swap) Stmt (+) Fun id fun Stmts . Stmt (swap) Stmts Stmt (drop) Stmts Stmt (+) Stmts
26/28
SLIDE 78
ABSTRACT SYNTAX TREE: EXAMPLE (1)
Fun → fun id Stmts . Stmts → ϵ Stmts → Stmt Stmts Stmt → . . . fun foo swap drop + . AST: Fun id Stmts Stmt (drop) Stmt (swap) Stmt (+) Fun id fun Stmts . Stmt (swap) Stmts Stmt (drop) Stmts Stmt (+) Stmts
26/28
SLIDE 79
ABSTRACT SYNTAX TREE: EXAMPLE (1)
Fun → fun id Stmts . Stmts → ϵ Stmts → Stmt Stmts Stmt → . . . fun foo swap drop + . AST: Fun id Stmts Stmt (drop) Stmt (swap) Stmt (+) Fun id fun Stmts . Stmt (swap) Stmts Stmt (drop) Stmts Stmt (+) Stmts ϵ
26/28
SLIDE 80
ABSTRACT SYNTAX TREE: EXAMPLE (1)
Fun → fun id Stmts . Stmts → ϵ Stmts → Stmt Stmts Stmt → . . . fun foo swap drop + . AST: Fun id Stmts Stmt (drop) Stmt (swap) Stmt (+) Fun id fun Stmts . Stmt (swap) Stmts Stmt (drop) Stmts Stmt (+) Stmts ϵ
26/28
SLIDE 81
ABSTRACT SYNTAX TREE: EXAMPLE (2)
def parseFun(node0): node1 = ParseTreeNode() node2 = ParseTreeNode() matchFunKW() parseId(node1) parseStatements(node2) matchEndKW() def parseStatements(node0): if next token is .: node0.statements = [] else: node1 = ParseTreeNode() node2 = ParseTreeNode() parseStatement(node1) parseStatement(node2) node0.statements = \ [node1.statement] + \ node2.statements
Fun id fun Stmts . Stmt (swap) Stmts Stmt (drop) Stmts Stmt (+) Stmts ϵ Fun id Stmts Stmt (drop) Stmt (swap) Stmt (+)
27/28
SLIDE 82 SUMMARY
- Semantic analysis augments the parsing process to represent the meaning
- f the program.
- The output is often an annotated abstract syntax tree (AST).
- Attribute grammars and action routines are used to construct the AST.
28/28