Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Semantic Analysis and Symbol Tables
Compiler construction in4020 – lecture 10 Koen Langendoen Delft University of Technology The Netherlands.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Compilation Encapsulation Or: Why Every Component Should Just Do Its Damn Job.
Mooly Sagiv and Roman Manevich School of Computer Science
1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
Lecture 01 - Introduction Eran Yahav 1. 2 Who? Eran Yahav Taub 734 Tel: Monday 13:30-14:30
Lecture 14 – Recap Eran Yahav Thanks to Ohad Shacham (TAU) for some of the slides in this lecture 1
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.
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
Compiler Summary Mooly Sagiv html://
Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Compiler Construction Recap Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
Compiler Construction Semantic Analysis II Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
Compiler Construction Semantic Analysis I Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
Compiler Construction Parsing I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
Semantic Analysis III + Intermediate Representation I.
COP4020 Programming Languages
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.
CSC 338: Compiler design and implementation
Compiler Construction1 COMP Compiler Construction Lecturer: Dr. Arthur Cater Teaching Assistant:
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.
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.
Compiler Construction Dr. Noam Rinetzky and Orr Tamir School of Computer Science Tel Aviv University
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Introduction to Compiling
Introduction CPSC 388 Ellen Walker Hiram College.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
Semantic Analysis II. Messages Please check lecturer notices in the Moodle Appeals  Legitimate: “I don’t have the bug you mentioned…”  Illegitimate:
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
What is a compiler? –A program that reads a program written in one language (source language) and translates it into an equivalent program in another language.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
CSC 8505 Compiler Construction
Compiler Construction Recap. 2 Announcements PA4: extension until end of exam period – Not a single day more, for any reason! PA5: bonus exercise – Will.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
Presented by : A best website designer company. Chapter 1 Introduction Prof Chung. 1.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
CS510 Compiler Lecture 1. Sources Lecture Notes Book 1 : “Compiler construction principles and practice”, Kenneth C. Louden. Book 2 : “Compilers Principles,
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Lecture 9 Symbol Table and Attributed Grammars
Compiler Design (40-414) Main Text Book:
Chapter 1 Introduction.
Introduction to Compiler Construction
Winter Compiler Construction T11 – Recap
Constructing Precedence Table
Chapter 1 Introduction.
Compiler Lecture 1 CS510.
CS 536 / Fall 2017 Introduction to programming languages and compilers
Fall Compiler Principles Lecture 4: Parsing part 3
CS416 Compiler Design lec00-outline September 19, 2018
Introduction CI612 Compiler Design CI612 Compiler Design.
CPSC 388 – Compiler Design and Construction
Lecture 4: Lexical Analysis & Chomsky Hierarchy
Subject: Language Processor
CS416 Compiler Design lec00-outline February 23, 2019
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Presentation transcript:

Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University

2 Exam 31/01/2010 at 9:00 Look for previous exams on website Possible questions Parsing Extend IC Activation records …

3 Generic compiler structure Executable code exe Source text txt Semantic Representation Backend (synthesis) Compiler Frontend (analysis)

4 Executable code exe Source text txt Semantic Representation Backend (synthesis) Compiler Frontend (analysis) IC Program ic x86 executable exe Lexical Analysis Syntax Analysis Parsing ASTSymbol Table etc. Inter. Rep. (IR) Code Generation IC compiler

5 Lexical Analysis Input program text (file) Output sequence of tokens Read input file Identify language keywords Count line numbers Remove whitespaces Report illegal symbols

6 Scanning CLASS,CLASS_ID(Hello),LB,BOOLEAN,ID(state),SEMI … Issues in lexical analysis: Language changes: New keywords, New operators, New meta-language features, e.g., annotations // An example program class Hello { boolean state; static void main(string[] args) { Hello h = new Hello(); boolean s = h.rise(); Library.printb(s); h.setState(false); } boolean rise() { boolean oldState = state; state = true; return oldState; } void setState(boolean newState) { state = newState; } }

