System Software Unit-1 (Language Processors) A TOY Compiler

Slides:



Advertisements
Similar presentations
Intermediate Code Generation
Advertisements

C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Compilers and Language Translation
Grammars, Languages and Parse Trees. Language Let V be an alphabet or vocabulary V* is set of all strings over V A language L is a subset of V*, i.e.,
Chapter Chapter Summary Languages and Grammars Finite-State Machines with Output Finite-State Machines with No Output Language Recognition Turing.
UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-
Chapter3: Language Translation issues
Chapter 9 Compilers and Language Translation. The Compilation Process Phase I: Lexical analysis Phase I: Lexical analysis Phase II: Parsing Phase II:
Chapter 2 A Simple Compiler
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Course Revision Contents  Compilers  Compilers Vs Interpreters  Structure of Compiler  Compilation Phases  Compiler Construction Tools  A Simple.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
Compiler1 Chapter V: Compiler Overview: r To study the design and operation of compiler for high-level programming languages. r Contents m Basic compiler.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
Lexical Analysis I Specifying Tokens Lecture 2 CS 4318/5531 Spring 2010 Apan Qasem Texas State University *some slides adopted from Cooper and Torczon.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Review: Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Code optimizer Code generator Symbol.
CPS 506 Comparative Programming Languages Syntax Specification.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
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.
The Functions and Purposes of Translators Syntax (& Semantic) Analysis.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
Parsing and Code Generation Set 24. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program,
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
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.
GRAMMARS & PARSING. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program, referred to as a.
Compilers Computer Symbol Table Output Scanner (lexical analysis)
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 3 – Describing Syntax
Advanced Computer Systems
CS 326 Programming Languages, Concepts and Implementation
A Simple Syntax-Directed Translator
CS510 Compiler Lecture 4.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Chapter 3 – Describing Syntax
What does it mean? Notes from Robert Sebesta Programming Languages
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Compiler Construction
Syntax Analysis Sections :.
Lexical and Syntax Analysis
Review: Compiler Phases:
Compilers B V Sai Aravind (11CS10008).
Lecture 4: Lexical Analysis & Chomsky Hierarchy
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
High-Level Programming Language
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Chapter 10: Compilers and Language Translation
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Visual Programming Languages ICS 539 Icon System Visual Languages & Visual Programming, Chapter 1, Editor Chang, 1990 ICS Department KFUPM Sept. 1,
Presentation transcript:

System Software Unit-1 (Language Processors) A TOY Compiler Prepared By :- Bhavin Dalsaniya MEFGI-MCA Studant

The Front End The front end performs Lexical Analysis Syntax Analysis Semantic Analysis of the source program. Each kind of analysis involves the following functions Determine validity of a source statement Determine the ‘content’ of a source statement Construct the IC of a source statement for use by subsequent analysis functions.

‘content’ word The word ‘content’ has different meaning in laxical,syntax and semantic analysis. In lexical analysis, the content is the lexical class to which each lexical unit belongs. In syntax analysis it is the syntactic structure of a source statement. In semantic analysis the content is the meaning of a statement.

After Analysis of ‘content’ It generates information in form of Tables of information Description of the source statement Subsequent analysis uses this information for its own purpose and either adds information to these tables and description. For example :- syntax analysis uses the information generated by lexical analysis and construct a representation for the syntactic structure of source statement . Semantic analysis uses the information generated by syntax analysis and construct representation for the meaning of the statement. The tables and descriptions at the end of semantic analysis form the IR (Intermediate Representation) of the front end. Its more clear from the following diagram.

Diagram of front end toy compiler Source Program ------------------------------ ||||||||||||| |||||||||||||| Lexical Or Scanning Lexical Errors Symbol table, Constant table, Other tables… Tokens Syntax OR Parsing Syntax Errors Trees Semantic Analysis Semantic Errors ------------------------------ IC IR

1.Lexical Analysis(Scanning) Lexical analysis identifies the lexical units in a source statement. It then classifies the units into different lexical classes. E.g. id’s,constants,reserved id’s etc and enters them into different tables Lexical analysis builds a descriptor, called a token, for each lexical unit. A token contains two fields—class code and number in class. Class code  identifies the class to which a lexical unit belongs. Number in class  the entry number of the lexical unit in the relevant table. We depict a token as Code #no

