Static checking and symbol table Chapter 6, Chapter 7.6 and Chapter 8.2 Static checking: check whether the program follows both the syntactic and semantic.

Slides:



Advertisements
Similar presentations
CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi –
Advertisements

CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.
CPSC 388 – Compiler Design and Construction
Semantic Analysis and Symbol Tables
Symbol Table.
Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
1 Compiler Construction Intermediate Code Generation.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Lecture # 20 Type Systems. 2 A type system defines a set of types and rules to assign types to programming language constructs Informal type system rules,
Type Checking Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic.
Chapter 6 Type Checking Section 0 Overview 1.Static Checking Check that the source program follows both the syntactic and semantic conventions of the source.
Type Checking.
CH4.1 CSE244 Type Checking Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs,
Compiler Construction
Lesson 12 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Chapter 6 Type Checking Section 0 Overview
1 Intermediate Code generation. 2 Intermediate Code Generation l Intermediate languages l Declarations l Expressions l Statements l Reference: »Chapter.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
CH4.1 CSE244 Intermediate Code Generation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
Types Type = Why? a set of values
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Imperative Programming
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Compiler Construction
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 Run-Time Environments. 2 Procedure Activation and Lifetime A procedure is activated when called The lifetime of an activation of a procedure is the.
1 Intermediate Code Generation Part I Chapter 8 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
1 October 25, October 25, 2015October 25, 2015October 25, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
Intermediate Code Generation
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
1 Intermediate Code Generation Abstraction at the source level identifiers, operators, expressions, statements, conditionals, iteration, functions (user.
Review: Syntax directed translation. –Translation is done according to the parse tree. Each production (when used in the parsing) is a sub- structure of.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Static Checking and Type Systems Chapter 6. 2 Static versus Dynamic Checking Static checking: the compiler enforces programming language’s static semantics.
1 Run-Time Environments Chapter 7 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Lecture 8 Semantic Analysis.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
1 Semantic Analysis  Semantic analysis includes  Dynamic Checking (Those checks for which to perform, compiler doesn’t have sufficient information) 
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Lecture 9 Symbol Table and Attributed Grammars
Compiler Design – CSE 504 Type Checking
Run-Time Environments Chapter 7
Semantic Analysis Type Checking
Context-Sensitive Analysis
Constructing Precedence Table
Review: Chapter 5: Syntax directed translation
Compiler Construction
Semantic Analysis Chapter 6.
Subject Name:COMPILER DESIGN Subject Code:10CS63
Intermediate Code Generation Part I
Intermediate Code Generation
Static Checking and Type Systems
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Intermediate Code Generation Part I
Three-address code A more common representation is THREE-ADDRESS CODE . Three address code is close to assembly language, making machine code generation.
Semantic Analysis Semantic analysis includes
Intermediate Code Generation Part I
Semantic Analysis Chapter 6.
Compiler Construction
Intermediate Code Generation Part I
Compiler Construction
Corresponds with Chapter 5
Presentation transcript:

Static checking and symbol table Chapter 6, Chapter 7.6 and Chapter 8.2 Static checking: check whether the program follows both the syntactic and semantic conventions at compile time (versus dynamic checking -- check at run time). Examples of static checking –Type checks: –Flow of control checks int a, b[10], c; … a = b + c; main { int I …. I++; break; }

–Examples of static checks –uniqueness check: –defined before use: –name related check: –Some checks can only be done at runtime: Array-bound checking in java: a[i] = 0; main() { int i, j; double i, j; …. } main() { int i; i1 = 0; …. } LOOPA: LOOP EXIT WHEN I=N I=I+1; END LOOP LOOPB;

–To perform static checks, semantic information must be recorded in some place -- symbol table. Grammar specifies the syntax, additional (semantic) information, sometimes called attributes, must be recorded in symbol table for all identifiers. Typically attributes in a symbol table entry include type and offset (where in the memory can I find this variable?). –Struct {int id; int type; int offset;} stentry; Organization of a symbol table: –basic requirement: must be able to find the information associated with a symbol (identifier) quickly. –Example: array, link list, hash table. –Provides two functions: enter(table, name, type, offset) and lookup(name);

–Dealing with nested scope: –How to organize the symbol table? –How to do lookup and enter? One symbol table for each scope (procedure, blocks)? Maintain a stack of symbol tables for lookup/enter Program sort(input, output) var a: array [0..10] of integers; x: integer; procedure readarray var x : real; begin …. x …. End procedure quicksort(i, j) begin … x … end main() { int a, b; a = 0; { int a; a = 1; } printf(“a = %d\n”, a); }

Symbol tables for sort: nil header a... x... readarray quicksort header x …. header Symbol table for sort Symbol table for readarray Symbol table for quicksort

How does the compiler created the symbol table? –First let us consider the simple case: no nested scope, every thing entered into one symbol table: table by using enter (table, id, type, offset) –grammar: P ->D D ->D; D D ->id : T T -> integer T ->real T ->array [num] of T T ->^T I : array [10] of integer; j : real; k : integer I array(10, integer) 0 j real 40 k integer 48

P -> {offset = 0;} D D ->D; D D ->id : T {enter(table, id.name, T.type, offset); offset:= offset + T.width} T -> integer {T.type = integer; T.width = 4} T ->real {T.type = real; T.width = 8;} T ->array [num] of T1 {T.type = array(num.val, T1.type); T.width = num.val * T1.width} T ->^T1 {T.type = pointer(T1.type); T.width = 4;}

–Now consider the case when you have nested procedures (blocks can be considered as special procedures) must maintain a stack of symbol tables, create new ones when entering new procedure must reset offset when entering new procedures (a stack of offsets) Let us also compute the total size of a table –Grammar: P->D D ->D; D D->id : T D->proc id; D; S T ->integer | real | array[num] of T | ^T

mktable(previous): make a new table, properly set all links and related information. Enter(table, name, type, offset). Addwidth(table, width): compute all memory needed by the symbol table. Enterproc(table, name, newtable): enter the procedure name with its symbol table into the old table. –Grammar: P->{t=mktable(nil); push(t, tblptr);push(0, offset);}D {addwidth(top(tblptr), top(offset))} D ->D; D D->id : T {enter(top(tblptr), id.name, T.type, top(offset)); top(offset) = top(offset) + T.width;} D->proc id; {t:=mktable(top(tblptr));push(t, tblptr); push(0, offset);}D; S {t:= top(tblptr);addwidth(t, top(offset)); pop(tblptr); pop(offset);enterproc(top(tblptr), id.name, t)}

Dealing with structure (record): –T ->record D end –Make a new symbol table for all the fields in the record.

T->record { t=mktable(nil); push(t, tblptr); push(0, offset); } D end { T.type = record(top(tblptr)); T.width = top(offset); pop(tblptr); pop(offset); }

Question: How does allowing variable declaration at anywhere in a program (like in C++, java) affect the maintenance of the symbol tables?

–Type checking Make sure operations can be performed on operands. Make sure the types of actual arguments matches the types of formal arguments. Need a type system to do the job. –A type system is a collection of rules for assigning type expression to the various parts of a program. –The type system for a practical language can be complicated. Type checking of expressions: P->D;E D->D;D | id : T T->char | integer | array[num] of T | ^T E->literal | num | id | E mod E | E[E] | E^

P->D;E D->D;D D->id : T {enter(id.val, T.type);} T->char {T.type = char;} | integer {T.type = integer;} | array[num] of T1 {T.type = array(num.val, T1.type);} | ^T1 {T.type = pointer(T1.type);} E->literal {E.type = char;} | num {E.type = integer;} | id {E.type = lookup(id.val);} | E1 mod E2 {if E1.type == integer && E2.type ==integer then E.type = integer; else E.type =error;} | E1[E2] {if E1.type == array(s, t) && E2.type == integer then E.type = t; else E.type =error;} | E1^ {if E1.type == pointer(t) then E.type = t; else E.type =error;}

Type checking for statements S -> id := E S -> if E then S1 S ->while E do S1 S->S1;S2

Type checking for statements S -> id := E {if id.type == E.type then S.type = void; else S.type = error;} S -> if E then S1 {if E.type == boolean then S.type = S1.type; else S.type = error;} S ->while E do S1 { if E.type == boolean then S.type = S1.type; else S.type = error;} S->S1;S2 {if S1.type == void and S2.type == void then S.type = void; else S.type = type_error; }

Type checking for functions: T->T1->T2 /* function declaration */ {T.type = T1.type ->T2.type} E->E1(E2) /* function call */ {if E1.type == t1.type->t2.type && E2.type == t1.type then T.type = t2.type; else T.type - error; }

Equivalence of type expressions Name equivalence - each type with different name is different structural equivalence - names are replaced by the type expressions they define Example: type link = ^cell; var next : link last : link p: ^cell Is structural equivalence good for C++?

–Other things related to type. coercion: implicit type conversion: –e.g. double x; ….x = 1; overloading: –a function or operator can represent different operations in different contexts. polymorphic functions: –the body of a polymorphic function can be executed with arguments of different types.