CS502: Compiler Design Semantic Analysis Manas Thakur Fall 2020 - - PowerPoint PPT Presentation

cs502 compiler design semantic analysis manas thakur
SMART_READER_LITE
LIVE PREVIEW

CS502: Compiler Design Semantic Analysis Manas Thakur Fall 2020 - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

Manas Thakur

Fall 2020

CS502: Compiler Design Semantic Analysis

slide-2
SLIDE 2

Manas Thakur CS502: Compiler Design 2

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

slide-3
SLIDE 3

Manas Thakur CS502: Compiler Design 3

Let’s move ahead

Lexical Analyzer Lexical Analyzer Syntax Analyzer Syntax Analyzer Semantic Analyzer Semantic Analyzer Intermediate Code Generator Intermediate Code Generator Character stream Token stream Syntax tree Syntax tree Intermediate representation Machine-Independent Code Optimizer Machine-Independent Code Optimizer Code Generator Code Generator Target machine code Intermediate representation Machine-Dependent Code Optimizer Machine-Dependent Code Optimizer Target machine code Symbol Table

F r o n t e n d B a c k e n d

slide-4
SLIDE 4

Manas Thakur CS502: Compiler Design 4

Beyond syntax

  • What’s wrong with the following code?

But it parses correctly!

  • Undeclared identifier
  • Multiply declared identifier
  • Array index out of bounds
  • Wrong number or type of args
  • Break outside switch/loop
  • Incompatible type for operation
  • Goto with no label

. . .

bar(int a, char* s) {...} int foo() { int f[3]; int i, j, k; char q, *p; float k; bar(f[6], 10, x); break; i->val = 5; q = k + p; printf(“%s, %s.\n”, p, k); goto label2; }

slide-5
SLIDE 5

Manas Thakur CS502: Compiler Design 5

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
slide-6
SLIDE 6

Manas Thakur CS502: Compiler Design 6

“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!
slide-7
SLIDE 7

Manas Thakur CS502: Compiler Design 7

Now say the problems again

  • Programmers

– should not have the ability to

access an undeclared variable.

– should not have the ability to

declare a variable again.

– should not have the ability to

access the 7th element of a 3-sized array.

– should not have the ability to add

integers and characters.

– and so on.

  • But programmers are mischiveous!
  • Compiler comes to the rescue :)

bar(int a, char* s) {...} int foo() { int f[3]; int i, j, k; char q, *p; float k; bar(f[6], 10, x); break; i->val = 5; q = k + p; printf(“%s, %s.\n”, p, q); goto label2; } Why? How do you know?

slide-8
SLIDE 8

Manas Thakur CS502: Compiler Design 8

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

slide-9
SLIDE 9

Manas Thakur CS502: Compiler Design 9

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

slide-10
SLIDE 10

Manas Thakur CS502: Compiler Design 10

Example: Calculator using SDT

  • Store intermediate values with non-terminals
  • Perform computations with each production
  • Grammars with such attributes are called attribute grammars.

No. Production Computation 1. E’ → E print(E.val) 2. E → E1 + T E.val ← E1.val + T.val 3. E → T E.val ← T.val 4. T → T1 * F T.val ← T1.val * F.val 5. T → F T.val ← F.val 6. F → (E) F.val ← E.val 7. F → id F.val ← id.tokenImage

slide-11
SLIDE 11

Manas Thakur CS502: Compiler Design 11

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.
slide-12
SLIDE 12

Manas Thakur CS502: Compiler Design 12

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.
  • In SEM, we would usually write SDDs; would use SDT in ICG.

No. Production SDT SDD 1. E’ → E print(E.val) E’.val ← E.val 2. E → E1 + T E.val = E1.val + T.val E.val ← E1.val + T.val 3. E → T E.val = T.val E.val ← T.val 4. T → T1 * F T.val = T1.val * F.val T.val ← T1.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

slide-13
SLIDE 13

Manas Thakur CS502: Compiler Design 13

S-attributed SDDs

  • All the attributes are synthesized.
  • 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).
slide-14
SLIDE 14

Manas Thakur CS502: Compiler Design 14

L-attributed SDDs

  • Each attribute must be either

– synthesized; or – inherited, but with restrictions:

  • Basically we overcome the limitations of S-attributed SDDs while

still guaranteeing an evaluation order.

  • What does L stand for?

– Left to right.

For a production A → X1 X2 … Xn with an inherited attribute Xi.a, computed by an action, the rule in an L-attributed SDD may use:

  • inherited attributes of A.
  • inherited or synthesized attributes of X1, X2, …, Xi-1.
  • inherited or synthesized attributes of Xi with no cyclic dependence.
slide-15
SLIDE 15

Manas Thakur CS502: Compiler Design 15

Announcements

  • Next class: We would start using SDDs to

check program semantics.

  • Poor CS502 has only two poets!