CS 536 / Fall 2017 Introduction to programming languages and compilers

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Semantic Analysis and Symbol Tables
CS 31003: Compilers Introduction to Phases of Compiler.
Control Flow Analysis (Chapter 7) Mooly Sagiv (with Contributions by Hanne Riis Nielson)
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
Compiler Construction
Compiler design Computer Science Rensselaer Polytechnic Lecture 1.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
CPSC 388 – Compiler Design and Construction Lecture: MWF 11:00am-12:20pm, Room 106 Colton.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
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.
Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.
D. M. Akbar Hussain: Department of Software & Media Technology 1 Compiler is tool: which translate notations from one system to another, usually from source.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
1 Compiler 3.2 A Level computing. 2 So, Sir what is a compiler?
Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
IN LINE FUNCTION AND MACRO Macro is processed at precompilation time. An Inline function is processed at compilation time. Example : let us consider this.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
INTRODUCTION TO COMPILERS(cond….) Prepared By: Mayank Varshney(04CS3019)
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
CSC 4181 Compiler Construction
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
ICS312 Introduction to Compilers Set 23. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
1 Compiler Construction Vana Doufexi office CS dept.
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
Chapter 1. Introduction.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lecture 9 Symbol Table and Attributed Grammars
Compiler Design (40-414) Main Text Book:
PRINCIPLES OF COMPILER DESIGN
Introduction to Compiler Construction
CS 3304 Comparative Languages
A Simple Syntax-Directed Translator
Compiler Construction (CS-636)
Finite-State Machines (FSMs)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Overview of Compilation The Compiler BACK End
Finite-State Machines (FSMs)
-by Nisarg Vasavada (Compiled*)
课程名 编译原理 Compiling Techniques
Chapter 1: Introduction to Compiling (Cont.)
Compiler Lecture 1 CS510.
Compiler Construction
Introduction to Compiler Construction
Basic Program Analysis: AST
Course supervisor: Lubna Siddiqui
Lecture 2: General Structure of a Compiler
Compiler 薛智文 TH 6 7 8, DTH Spring.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSE401 Introduction to Compiler Construction
Overview of Compilation The Compiler BACK End
Compilers B V Sai Aravind (11CS10008).
Compiler 薛智文 TH 6 7 8, DTH Spring.
Compiler design.
Introduction to Compiler Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Construction
Compiler 薛智文 M 2 3 4, DTH Spring.
Introduction to Compiler Construction
Presentation transcript:

CS 536 / Fall 2017 Introduction to programming languages and compilers Aws Albarghouthi aws@cs.wisc.edu

About me PhD at University of Toronto Joined University of Wisconsin in 2015 Part of madPL group Program verification Program synthesis http://pages.cs.wisc.edu/~aws/

About the course We will study compilers We will understand how they work We will build a full compiler We will have fun

Course Mechanics Home page: http://cs.wisc.edu/~aws/courses/cs536-f17/ Workload: 6 Programs (40% = 5% + 7% + 7% + 7% + 7% + 7%) 10 short homeworks (20%) 2 exams (midterm: 20% + final: 20%)

A compiler is a recognizer of language S a translator from S to T a program in language H

front end = recognize source code S; map S to IR IR = intermediate representation back end = map IR to T Executing the T program produces the same result as executing the S program?

Phases of a compiler P2 P3 P1 P4, P5 front end back end P6 Symbol table P4, P5 front end back end P6

Scanner (P2) Input: characters from source program Output: sequence of tokens Actions: group chars into lexemes (tokens) Identify and ignore whitespace, comments, etc. Error checking: bad characters such as ^ unterminated strings, e.g., “Hello int literals that are too large

a = 2 * b + abs(-71) Example scanner ident (a) asgn int lit (2) times ident (b) plus ident (abs) lparens int lit (71) rparens minus Whitespace (spaces, tabs, and newlines) filtered out a = 2 *b+ abs ( - 71) The scanner’s output is still the sequence ident (a) asgn int lit (2) times ident (b) plus ident (abs) lparens int lit (71) rparens minus

Parser (P3) Input: sequence of tokens from the scanner Output: AST (abstract syntax tree) Actions: groups tokens into sentences Error checking: syntax errors, e.g., x = y *= 5 (possibly) static semantic errors, e.g., use of undeclared variables

Semantic analyzer (P4,P5) Input: AST Output: annotated AST Actions: does more static semantic checks Name analysis process declarations and uses of variables enforces scope Type checking checks types augments AST w/ types

Semantic analyzer (P4,P5) Scope example: … { int i = 4; i++; } i = 5; out of scope

Intermediate code generation Input: annotated AST (assumes no errors) Output: intermediate representation (IR) e.g., 3-address code instructions have 3 operands at most easy to generate from AST 1 instr per AST internal node

Phases of a compiler P2 P3 P1 P4, P5 front end back end P6 Symbol table P4, P5 front end back end P6

a = 2 * b + abs(-71) Example scanner parser ident (a) asgn int lit (2) times ident (b) plus ident (abs) lparens int lit (71) rparens minus parser

Example (cont’d) semantic analyzer Symbol table a var int b var int abs fun int->int

Example (cont’d) code generation tmp1 = 0 - 71 move tmp1 param1 call abs move ret1 tmp2 tmp3 = 2*b tmp4 = tmp3 + tmp2 a = tmp4 code generation

Optimizer Input: IR Output: optimized IR Actions: Improve code make it run faster; make it smaller several passes: local and global optimization more time spent in compilation; less time in execution

Code generator (~P6) Input: IR from optimizer Output: target code This slide does not exactly match what will be done in Program 6. In CS 536, we are building a simple compiler that does not have an optimization phase. Thus, instead of translating the (annotated) AST to intermediate code and then translating the intermediate code to target code, P6 goes directly from the (annotated) AST to target code

Symbol table (P1) Compiler keeps track of names in semantic analyzer — both name analysis and type checking code generation — offsets into stack optimizer — def-use info P1: implement symbol table

Symbol table Block-structured language Java, C, C++ Ideas: nested visibility of names (no access to a variable out of scope) easy to tell which def of a name applies (nearest definition) lifetime of data is bound to scope

Symbol table int x, y; void A() { double x, z; C(x, y, z) } void B() { block structure: need symbol table with nesting int x, y; void A() { double x, z; C(x, y, z) } void B() { C(x, y, z); implement as list of hashtables