5-1 5 Compilation  Overview  Compilation phases –syntactic analysis –contextual analysis –code generation  Abstract syntax trees  Case study: Fun language.

Slides:



Advertisements
Similar presentations
CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.
Advertisements

Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00.
CPSC 388 – Compiler Design and Construction
8 VM code generation Aspects of code generation Address allocation
Intermediate Code Generation
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 Languages and Compilers (SProg og Oversættere) Code Generation.
1 Compiler Construction Intermediate Code Generation.
1 JavaCUP JavaCUP (Construct Useful Parser) is a parser generator Produce a parser written in java, itself is also written in Java; There are many parser.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
6-1 6 Syntactic analysis  Aspects of syntactic analysis  Tokens  Lexer  Parser  Applications of syntactic analysis  Compiler generation tool ANTLR.
Code Generation Mooly Sagiv html:// Chapter 4.
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
Chapter 2 A Simple Compiler
Programming Languages 2nd edition Tucker and Noonan Chapter 6 Types I was eventually persuaded of the need to design programming notations so as to maximize.
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.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
Compilation (Chapter 3) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
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.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
7-1 7 Contextual analysis  Aspects of contextual analysis  Scope checking  Type checking  Case study: Fun contextual analyser  Representing types.
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
4-1 4 Interpretation  Overview  Virtual machine interpretation  Case study: SVM  Case study: SVM interpreter in Java Programming Languages 3 © 2012.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Contextual Analysis (Chapter 5) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
 Fall Chart 2  Translators and Compilers  Textbook o Programming Language Processors in Java, Authors: David A. Watts & Deryck F. Brown, 2000,
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.
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.
1 Languages and Compilers (SProg og Oversættere) Semantic Analysis.
MiniJava Compiler A multi-back-end JIT compiler of Java.
1 Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer.
Lecture 9 Symbol Table and Attributed Grammars
Chapter 3 – Describing Syntax
Compiler Design (40-414) Main Text Book:
Project 1: Part b SPECIFICATION
A Simple Syntax-Directed Translator
Constructing Precedence Table
Semantic Analysis with Emphasis on Name Analysis
Overview of Compilation The Compiler BACK End
Languages and Compilers (SProg og Oversættere)
CS 536 / Fall 2017 Introduction to programming languages and compilers
Basic Program Analysis: AST
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSE401 Introduction to Compiler Construction
Overview of Compilation The Compiler BACK End
Syntax-Directed Translation
Compiler design.
Course Overview PART I: overview material PART II: inside a compiler
Assignments and Procs w/Params
Lecture 18 Compilers and Language Translation (S&G, ch. 9)
Presentation transcript:

5-1 5 Compilation  Overview  Compilation phases –syntactic analysis –contextual analysis –code generation  Abstract syntax trees  Case study: Fun language and compiler Programming Languages 3 © 2012 David A Watt, University of Glasgow

5-2 Overview  Recall: A compiler translates a source program to object code, provided that it conforms to the source language’s syntax and scope/type rules.  This suggests a decomposition of the compiler into three phases: –syntactic analysis –contextual analysis –code generation.

5-3 Compilation phases (1)  Syntactic analysis: Parse the source program to check whether it is well-formed, and to determine its phrase structure, in accordance with the source language’s syntax.  Contextual analysis: Analyze the parsed program to check whether it conforms to the source language’s scope rules and type rules.  Code generation: Translate the parsed program to object code, in accordance with the source language’s semantics.

5-4 Compilation phases (2)  Data flow between phases: code generation object program source program syntactic analysis AST syntactic errors contextual analysis AST scope/type errors  An AST (abstract syntax tree) is a convenient way to represent a source program after syntactic analysis (see later for details).

5-5 Case study: Fun language (1)  Fun is a simple imperative language.  A Fun program declares some global variables and some procedures/functions, always including a procedure named main().  A Fun procedure/function may have a single parameter. It may also declare local variables. A function returns a result.  Fun has two data types, bool and int.  Fun commands include assignments, procedure calls, if-commands, while-commands, and sequential commands.

5-6 Case study: Fun language (2)  Sample Fun program: func int fac (int n): # returns n! int f = 1 while n > 1: f = f*n n = n-1. return f. proc main (): int num = read() write(num) write(fac(num)).  Fun programs are free-format: spaces, tabs, and EOLs (ends-of-lines) are not significant.

5-7 Case study: Fun language (3)  Fun syntax (extracts): prog=var-decl * proc-decl + eof var-decl=type id ‘ = ’ expr type=‘ bool ’ |‘ int ’ com=id ‘ = ’ expr |‘ if ’ expr ‘ : ’ seq-com ‘. ’ |… seq-com=com *

5-8 Case study: Fun language (4)  Fun syntax (continued): expr=sec-expr … sec-expr=prim-expr ( ( ‘ + ’ | ‘ - ’ | ‘ * ’ | ‘ / ’ ) prim-expr ) * prim-expr=num |id |‘ ( ’ expr ‘ ) ’ |…  For a full description, see Fun Specification (available from the Moodle page). primary- expression secondary- expression expression

