Week 6(10.7): The TINY sample language and it ’ s compiler The TINY + extension of TINY Week 7 and 8(10.14 and 10.21): The lexical of TINY + Implement.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Chapter 8 ICS 412. Code Generation Final phase of a compiler construction. It generates executable code for a target machine. A compiler may instead generate.
CS 31003: Compilers Introduction to Phases of Compiler.
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.
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
Yu-Chen Kuo1 Chapter 1 Introduction to Compiling.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Compiler Summary Mooly Sagiv html://
Compiler Construction1 A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University First Semester 2009/2010.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Syntax and Semantics Structure of programming languages.
INTRODUCTION TO COMPUTING CHAPTER NO. 06. Compilers and Language Translation Introduction The Compilation Process Phase 1 – Lexical Analysis Phase 2 –
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.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
The TINY sample language and it’s compiler
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Context-Free Grammars and Parsing
D. M. Akbar Hussain: Department of Software & Media Technology 1 Compiler is tool: which translate notations from one system to another, usually from source.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
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.
Syntax and Semantics Structure of programming languages.
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.
Introduction to Code Generation and Intermediate Representations
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
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.
. 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.
Introduction to Compiling
Chapter 1 Introduction Major Data Structures in Compiler
INTRODUCTION TO COMPILERS(cond….) Prepared By: Mayank Varshney(04CS3019)
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.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
1.3 Analysis And Synthesis OF LP Language Processor = Analysis of Source Program + Synthesis of Target Program. 1.
 Fall Chart 2  Translators and Compilers  Textbook o Programming Language Processors in Java, Authors: David A. Watts & Deryck F. Brown, 2000,
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.
Code Generation How to produce intermediate or target code.
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.
©SoftMoore ConsultingSlide 1 Structure of Compilers.
Sung-Dong Kim Dept. of Computer Engineering, Hansung University
CS510 Compiler Lecture 1. Sources Lecture Notes Book 1 : “Compiler construction principles and practice”, Kenneth C. Louden. Book 2 : “Compilers Principles,
Objective of the course Understanding the fundamentals of the compilation technique Assist you in writing you own compiler (or any part of compiler)
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
Chapter 1. Introduction.
Lecture 9 Symbol Table and Attributed Grammars
System Software Theory (5KS03).
Compiler Design (40-414) Main Text Book:
PRINCIPLES OF COMPILER DESIGN
CS510 Compiler Lecture 1.
Introduction to Compiler Construction
A Simple Syntax-Directed Translator
Chapter 3 Context-Free Grammar and Parsing
Compiler Construction (CS-636)
课程名 编译原理 Compiling Techniques
Compiler Lecture 1 CS510.
Compiler Designs and Constructions
CPSC 388 – Compiler Design and Construction
CSE401 Introduction to Compiler Construction
Compilers B V Sai Aravind (11CS10008).
Subject: Language Processor
COMPILER CONSTRUCTION
Presentation transcript:

Week 6(10.7): The TINY sample language and it ’ s compiler The TINY + extension of TINY Week 7 and 8(10.14 and 10.21): The lexical of TINY + Implement of TINY + scanner To be determined Syntax of TINY + and Implement the parser of TINY + Semantic of TINY + Implement of semantic analyzer and intermediate code generator for TINY +

Time Week 6,7,8 Tuesday: section 3,4 venue Week 6,7,8 Computer Bilingual: B3-230 Network Engineering: B3-231

Due date Experiment 1 Implementing a Scanner for TINY+: by the end of week 10(11.09)

The TINY sample language and it ’ s compiler

1 The TINY Sample Language and Compiler 2 Implementation of a TINY Scanner 3 Syntax of the TINY Language 4 A Recursive-Descent Parser for TINY 5 A Semantic Analyzer for the TINY Language 6 A runtime environment for the TINY Language

1 The TINY Sample Language and Compiler

TINY Compiler The construction of TINY compiler uses the techniques studied in each chapter and show how all the parts of a compiler fit together. Using experiment language TINY as source language Using TM(the assembly language for a simple hypothetical processor) as the target language Compiler is written in C

The construction of compiler for a concrete language Familiar with the source language (lexical, syntax, semantic) Familiar with the target language (addressing mode, the number of register, data representation ) Determine the structure of compiler