7 Tools Automatically generate lexical analyzers Less error prune Many people used these tools Easily updated Easily to port code between developers Lex, Flex, Jlex, JFlex, …

8 Parsing Input A context free grammar A stream of tokens Output An abstract syntax tree or error

9 Parsing and AST CLASS,CLASS_ID(Hello),LB,BOOLEAN,ID(state),SEMI … prog class_list class field_method_list fieldfield_method_list type ID(state) BOOLEAN method field_method_list … … parser uses stream of tokens and generate derivation tree Issues in syntax analysis: Grammars: LL(1), LR(0), SLR(1), LALR(1), LR(1) Building parsers Transition diagram Parse table Running automaton Conflict resolution Write LR grammar for a language Ambiguity

10 Parsing and AST prog class_list class field_method_list fieldfield_method_list type ID(state) BOOLEAN method field_method_list … … Syntax tree built during parsing parser uses stream of token and generate derivation tree CLASS,CLASS_ID(Hello),LB,BOOLEAN,ID(state),SEMI … ProgAST ClassAST classList FieldAST[0] type:BoolType name:state MethodAST[0] MethodAST[1] MethodAST[2] … … methodList fieldList Should know difference between derivation tree and AST Know how to build AST from input

11 FieldsOrMethods ::= Field:field FieldsOrMethods:next {: RESULT = next; RESULT. addField(field); :} | Method:method FieldsOrMethods:next {: RESULT = next; RESULT.addMethod(method); :} | /* empty */ {: RESULT = new FieldsMethods(); :};

12 Tools Automatically generate parsers Mostly LR (LALR(1)) Less error prune Many people used these tools Easily updated Ease the process of switching code between developers YACC, Bison, Cup, …

13 Questions Build an LR grammar for the language Is the following grammar in LR(0), SLR(1), LALR(1), LR(1) ? Build a parser for the grammar Run an input string using your parser

14 Question Is the following grammar is LR(0)? E  E + T E  T T  F F  id F  (E)

15 Answer Add a production S  E$ Construct a finite automaton States are set of items A  