Example :- i : integer a,b : real The statement a:=b+i; Symbol Table Intermediate Code 1. Convert (Id,#1) to real ,giving (Id,#4) 2. Add (Id,#4) to (Id,#3), giving (Id,#5) 3. Store (Id,#5) in (Id,#2) Id,#2 Op,#5 Id,#3 Op,#3 Id,#1 Op,#10 No Symbol Type Length Address 1 i int 2 a real 3 b 4 i* 5 temp

2.Syntax Analysis(Parsing) Syntax analysis processes the string of tokens built by lexical analysis to determine the statement class, e.g. assignment statement, if statement , etc. It then builds an IC which represents the structure of the statement. The IC is passed to semantic analysis to determine the meaning of the statement A tree form is chosen for IC because a tree can represent the hierarchical structure of a PL statement appropriately. a:= b+i; := real a + a b b i

3.Semantic Analysis Semantic analysis identifies the sequence of actions necessary to implement the meaning of a source statement When semantic analysis determines the meaning of a subtree in the IC,it adds information to a table or adds an action to the sequence of actions. It then modifies the IC to enable further semantic analysis. The analysis ends when the tree has been completely processed.

Example of Semantic Analysis Source statement a:=b+i; No of Analysis Steps :- Add type Right hand side Expression evaluated first in assignment. Before Add , perform Conversion int to real Addition operation and store into temp. temp store into a. Its more clear from the tree shown in front. := A) a, real + b, real i, int := B) a, real + b, real i*, real := C) a, real temp, real

* The Back End The back end performs two task as follows Memory Allocation Code generation Memory Allocation :-memory allocation is a simple task given the presence of the symbol table. The memory requirement of an identifier is computed from its type, length an dimensionality and memory is allocated to it. The address of the memory area is entered in the symbol table.

Conti…

Conti… Code Generation :- code generation uses knowledge of the target architecture.. Knowledge of instruction and addressing modes in the target computer, to select the appropriate instruction. The important issues in code generation are : Determine the places where the intermediate results should be kept. either it is in memory location or in machine register. Determine which instructions should be used for type conversion operation. Determine which addressing modes should be used for accessing variables.

Conti…

Toy Compiler

Programming Language Grammar A language L can be considered to be a collection of valid sentences. Each sentences can be looked upon as a sequence of words , and each word as a sequence of letters or graphic symbols acceptable in L. A Language specified in this manner is known as a “Formal Language”. Terminal Symbol :- The alphabet of L, denoted by the Greek symbol ∑, is a collection of symbol in its character set. We will use lower case letters a , b , c , etc. to denote symbols in ∑. A symbol in the alphabet is known as a terminal symbol (T) of L. The alphabet can be represented using the mathematical notation of a set , e . g ∑={ a ,b , c …..z,0,1,2 …9}

Conti… Here the symbol {, ‘,’ and} are part of the notation . we call them metasymbols to differentiate them from the terminal symbols. Strings :- A string is a finite sequence of symbols . we will represent strings by Greek symbols α β γ etc. α= axy is a string over ∑. The length of a string is the number of symbols in it. Note that absence of any symbol is also a string, the null string €. Concatenation operation combines two strings into single strings.

Conti… Nonterminal symbols :- Productions :- A nonterminal symbol (NT) is the name of a syntax category of a language. E.g noun, verb etc. An NT is written as a single capital letter or as a name enclosed between <….>, e.g A or <Noun>. During grammatical analysis, a nonterminal symbol represents an instance of the category . thus,<Noun> represents a noun. Productions :- A production also called a rewriting rule, is a rule of grammar. A production has the form A nonterminal symbol::= String of Ts and NTs

Conti… Each grammar G defines a language Lg . G contains an NT called the distinguished symbol or start NT of G . unless otherwise specified, we use the symbol S as the distinguished symbol of G. A valid string α of Lg is obtained by using the following procedure Let α=‘S’ While α is not a string of terminal symbols Select an NT appearing in α,say X Replace X by a string appearing on the RHS of a production of X. Grammar Derivation Reduction Parse Tree