1.1 The TINY Language Syntax description A program is a sequence of statements separated by semicolons Declarations No procedures and no declarations Data Type All variables are integer variables, and variables are declared by assigning values to them

Statement Two control statements: if-statement and repeat-statement, read and write statements Expression Boolean and integer arithmetic expressions Comment Comments are allowed within curly bracket

{sample program in TINY language- computes factorial} read x; { input an integer } if 0<x then { don ’ t compute if x<=0 } fact:=1; repeat fact := fact*x; x := x-1 until x=0; write fact{output factorial of x} end Example

1.2 The TINY Compiler Components of the TINY Compiler It has the following components: Scanner, parser, semantic analyzer, and code generator phases together with a symbol table Following components are absent No optimization phases and separate error handler or literal table

Structure of TINY Compiler Four-pass compiler The first pass consists of the scanner and parser, which construct the syntax tree; The second and third passes undertake semantic analysis The second pass constructs the symbol table The third pass performs type checking The forth pass is the code generator

The code that drives these passes: syntaxTree=parse(); buildSymtab(syntaxTree); typeCheck(syntaxTree); codeGen(syntaxTree,codefile);

2. Implementation of a TINY Scanner

2.1 Implementing a Scanner for the Sample Language TINY The lexical structure of TINY Reserved WordsSpecial SymbolsOther if then else end repeat until read write + - * / = < ( ) ; := number identifier

Construct a DFA for the scanner directly

Explanation:  All accepting states are collected into one state “ DONE ”, the different token recognized is saved in a variable  Construct a table of reserved words, reserved words are considered only after an identifier has been recognized, and then to look up the identifier in the table  The implementation of the DFA uses the doubly nested case analysis

3 Syntax of the TINY Language

3.1 A Context-Free Grammar for TINY program-> stmt-seq stmt->seq-> stmt-seq;stmt | stmt stmt-> if-stmt|repeat-stmt|assign- stmt|read-stmt | write-stmt if-stmt ->if exp then stmt-seq end | if exp then stmt-seq else stmt-seq end repeat-stmt->repeat stmt-seq until exp assign-stmt-> id:= exp read-stmt -> read id write-stmt -> write exp

exp-> simp-exp cop simp-exp |simp-exp cop-> < | = simp-exp-> simp-exp addop term |term term-> term mulop factor | factor factor-> (exp) |num |id

3.2 Syntax Tree Structure for the TINY Compiler Basic syntax tree structures 1 A sequence of statements 2 An if-statement ; ; s ss syntax tree of s;s;s seq ss s

3 A repeat-statement 4 An assign-statement 5 A write-statement 6 An operator-expression

{sample program in TINY language- computes factorial} read x;{input an integer} if 0<x then {don ’ t compute if x<=0} fact:=1; repeat fact:=fact*x; x:=x-1 until x=0; write fact{output factorial of x} end

4 A Recursive-Descent Parser for TINY

TINY Grammar in EBNF … stmt->seq -> stmt {;stmt} exp -> simp-exp [cop simp-exp] simp-exp -> term {addop term} term -> factor {mulop factor} ……

5 A Semantic Analyzer for the TINY Language

We separate the discussion of the TINY semantic analyzer into two parts The structure of the symbol table and its associated operations The operations of the semantic analyzer itself, including the construction of the symbol table and type checking

5.1 A Symbol Table for TINY What information needs to be held in the table  It does not need to contain scope information, and data type  It contains locations for variables for code generation  It also contains a cross-reference listing of line numbers where variables are accessed

For example 5:read x; 6:if x>0 then 7:fact:=1; 8:repeat 9:fact:=fact*x; 10:x:=x-1 11:until x=0; 12:write fact 13:end The symbol table for this program Variable NameLocationLine numbers x05,6,9,10,10,11 fact17,9,9,12

5.2 A Semantic Analyzer for TINY The symbol table is an inherited attribute, while the data type of an expression is a synthesized attribute Thus, the symbol table can be built by a preorder traversal of the syntax tree, and type checking can be performed by a postorder traversal Each is processed in a separate pass over the syntax tree

6 A Runtime Environment for the TINY Language

The structure of a runtime environment for the TINY language Place the variables in absolute addresses at the bottom end of program memory Allocate the temporary(dynamic storage during expression evaluation) stack at the top end

temp1 temp2 temp3 free memory w z y x bottom of memory top of memory top of temp stack Runtime environment of TINY