Algorithms and Patents Aaron Bloomfield CS 4102 Spring 2011 1.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
-
Upload
lynette-hunt -
Category
Documents
-
view
215 -
download
0
Transcript of 1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
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
;
;
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
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)
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
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
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
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
9
10
11
12
13
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.
15
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”
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
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.
19
20
Symbol Table Implementation
• Usually implemented as hash tables
• Return closest lexical declaration to handle nested lexical scoping
• How to handle multiple scopes?
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
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.