Docsity

Semantic Analysis and Symbol Tables in Compiler Technology, Slides of Compilers

Partial preview of the text

Download Semantic Analysis and Symbol Tables in Compiler Technology and more Slides Compilers in PDF only on Docsity!

COMPILERS

Semantic Analysis

Semantic Analysis

 The compilation process is driven by the syntactic

structure of the program as discovered by the

parser.

 Semantic routines:

 interpret meaning of the program based on its syntactic

structure

 it has two functions:

 (^) finish analysis by deriving context-sensitive information  (^) begin synthesis by generating the IR or target code

 Associated with individual productions of a

context free grammar or subtrees of a syntax

tree.

Context Sensitive Analysis - How  (^) How to check symbols and their semantics at various points in the program?

 Process program linearly (roughly, in-order tree

traversal).

 Maintain a list of currently defined symbols and

what they mean as the program is processed –

this is called a Symbol Table.

Symbol Tables

 (^) Associate lexical names (symbols) with their attributes.  (^) Can contain:

 variable names

 defined constants

 procedure/function/method names

 literal constants and strings

 source text labels

 compiler-generated temporaries

 subtables for structure layouts (types) (field

offsets and lengths)

Attribute Differentiation

 (^) Names may have different attributes depending on their meaning:

 variables: type, procedure level, frame offset

 types: type descriptor, data size/alignment

 constants: type, value

 methods: formals (names/types), result type,

block information (local decls.), frame size

Binding

 (^) As the declarations of types, variables, and functions are processed, identifiers are bound to “meanings” in the symbol table.  (^) A symbol table is a set of bindings.  (^) … But this binding is not static – it changes over the course of the program.

Basic Static Scope

 (^) Usually, a name begins life where it is declared and ends at the end of its block. void foo() { int k; ……. }

Why Scope?

 (^) Scope is not necessary.

 Languages such as assembler have exactly

one scope: the whole program.

 (^) Modern programming languages have more than one scope.

 Information hiding and modularity.

 (^) Goal of any language is to make the programmer’s job simpler.

 One way: keep things isolated.

 Make each thing only affect a limited area.

 Make it hard to break something far away.

Types of Scope

 Static Scope

 Accessibility of variables depends on lexical

structure of program.

 Determined at compile time / “programming

time”.

 Dynamic Scope

 Accessibility of variables depends on call

sequence of program.

 Determined at runtime.

Static Scope

 Consider the Pascal program (which uses static scoping):

program test; var a : integer; procedure proc1; var b : integer; begin end; procedure proc2; var a, c : integer; begin proc1; end; begin proc2; end. in scope: a (from test) in scope: a, c (from proc2) in scope: b (from proc1), a (from test)

Static vs. Dynamic Scope

 (^) Dynamic scope makes it easier to access variables with lifetime, but it is difficult to understand the semantics of code outside the context of execution.  (^) Static scope is more restrictive – therefore easier to read – but may force the use of more subprogram parameters or global identifiers to enable visibility when required.

Scope in a symbol table

 (^) Most modern programming languages have nested static scope.

 The symbol table must reflect this.

 (^) What additional information can reflect nested scope?

 A name query must access the most recent

declaration, from the current scope or some

enclosing scope.

 Innermost scope overrides declarations from

outer scopes.

Symbol Table Implementation  (^) Implemented as a collection of dictionaries in which each symbol is placed.  (^) Many different possible data structures:

 array

 linked list

 hash table

 binary tree

Symbol Table Lookup

 (^) Basic operation is to find the entry for a given symbol.  (^) Each symbol table may have a pointer to its parent scope.  (^) Lookup: if symbol in current table, return it, otherwise look in parent.  (^) Hash tables and binary trees can be used more efficiently.