5-9 Case study: Fun compiler (1)  The Fun compiler generates SVM code. It is expressed in Java: Java Fun → SVM  This contains the following classes: –syntactic analyser ( FunLexer, FunParser ) –contextual analyser ( FunChecker ) –code generator ( FunEncoder ).

5-10 Case study: Fun compiler (2)  The compiler calls each of these in turn: –The syntactic analyser lexes and parses the source program, printing any error messages, and generates an AST. Then the AST is printed. –The contextual analyser performs scope/type checking, printing any error messages. –The code generator emits object code into the SVM code store. Then the object code is printed.  Compilation is terminated after syntactic or contextual analysis if any errors are detected.

5-11 Case study: Fun driver  The driver FunRun does the following: –It compiles the source program into an SVM object program. –If no errors are detected, it calls the SVM interpreter to run the object program.  Of course, a real compiler would save the object program in a file.

5-12 Abstract syntax trees  An abstract syntax tree (AST) is a convenient way to represent a source program’s phrase structure.  Structure of an AST: –Each leaf node represents an identifier or literal. –Each internal node corresponds to a source language construct (e.g., a variable declaration or while- command). The internal node’s subtrees represent the parts of that construct.  ASTs are much more compact than syntax trees (§1).

5-13 Example: AST for Fun expression  AST for expression ‘ (x+13)*(y-z) ’:  Note: The AST makes no distinction between exprs, sec-exprs, etc.: they are all just expressions. This subtree is the right operand. This subtree is the left operand. ID ‘y’ MINUS TIMES ID ‘z’ ID ‘x’ PLUS NUM ‘13’

5-14 Example: AST for Fun command  AST for ‘ if n>0: n = n-1 write(n). ’: This subtree is the if- body. This subtree is the if- condition.  Note: The AST makes no distinction between coms and seq-coms: they are all just commands. ID ‘n’ MINUS ASSN IF SEQ ID ‘n’ GT NUM ‘0’ NUM ‘1’ ID ‘write’ ID ‘n’ PROCCALL ID ‘n’

5-15 Case study: summary of Fun ASTs (1)  ASTs for Fun commands (com): … SEQ com IF exprcom IFELSE exprcom 1 com 2 WHILE exprcom ASSN expr ID ‘…’ PROCCALL expr ID ‘…’ subtree Key:

5-16 Case study: summary of Fun ASTs (2)  ASTs for Fun expressions (expr): FALSETRUENUM ‘…’ ID ‘…’ EQ expr 1 expr 2 LT expr 1 expr 2 GT expr 1 expr 2 NOT expr PLUS expr 1 expr 2 MINUS expr 1 expr 2 TIMES expr 1 expr 2 DIV expr 1 expr 2 FUNCCALL ID ‘…’ expr

5-17 Case study: summary of Fun ASTs (3)  AST for Fun programs: PROG var- decl …proc- decl …

5-18 Case study: summary of Fun ASTs (4)  ASTs for Fun variable declarations (var-decl):  ASTs for Fun types (type): BOOL INT VAR type ID ‘…’ expr

5-19 Case study: summary of Fun ASTs (5)  ASTs for Fun procedure declarations (proc-decl): FORMAL type ID ‘…’  ASTs for Fun formal parameters (formal): NOFORMAL PROC ID ‘…’ formal var- decl … com FUNC type ID ‘…’ formal var- decl … com expr

5-20 Example: Fun compilation (1)  Source program: int n = 15 # pointless program proc main (): while n > 1: n = n/2..

5-21 Example: Fun compilation (2)  AST after syntactic analysis (slightly simplified): PROG NOFORMAL DIV ASSN PROC WHILE GT VAR ID ‘n’ INT ‘int’ ID ‘n’ NUM ‘1’ NUM ‘2’ ID ‘n’ NUM ‘15’ ID ‘n’ ID ‘main’

5-22 Example: Fun compilation (3)  AST after contextual analysis: Type table ‘n’ INT ‘main’ VOID → VOID :BOOL :INT PROG DIV ASSN PROC WHILE GT VAR ID ‘n’ INT ‘int’ ID ‘n’ NUM ‘1’ NUM ‘2’ ID ‘n’ NUM ‘15’ ID ‘n’ ID ‘main’ NOFORMAL

5-23 Example: Fun compilation (3)  SVM object code after code generation: Address table ‘n’0 (global) ‘main’7 (code) 0: 3: 6: 7: 10: 13: 14: 17: 20: 23: 24: 27: 30: LOADLIT 15 CALL 7 HALT LOADG 0 LOADLIT 1 COMPGT JUMPF 30 LOADG 0 LOADLIT 2 DIV STOREG 0 JUMP 7 RETURN 0