1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

22
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005

Transcript of 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

Page 1: 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

1

Semantic Analysis

Aaron Bloomfield

CS 415

Fall 2005

Page 2: 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

2

Compilation in a Nutshell 1

Source code(character stream)

Lexical analysis

Parsing

Token stream

Abstract syntax tree(AST)

Semantic Analysis

if (b == 0) a = b;

if ( b ) a = b ;0==

if==

b 0

=

a b

if

==

int b int 0

=

int alvalue

int b

boolean

Decorated ASTint

;

;

Page 3: 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

3

Compilation in a Nutshell 2

Intermediate Code Generation

Optimization

Code generation

if

==

int b int 0

=

int alvalue

int b

boolean int;

CJUMP ==

MEM

fp 8

+

CONST MOVE

0 MEM MEM

fp 4 fp 8

NOP

+ +

CJUMP ==

CONST

MOVE

0 DX

CX

NOP

CX CMP CX, 0

CMOVZ DX,CX

Page 4: 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

4

Questions to Answer:

• Is x a scalar, an array, or a function?• Is x declared before it is used?• Which declaration of x does this reference?• Does the dimension of a reference match the

declaration?• Is an array reference in bounds?• Type errors (that can be caught statically)

Page 5: 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

5

Context-Sensitive Analysis

• Two solutions:

Attribute grammars – augment CFG with rules, calculate attributes for grammar symbols

ad hoc techniques – augment grammar with arbitrary code, execute at corresponding reduction, store information in attributes, symbol tables values

Page 6: 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

6

Attribute Grammars

• Generalization of context-free grammars• Each grammar symbol has an associated set of

attributes• Augment grammar with rules that define values

– Not allowed to refer to any variables or attributes outside the current production

Page 7: 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

7

Attribute Types

• Values are computed from constants and other types:– Synthesized attribute – value computed from

children– Inherited attribute – value computed from siblings,

parent, and own attributes

Page 8: 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

8

Attribute Flow

S-attributed grammar– Uses only synthesized types– Bottom-up attribute flow

L-attributed grammar– Attributes can be evaluated in a single left-to-right

pass over the input– Each synthesized attribute of LHS depends only on

that symbol’s own inherited attributes or on attributes (synthesized or inherited) of the production’s RHS symbols

Page 9: 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

9

Page 10: 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

10

Page 11: 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

11

Page 12: 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

12

Page 13: 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

13

Page 14: 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

14

Action Routines

• We need a translation scheme - an algorithm that invokes the attributes in a order that respects attribute flow.

• Action Routines = an Ad hoc translation scheme that is interleaved with parsing

• An action routine = Semantic function that programmer instructs the compiler to execute at a particular point in the parse.

• What most production compilers use.

Page 15: 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

15

Page 16: 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

16

Abstract Syntax Tree

• An abstract syntax tree is the procedure’s parse tree with the nodes for most non-terminal symbols removed

E.g., “a + 3 * b”

Page 17: 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

17

Symbol Table

• A “Dictionary” that maps names to info the compiler knows about that name.

• What names?– Variable and procedure names– Literal constants and strings

• What info?Textual nameData typeDeclaring procedureLexical level of declarationIf array, number and size of dimensionsIf procedure, number and type of parameters

Page 18: 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

18

Sample program

Program gcd (input, output);

Var I,j: integer;

BeginRead(I,j);

While I <> j toIf I > j then I := I – j

Else j := j – I;

Writeln (i)

End.

Page 19: 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

19

Page 20: 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

20

Symbol Table Implementation

• Usually implemented as hash tables

• Return closest lexical declaration to handle nested lexical scoping

• How to handle multiple scopes?

Page 21: 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

21

One option

• Use one symbol table per scope

• Chain tables to enclosing scope

• Insert names in tables for current scope

• Start name lookup in current table, checking enclosing scopes in order if needed

Page 22: 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.

22

LeBlanc-Cook symbol tables

- Give each scope a number,- All names in a hash table, keyed by name - Also have a scope stack – to show current

referencing environment.- As analyzer looks at programs, it pushes and

pops this stack as it enters and leaves scopes.- To look up – scan down the appropriate hash

chain, for each matching entry, scan down the scope stack to see if that is visible. We look no deeper than the top-most scope.