16 S   E$ E   T E   E + T T   F F   i F   (E) E  T  T F  i  i S  E  $ E  E  + T E F  (  E) E   T E   E + T T   F F   i F   (E) ( ( F  (E)  ) F  (E  ) E  E  + T E E  E +  T T   F F   i F   (E) + + E  E + T  T S  E $  $ i i T  F  F F ( T F LR(0)

17 Question Is the following grammar is LR(0)? E  E + T T  T * F E  T T  F F  id F  (E)

18 S   E$ E   T E   E + T T   T * F T   F F   i F   (E) E  T  T  T  * F T F  i  i S  E  $ E  E  + T E F  (  E) E   T E   E + T T   T * F T   F F   i F   (E) ( ( F  (E)  ) F  (E  ) E  E  + T E E  E +  T T   T * F T   F F   i F   (E) + + E  E + T  T  T  * F T S  E $  $ i i T  F  F F ( T F T  T *  F F   i F   (E) i ( T  T * F  F * *

19 Question Is the following grammar is SLR(1)? E  E + T T  T * F E  T T  F F  id F  (E)

20 E  T  T  T  * F E  E + T  T  T  * F T  T *  F F   i F   (E) * * Calculate FOLLOW sets for each non terminal Use the FOLLOW set to break conflicts FOLLOW(E) = {),+,$} S  E$ E  E + T T  T * F E  T T  F F  id F  (E) SLR(1)

21 Semantic analysis Context analysis Does break and continue appear only inside while statement? Scope analysis Every var is predefined No double definitions Bound var usage to its definition Type checking Every expression is well type from the type of its definition Every statement is well type

22 Semantic analysis motivation Syntax analysis is not enough int a; a = “hello”; int a; b = 1; Assigning wrong type Assigning undeclared variable int a; int a; a = 1; Variable re declaration

23 Semantic analysis ProgAST ClassAST classList FieldAST[0] type:BoolType MethodAST[0] MethodAST[1] MethodAST[2] … … methodList fieldList Representing scopes Type-checking Semantic checks SymbolKindType HelloclassHello SymbolKindTypeProperties statefieldbooleaninstance mainmethodstring[]->voidstatic risemethodvoid->booleaninstance setStatemethodboolean->voidinstance SymbolKindType newStateparamint (Program) (Hello) (setState) …

24 Examples of type errors int a; a = true; void foo(int x) { int x; foo(5,7); } 1 < true class A {…} class B extends A { void foo() { A a; B b; b = a; } } argument list doesn’t match formal parameters a is not a subtype of b assigned type doesn’t match declared type relational operator applied to non-int type

25 Type rules E  true : bool E  e1 : intE  e2 : int E  e1 op e2 : int E  false : bool E  int-literal : int E  string-literal : string op  { +, -, /, *, %} E  e1 : intE  e2 : int E  e1 rop e2 : bool rop  {, >=} E  e1 : TE  e2 : T E  e1 rop e2 : bool rop  { ==,!=}

26 Semantic conditions What is checked in compile-time and what is checked in runtime? EventC/R Program execution halts Break/continue inside a while statement Array index within bound In Java the cast statement (A)f is legal In Java method o.m(…) is illegal since m is private

27 Semantic conditions What is checked in compile-time and what is checked in runtime? EventC/R Program execution haltsR (undecidable in general) Break/continue inside a while statement C Array index within boundR (undecidable in general) In Java the cast statement (A)f is legal Depends: if A is sub-type of f then checked during runtime (raising exception), otherwise flagged as an error during compilation In Java method o.m(…) is illegal since m is private C

28 Question – extend IC Support Java override annotation inside comments Annotation is written above method to indicate it overrides a method in superclass Describe the phases in the compiler affected by the change and the changes themselves class A { void rise() {…} } class B extends A { void rise() {…} } class A { void rise() {…} } class B extends A { void ris() {…} } Legal program Illegal program

29 Answer The change affects the lexical analysis, syntax analysis and semantic analysis Does not affect later phases User semantic condition

30 Changes to scanner Add pattern inside comment state patterns Add Java code to action to comments – instead of not returning any token, we now return a token for the annotation boolean override=false; % // { override=false; yybegin(comment); { override=true; } \n { if (override) return new Token(…,override,…) }

31 Changes to parser and AST method  static type name params ‘{‘ mbody ‘}’ | type name params ‘{‘ mbody ‘}’ | OVERRIDE type name params ‘{‘ mbody ‘}’ Add a Boolean flag to the method AST node to indicate that the method is annotated

32 Changes to semantic analysis Suppose we have an override annotation above a method m in class A We check the following semantic conditions 1. class A extends a superclass (otherwise it does not make sense to override a method) 2. Traverse the superclasses of A by going up the class hierarchy until we find the first method m and check that it has the same signature as A.m If we fail to find such a method we report an error

33 Intermediate representation Allows language-independent, machine independent optimizations and transformations Easy to translate from AST Easy to translate to assembly ASTIR Pentium Java bytecode Sparc optimize

34 Translation to IR Accept AST and translate functions into lists of instructions Compute offsets for fields and virtual functions Dispatch vectors Register allocation

35 Translation to IR Question: give the method tables for Rectangle and Square class Shape { boolean isShape() {return true;} boolean isRectangle() {return false;} boolean isSquare() {return false;} double surfaceArea() {…} } class Rectangle extends Shape { double surfaceArea() {…} boolean isRectangle() {return true;} } class Square extends Rectangle { boolean isSquare() {return true;} }

36 Answer Shape_isShape Rectangle_isRectangle Shape_isSqaure Rectangle_surfaceArea Shape_isShape Rectangle_isRectangle Sqaure_isSqaure Rectangle_surfaceArea Method table for rectangleMethod table for square