CS502: Compiler Design Semantic Analysis Manas Thakur Fall 2020
Trivia Gone are the days of doing things mechanically Taken have they the enjoyment of sitting easily But where is real joy when man becomes machine For it is conscious change that makes life serene Manas Thakur CS502: Compiler Design 2
Let’s move ahead Character stream Machine-Independent Machine-Independent Lexical Analyzer Lexical Analyzer Code Optimizer Code Optimizer B a c k e n d Intermediate representation Token stream F r o n t e n d Syntax Analyzer Code Generator Syntax Analyzer Code Generator Target machine code Syntax tree Machine-Dependent Machine-Dependent Semantic Analyzer Semantic Analyzer Code Optimizer Code Optimizer Syntax tree Target machine code Intermediate Intermediate Symbol Code Generator Code Generator Table Intermediate representation Manas Thakur CS502: Compiler Design 3
Beyond syntax ● What’s wrong with the following code? But it parses correctly! bar(int a, char* s) {...} ● Undeclared identifier int foo() { ● Multiply declared identifier int f[3]; int i, j, k; ● Array index out of bounds char q, *p; ● Wrong number or type of args float k; bar(f[6], 10, x); ● Break outside switch/loop break; i->val = 5; ● Incompatible type for operation q = k + p; ● Goto with no label printf(“%s, %s.\n”, p, k); goto label2; . . . } Manas Thakur CS502: Compiler Design 4
Semantic Analysis ● Beyond syntax; hard to express directly in grammar. ● Requires extra computation, extra data structures. ● But why do we care? – Obvious: ● Report mistakes to programmer ● Avoid bugs: f[6] will cause a run-time failure ● Help programmer verify intent (warnings!) – Such checks also help the compiler: ● Allocate right amount of space for variables ● Select right machine operands ● Proper implementation of control structures Manas Thakur CS502: Compiler Design 5
“Semantics” ● Merriam-Webster: the study of meanings ● What’s wrong with these sentences? – The door kicked the man. – The ship died. – Are they grammatically incorrect? – Is the syntax wrong? – Then what? ● Door and ship do not have the ability to kick or die! Manas Thakur CS502: Compiler Design 6
Now say the problems again ● Programmers bar(int a, char* s) {...} – should not have the ability to access an undeclared variable. int foo() { – should not have the ability to int f[3]; declare a variable again. int i, j, k; char q, *p; – should not have the ability to float k; access the 7 th element of a 3-sized bar(f[6], 10, x); array. Why? How do you know? break; – should not have the ability to add i->val = 5; q = k + p; integers and characters. printf(“%s, %s.\n”, p, q); – and so on. goto label2; ● But programmers are mischiveous! } ● Compiler comes to the rescue :) Manas Thakur CS502: Compiler Design 7
Roadmap ● Parsing – Tells us if input is syntactically correct – Gives us a parse tree ● But we want to do more – Perform some checks and computations – Currently we are not empowered enough Manas Thakur CS502: Compiler Design 8
Syntax-Directed Translation (SDT) ● Basic idea: – Add some computations to parsing – Computations triggered by parsing steps – Syntax-directed ● In practice: – Associate attributes with grammar symbols – Associate computations with productions Manas Thakur CS502: Compiler Design 9
Example: Calculator using SDT ● Store intermediate values with non-terminals ● Perform computations with each production No. Production Computation 1. E’ → E print(E.val) 2. E → E 1 + T E.val ← E 1 .val + T.val 3. E → T E.val ← T.val 4. T → T 1 * F T.val ← T 1 .val * F.val 5. T → F T.val ← F.val 6. F → (E) F.val ← E.val 7. F → id F.val ← id.tokenImage ● Grammars with such attributes are called attribute grammars. Manas Thakur CS502: Compiler Design 10
Attributes ● Synthesized – In terms of the attributes of the node and its children. – Bottom-up over the syntax tree. – Example: computation of values (our SDT calculator). ● Inherited – In terms of the attributes of the node, its parents, and its siblings. – Top-down over the syntax tree. – Example: carrying the type from declaration (int x; x = 10;). ● In practice, we may use both. Manas Thakur CS502: Compiler Design 11
Syntax-Directed Definition (SDD) ● A subtle difference between SDD and SDT. ● The specification of attributes and computations forms an SDD. ● An executable format results into an SDT. No. Production SDT SDD 1. E’ → E print(E.val) E’.val ← E.val 2. E → E 1 + T E.val = E 1 .val + T.val E.val ← E 1 .val + T.val 3. E → T E.val = T.val E.val ← T.val 4. T → T 1 * F T.val = T 1 .val * F.val T.val ← T 1 .val * F.val 5. T → F T.val = F.val T.val ← F.val 6. F → (E) F.val = E.val F.val ← E.val 7. F → id F.val = id.tokenImage F.val ← id.tokenImage ● In SEM, we would usually write SDDs; would use SDT in ICG. Manas Thakur CS502: Compiler Design 12
S-attributed SDDs ● All the attributes are s ynthesized. ● Why are they good? – A well-defined (reverse) topological evaluation order. ● That is, no cycles in evaluation order. ● Why is this good? – Attribute values can be computed easily during bottom-up parsing. ● Why are they bad? – Too strict! – Disallow reasonable non-cyclic orders: ● Use attributes only from parents (and not from siblings). ● Use attributes only from left siblings (and not from right siblings). Manas Thakur CS502: Compiler Design 13
L-attributed SDDs ● Each attribute must be either – synthesized; or – inherited, but with restrictions: For a production A → X 1 X 2 … X n with an inherited attribute X i .a , computed by an action, the rule in an L-attributed SDD may use: ● inherited attributes of A . ● inherited or synthesized attributes of X 1 , X 2 , …, X i-1 . ● inherited or synthesized attributes of X i with no cyclic dependence. ● Basically we overcome the limitations of S-attributed SDDs while still guaranteeing an evaluation order. ● What does L stand for? – L eft to right. Manas Thakur CS502: Compiler Design 14
Announcements ● Next class: We would start using SDDs to check program semantics. ● Poor CS502 has only two poets! Manas Thakur CS502: Compiler Design 15
Recommend
More recommend