CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Semantic Analysis and Symbol Tables
Abstract Syntax Mooly Sagiv html:// 1.
Chapter 8 Intermediate Code Generation. Intermediate languages: Syntax trees, three-address code, quadruples. Types of Three – Address Statements: x :=
1 Compiler Construction Intermediate Code Generation.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #10, Feb. 14, 2007 Modified sets of item construction Rules for building LR parse tables.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
Recap Mooly Sagiv. Outline Subjects Studied Questions & Answers.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Chapter 2 Chang Chi-Chung Lexical Analyzer The tasks of the lexical analyzer:  Remove white space and comments  Encode constants as tokens.
Environments and Evaluation
Compiler Summary Mooly Sagiv html://
Chapter 2 Chang Chi-Chung Lexical Analyzer The tasks of the lexical analyzer:  Remove white space and comments  Encode constants as tokens.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
Chapter 2 A Simple Compiler
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
Semantic Analysis CS 671 February 5, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens –e.g.: main$
Semantic Analysis (Generating An AST) CS 471 September 26, 2007.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
1 Top Down Parsing. CS 412/413 Spring 2008Introduction to Compilers2 Outline Top-down parsing SLL(1) grammars Transforming a grammar into SLL(1) form.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
Introduction to Code Generation and Intermediate Representations
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
Compiler Principles Fall Compiler Principles Lecture 6: Parsing part 5 Roman Manevich Ben-Gurion University.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Semantic Analysis II. Messages Please check lecturer notices in the Moodle Appeals  Legitimate: “I don’t have the bug you mentioned…”  Illegitimate:
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Syntax-Directed Definitions CS375 Compilers. UT-CS. 1.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 6: LR grammars and automatic parser generators.
Lecture 5: LR Parsing CS 540 George Mason University.
Scope of Variable. 2 Scope and visibility Scope (visibility) of identifier = portion of program where identifier can be referred to Lexical scope = textual.
Lecture 9 Symbol Table and Attributed Grammars
Announcements/Reading
Chapter 3 – Describing Syntax
A Simple Syntax-Directed Translator
Constructing Precedence Table
Semantic Analysis with Emphasis on Name Analysis
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
CS 536 / Fall 2017 Introduction to programming languages and compilers
Basic Program Analysis: AST
CS 3304 Comparative Languages
Mini Language Interpreter Programming Languages (CS 550)
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSE401 Introduction to Compiler Construction
Syntax-Directed Translation
The Recursive Descent Algorithm
Presentation transcript:

CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 2 Outline Semantic actions in shift-reduce parsers Semantic analysis Type checking Symbol tables Using symbol tables for analysis

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 3 Administration Programming Assignment 1 due Monday at beginning of class How to submit: see CS412 web site

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 4 Review Abstract syntax tree (AST) constructed bottom up during parsing AST construction done by semantic actions attached to the corresponding parsing code Non-terminals, terminals have type -- type of corresponding object in AST Java option: use subtypes (subclasses) for different productions in grammar: If, Assign  Stmt

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 5 Review: top-down Top-down (recursive descent) parser: –construct AST node at return from parsing routine, using AST nodes produced by symbol on the RHS of production –parsing routines return object of type corresponding to type of non-terminal –AST construction and parsing code are interspersed

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 6 Review: bottom-up Semantic actions are attached to grammar statements E.g. CUP: attach Java statement to production non terminal Expr expr;... expr ::= expr:e1 PLUS expr:e2 {: RESULT = new Add(e1,e2); :} Semantic action executed when parser reduces a production semantic action grammar production

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 7 Actions in S-R parser Shift-reduce parser has –action table with shift and reduce actions –stack with pairs in it Stack entries augmented with result of semantic actions If reduce by X   –pop , bind variables –Execute semantic action –push  X, goto(X), RESULT  terminal symbols state action table goto table non-terminal symbols

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 8 Structure of a Compiler class Compiler { Code compile() throws CompileError { Lexer l = new Lexer(input); Parser p = new Parser(l); AST tree = p.parse(); // calls l.getToken() to read tokens …semantic analysis, IR gen... Code = IR.emitCode(); }

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 9 Thread of Control Compiler.compile Parser.parse Lexer.getToken Lexer Parser InputStream.read InputStream characters tokens AST easier to make re-entrant

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 10 Semantic Analysis Source code lexical analysis parsing semantic analysis tokens abstract syntax tree valid programs: decorated AST semantic errors lexical errors syntax errors

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 11 Goals of Semantic Analysis Find all possible remaining errors that would make program invalid –undefined variables, types –type errors that can be caught statically Figure out useful information for later compiler phases –types of all expressions –What if we can’t figure out type? Don’t need to do extra work

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 12 Recursive semantic checking Program is tree, so... –recursively traverse tree, checking each component –traversal routine returns information about node checked class Add extends Expr { Expr e1, e2; Type typeCheck() { Type t1 = e1.typeCheck(), t2 = e2.typeCheck(); if (t1 == Int && t2 == Int) return Int; else throw new TypeCheckError(“+”); }

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 13 Decorating the tree How to remember expression type ? One approach: record in the node abstract class Expr { protected Type type = null; public Type typeCheck(); } class Add extends Expr { Type typeCheck() { Type t1 = e1.typeCheck(), t2 = e2.typeCheck(); if (t1 == Int && t2 == Int) { type = Int; return Int; } return (); else throw new TypeCheckError(“+”); }

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 14 Context is needed class Id extends Expr { String name; Type typeCheck() { return ? } Need a symbol table that keeps track of all identifiers in scope

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 15 Symbol table Can write formally as set of identifier : type pairs: { x: int, y: array[string] } { int i, n =...; for (i = 0; i<n; i++) { boolean b =... } { i: int, n: int } { i: int, n: int, b: boolean }

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 16 Specification Symbol table maps identifiers to types class SymTab { Type lookup(String id)... void add(String id, Type binding)... }

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 17 Using the symbol table Symbol table is argument to all checking routines class Id extends Expr { String name; Type typeCheck(SymTab s) { try { return s.lookup(name); } catch (NotFound exc) { throw new UndefinedIdentifier(this); }

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 18 Propagation of symbol table class Add extends Expr { Expr e1, e2; Type typeCheck(SymTab s) { Type t1 = e1.typeCheck(s), t2 = e2.typeCheck(s); if (t1 == Int && t2 == Int) return Int; else throw new TypeCheckError(“+”); } Same variables in scope -- same symbol table used

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 19 Adding entries Java, Iota: statement may declare new variables. { a = b; int x = 2; a = a + x } Suppose { stmt 1 ; stmt 2 ; stmt 3... } represented by AST nodes: abstract class Stmt { … } class Block { Vector/*Stmt*/ stmts; … } And declarations are a kind of statement: class Decl extends Stmt { String id; TypeExpr typeExpr;...

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 20 A stab at adding entries class Block { Vector stmts; Type typeCheck(SymTab s) { Type t; for (int i = 0; i < stmts.length(); i++) { t = stmts.typeCheck(s); if (s instanceof Decl) s.add(Decl.id, Decl.typeExpr.evaluate()); } return t; } } Does it work?

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 21 Must be able to restore ST { int x = 5; { int y = 1; } x = y;// should be illegal! } scope of y

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 22 Handling declarations class Block { Vector stmts; Type typeCheck(SymTab s) { Type t; SymTab s1 = s.clone(); for (int i = 0; i < stmts.length(); i++) { t = stmts.typeCheck(s1); if (s1 instanceof Decl) s1.add(Decl.id, Decl.typeExpr.evaluate()); } return t; } Declarations added in block (to s1) don’t affect code after the block

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 23 Storing Symbol Tables Compiler constructs many symbol tables during static checking Symbol tables may keep track of more than just variables: types, break & continue labels Top-level symbol table contains global variables, types Nested scopes result in extended symbol tables containing add’l definitions for those scopes.

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 24 How to implement ST? Three operations: Object lookup(String name); void add (String name, Object type); SymTab clone(); // expensive? Or two operations: Object lookup(String name); SymTab add (String, Object); // expensive?

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 25 Impl #1: Linked list of tables class SymTab { SymTab parent; HashMap table; Object lookup(String id) { if (null == table.get(id))return table.get(id); else return parent.get(id); } void add(String id, Object t) { table.add(id,t); } SymTab clone() { return new SymTab(this); } }

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 26 Impl #2: Binary trees Discussed in Appel Ch. 5 Implements the two-operation interface –non-destructive add so no cloning is needed –O(lg n) performance Object lookup(String name); SymTab add (String, Object);

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 27 Structuring Analysis Analysis structured as a traversal of AST Technique used in lecture: recursion using methods of AST node objects -- object-oriented style class Add { Type typeCheck(SymTab s) { Type t1 = e1.typeCheck(s), t2 = e2.typeCheck(s); if (t1 == Int && t2 == Int) return Int; else throw new TypeCheckError(“+”); }}

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 28 Phases There will be several more compiler phases like typeCheck, including –constant folding –translation to intermediate code –optimization –final code generation Object-oriented style: each phase is a method in AST node objects Weakness: phase code spread all over

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 29 Separating Syntax, Impl. Can write each traversal in a single method Type typeCheck(Node n, SymTab s) { if (n instanceof Add) { Add a = (Add) n; Type t1 = typeCheck(a.e1, s), t2 = typeCheck(a.e2, s); if (t1 == Int && t2 == Int) return Int; else throw new TypeCheckError(“+”); } else if (n instanceof Id) { Id id = (Id)n; return s.lookup(id.name); … Now, code for a given node spread all over!

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 30 Modularity Conflict Two orthogonal organizing principles: node types and phases (rows or columns) typeCheck foldConst codeGen Addxxx Numxxx Idxxx Stmtxxx types phases

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 31 Which is better? Neither completely satisfactory Both involve repetitive code –modularity by objects: different methods share basic traversal code -- boilerplate code –modularity by traversals: lots of boilerplate: if (n instanceof Add) { Add a = (Add) n; …} else if (n instanceof Id) { Id x = (Id) n; … } else … Why is this bad?

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 32 Summary Semantic actions can be attached to shift-reduce parser conveniently Semantic analysis: traversal of AST Symbol tables needed to provide context during traversal Traversals can be modularized differently Read Appel, Ch. 4 & 5