×
In order to succeed, we must first believe that we can.
--Your friends at LectureNotes
Close

Compiler Design

by Siddharth Rajput
Type: NoteViews: 4Uploaded: 30 days agoAdd to Favourite

Order Printed Copy
0 User(s)

Share it with your friends

Suggested Materials

Leave your Comments

Contributors

UNIT VI: COMPILER DESIGN LAST SIX YEAR'S GATE ANALYSIS Number of questions 4 3 Marks 1 Marks 2 2 Total number of questions 1 0 2015 2014 2013 2012 2011 2010 Concepts on which questions were asked in the previous six years Chapter 6.indd 285 Year Concepts 2015 Control flow graph, Compiler phases, Parsers, LL(1) and LR(1) Grammar, Shift reducing parser 2014 Parsing, Static and dynamic memory allocation 2013 Parser, Languages 2012 Parsing, Grammar, Decidability 2011 Decidability, Languages 2010 Grammar, Compiler keywords 4/9/2015 9:59:32 AM
Chapter 6.indd 286 4/9/2015 9:59:32 AM
CHAPTER 6 COMPILER DESIGN Syllabus: Compiler design: Lexical analysis, Parsing, Syntax-directed translation, Runtime environments, Intermediate and target code generation, Basics of code optimization. 6.1  INTRODUCTION 6.2  COMPILERS AND INTERPRETERS Computer understands programs written in machine language. Building program in machine language is a tedious and an error-prone task for human. So the programs are written in high-level languages which are easily understood by human. A compiler is a program that converts high-level program into low-level machine language that is understood by machine. This subject deals with how a compiler is designed and organized. While writing a compiler, the compilation process is divided into various phases. These phases operate in sequence; each phase takes input from the previous phase and provides output to the next phase. 6.2.1  Compiler Chapter 6.indd 287 A compiler is a special program that reads statements written in a language (called source language) and then converts them into another language (called target language). In other words, a compiler is a program which translates statements written in high-level language (i.e. Java, C#, Visual Basic) into machine-level language (Fig. 6.1). In the process of translation, a compiler also checks for errors if any. 4/9/2015 9:59:32 AM
288     Chapter 6:  Compiler Design  6.2.3.1 Lexical Analyzer High level language (HLL) Compiler Run Low level language Input Output Figure 6.1 |   Compiler. 6.2.2  Interpreter An interpreter takes a single instruction as input and converts it into machine-level language and shows errors in the statement if any (Fig. 6.2). It requires less memory than a compiler, and execution of conditional control statements are slower. Debugging is easier in an interpreter. High level language Input Interpreter Run Output Figure 6.2 |   Interpreter. Lexical analyzer reads the source program character by character at a time and unites them into a stream of tokens. Token is a group of character which represents keywords, operators and identifiers. Character sequence formed by tokens is called “lexeme”. Lexical analyzer is also known as lexer, tokenizer or scanner. If a lexical ­analyzer gets an invalid token, then it will generate an error. The lexical analyzer assigns an id to each token according to their occurrence. It makes entries of each identifier into the symbol table. As a lexical analyzer cannot enter all information regarding an identifier such as type and scope, the remaining information is inserted by the other phases of the compiler into the symbol table. For detail explanation about lexical analyzer, refer to Section 6.3.   1.  Symbol table: A symbol table is a data structure which stores identifier information related to its declaration and appearance in a program such as identifier id, type, scope, value and sometimes location. Link list and hashing are the common techniques which are used to construct symbol tables. Example 6.1 6.2.3  Phases of a Compiler Statement written in a source program is A compiler takes a source program written in high-level language as input and produces an equivalent set of machine instruction as output. The compiler process is complex, so it is divided into six sub-processes which are also known as phases of a compiler. The following are different phases of a compiler (Fig. 6.3):             1.  Lexical analyzer 2.  Syntax analyzer 3.  Semantic analyzer 4.  Intermediate code generator 5.  Code optimization 6.  Target code Input Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Code optimization A = B + C *25; Tokens generated by lexical analyzer A = B + C * 25 → → → → → → → 6.2.3.2 Syntax Analyzer or Parser A syntax analyzer is the second and important phase of a compiler. It receives the tokens from the output of a lexical analyzer as an input. Parser performs two functions:   1.  A parser checks that the input tokens from a lexical analyzer are valid or not according to the specified grammar of source language.   2.  It generates a parse tree according to the given grammar to the source language. The grammar of source language is given below: Target code Output Figure 6.3 |   Phases of a compiler. Chapter 6.indd 288 Identifier Assignment operator Identifier Add operator Identifier Multiplication operator Constant S P T Q → → → → id = P P + T/T T *Q/Q id/Integer constant 4/9/2015 9:59:33 AM

Lecture Notes