Semantic Analysis
2
Where we are at
• So far, we’ve only defined
the structure of a
program—aka the syntax
• We are now diving into
the semantics of the
program
7
Semantic Analysis
• Two phases
• Name analysis (aka name resolution)
• For each scope
• Process declarations, add them to symbol table
• Process statements, update IDs to point to their entry
• Type analysis
• Process statements
• Use symbol table info to determine the type of each expression (and sub-expression)
Implementing Semantic Analysis
● Attribute Grammars
Augment rules to do checking during parsing.
Has its limitations.
● Recursive AST Walk
Construct the AST, then use functions and
recursion to explore the tree.
Scope
Simplified view of symbol table example 1
main(){ //line 1
char a[5]; //line 2
int x; //line 3
Simplified view of symbol table example 2
Possible Symbol Table Implementations
Why Two Interpretations?
•Spaghetti stack more accurately captures
the scoping structure
•Spaghetti stack is a static structure
•Explicit stack is a dynamic structure.
Scoping in Object-Oriented Languages
Explicit Disambiguation: Another Example
Single- and Multi-Pass Compilers
•Parsing methods such as LL(1), LR(0), LALR(1), etc. always
scan the input from left-to-right
•Since we only need one token of lookahead, we can do
lexical analysis and parsing simultaneously in one pass
over the file
•Some compilers can combine scanning, parsing,
semantic analysis, and code generation into the same
pass. These are called single-pass compilers
•Other compilers rescan the input multiple times. These
are called multi-pass compilers
89
Symbol table entries
• Table that binds a name to information we need
• What information do you think we need?
• Kind (struct, variable, function, class)
• Type (int, int × string → bool, struct)
• Nesting level
• Runtime location (where it’s stored in memory)
90
Symbol table implementation
•We want the symbol table to efficiently add an
entry when we need it, remove it when we’re
done with it
•A good choice is a list of Java HashMaps,
ArrayLists for ordered lists (parameters, etc.)
•This makes sense since we expect to remove a
lot of names from scope at once
Example
91
g: () -> void, 1
92
Symbol kinds
• Symbol kinds (= types of identifiers)
• Variables
• Carries a name, primitive type
• Function declarations
• Carries a name, return type, list of parameter types
• Struct definitions
• Carries a name, list of fields (types with names), size
93
Sym class implementation
• There are many ways to implement symbols
• Here’s one suggestion
• Sym class for variable definitions
• FnSym class for function declarations
• StructDefSym for struct type definitions
• Contains it’s OWN symbol table for it’s field definitions
• StructSym for when you want an instance of a struct
94
Implementing name analysis with an AST
• At this point, we’re done with the Parse Tree
- All subsequent processing done on the AST + symbol table
• Walk the AST
• Augment AST nodes where names are used (both declarations and uses) with
a link to the relevant object in the symbol table
• Put new entries into the symbol table when a declaration is encountered
95
int a;
int f(bool r) {
struct b{
int q;
};
cout << a;
}
WriteStmtNode
DeclListNode
VarDeclNode FnDeclNode
IntNode IdNode IntNode IdNode FormalListNode FnNodeBody
VarDeclNode StructDeclNode
BoolNode IdNode DeclListNode
VarDeclNode
IntNode IdNode
IdNode
Abstract Syntax Tree
Symbol Table
Id types: Var, Function, Struct
Sym
Type: Int
a
FnSym
RetType: Int
List<Type>: [bool]
f
StructDeclSym
fields
b
Sym
Type: bool
r
Sym
Type: Int
q
Symbol Table for Classes and Methods
8. Semantic analysis, scope, symbol table.pptx
8. Semantic analysis, scope, symbol table.pptx
8. Semantic analysis, scope, symbol table.pptx
8. Semantic analysis, scope, symbol table.pptx
8. Semantic analysis, scope, symbol table.pptx
8. Semantic analysis, scope, symbol table.pptx
8. Semantic analysis, scope, symbol table.pptx
8. Semantic analysis, scope, symbol table.pptx
8. Semantic analysis, scope, symbol table.pptx
8. Semantic analysis, scope, symbol table.pptx
8. Semantic analysis, scope, symbol table.pptx
8. Semantic analysis, scope, symbol table.pptx
8. Semantic analysis, scope, symbol table.pptx
8. Semantic analysis, scope, symbol table.pptx
8. Semantic analysis, scope, symbol table.pptx
8. Semantic analysis, scope, symbol table.pptx

8. Semantic analysis, scope, symbol table.pptx

  • 1.
  • 2.
    2 Where we areat • So far, we’ve only defined the structure of a program—aka the syntax • We are now diving into the semantics of the program
  • 7.
    7 Semantic Analysis • Twophases • Name analysis (aka name resolution) • For each scope • Process declarations, add them to symbol table • Process statements, update IDs to point to their entry • Type analysis • Process statements • Use symbol table info to determine the type of each expression (and sub-expression)
  • 8.
    Implementing Semantic Analysis ●Attribute Grammars Augment rules to do checking during parsing. Has its limitations. ● Recursive AST Walk Construct the AST, then use functions and recursion to explore the tree.
  • 9.
  • 14.
    Simplified view ofsymbol table example 1 main(){ //line 1 char a[5]; //line 2 int x; //line 3
  • 15.
    Simplified view ofsymbol table example 2
  • 40.
    Possible Symbol TableImplementations
  • 55.
    Why Two Interpretations? •Spaghettistack more accurately captures the scoping structure •Spaghetti stack is a static structure •Explicit stack is a dynamic structure.
  • 56.
  • 71.
  • 74.
    Single- and Multi-PassCompilers •Parsing methods such as LL(1), LR(0), LALR(1), etc. always scan the input from left-to-right •Since we only need one token of lookahead, we can do lexical analysis and parsing simultaneously in one pass over the file •Some compilers can combine scanning, parsing, semantic analysis, and code generation into the same pass. These are called single-pass compilers •Other compilers rescan the input multiple times. These are called multi-pass compilers
  • 89.
    89 Symbol table entries •Table that binds a name to information we need • What information do you think we need? • Kind (struct, variable, function, class) • Type (int, int × string → bool, struct) • Nesting level • Runtime location (where it’s stored in memory)
  • 90.
    90 Symbol table implementation •Wewant the symbol table to efficiently add an entry when we need it, remove it when we’re done with it •A good choice is a list of Java HashMaps, ArrayLists for ordered lists (parameters, etc.) •This makes sense since we expect to remove a lot of names from scope at once
  • 91.
  • 92.
    92 Symbol kinds • Symbolkinds (= types of identifiers) • Variables • Carries a name, primitive type • Function declarations • Carries a name, return type, list of parameter types • Struct definitions • Carries a name, list of fields (types with names), size
  • 93.
    93 Sym class implementation •There are many ways to implement symbols • Here’s one suggestion • Sym class for variable definitions • FnSym class for function declarations • StructDefSym for struct type definitions • Contains it’s OWN symbol table for it’s field definitions • StructSym for when you want an instance of a struct
  • 94.
    94 Implementing name analysiswith an AST • At this point, we’re done with the Parse Tree - All subsequent processing done on the AST + symbol table • Walk the AST • Augment AST nodes where names are used (both declarations and uses) with a link to the relevant object in the symbol table • Put new entries into the symbol table when a declaration is encountered
  • 95.
    95 int a; int f(boolr) { struct b{ int q; }; cout << a; } WriteStmtNode DeclListNode VarDeclNode FnDeclNode IntNode IdNode IntNode IdNode FormalListNode FnNodeBody VarDeclNode StructDeclNode BoolNode IdNode DeclListNode VarDeclNode IntNode IdNode IdNode Abstract Syntax Tree Symbol Table Id types: Var, Function, Struct Sym Type: Int a FnSym RetType: Int List<Type>: [bool] f StructDeclSym fields b Sym Type: bool r Sym Type: Int q
  • 96.
    Symbol Table forClasses and Methods