Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Compiler Notes

Compiler Notes



|Views: 2,689|Likes:
Published by ram5584

More info:

Published by: ram5584 on Jul 01, 2008
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less





A compiler accepts a program written in a high level language as input and produces its machine language equivalent as output. For the purpose of compiler construction, a high level programming language is described in terms of a grammar.This grammar specifies the formal description of the syntax or legal statements in thelanguage.
Assignment statement in Pascal is defined as:< variable > : = < Expression >The compiler has to match statement written by the programmer to the structuredefined by the grammars and generates appropriate object code for each statement. Thecompilation process is so complex that it is not reasonable to implement it in one singlestep. It is partitioned into a series of sub-process called phases. A phase is a logicallycohesive operation that takes as input one representation of the source program and produces an output of another representation. The basic phases are - Lexical Analysis,Syntax Analysis, and Code Generation.
Lexical Analysis:
It is the first phase. It is also called scanner. It separatescharacters of the source language into groups that logically belong together. These groupsare called tokens. The usual tokens are:Keyword: such as DO or IF,Identifiers: such as x or num,Operator symbols: such as <, =, or, +, andPunctuation symbols: such as parentheses or commas.The output of the lexical analysis is a stream of tokens, which is passed to thenext phase; the syntax analyzer or parser.
Syntax Analyzer:
It groups tokens together into syntactic structure. Foexample, the three tokens representing A + B might be grouped into a syntactic structurecalled as expression. Expressions might further be combined to form statements. Oftenthe syntactic structures can be regarded as a tree whose leaves are the tokens. The interior 
System Software
nodes of the tree represent strings of token that logically belong together. Fig. 1 showsthe syntax tree for READ statement in PASCAL
(id - list)READ (
Fig. 1 Syntax Tree
Code Generator:
It produces the object code by deciding on the memorylocations for data, selecting code to access each datum and selecting the registers inwhich each computation is to be done. Designing a code generator that produces trulyefficient object program is one of the most difficult parts of compiler design.In the following sections we discuss the basic elements of a simple compilation process, illustrating this application to the example program in fig. 2.PROGRAM STATSVAR SUM, SUMSQ, I, VALUE, MEAN, VARIANCE : INTEGER BEGINSUM : = 0 ;SUMSQ : = 0 ;FOR I : = 1 to 100 DoBEGINREAD (VALUE) ;SUM : = SUM + VALUE ;SUMSQ : = SUMSQ + VALUE * VALUEEND;MEAN : = SUM DIV 100;VARIANCE : = SUMSQ DIV 100 - MEAN * MEAN ;WRITE (MEAN, VARIANCE)END
Fig. 2 Pascal Program
A grammar for a programming language is a formal description of the syntax of  programs and individual statements written in the language. The grammar does notdescribe the semantics or memory of the various statements. To differentiate betweensyntax and semantics consider the following example
VARX, Y : REAL VAR I, J, K : INTEGEI : INTEGER X : = I + Y ; I : = J + K ;
Fig .3These two programs statement have identical syntax. Each is an assignmentstatement; the value to be assigned is given by an expression that consists of two variablenames separated by the operator '+'.The semantics of the two statements are quite different. The first statementspecifies that the variables in the expressions are to be added using integer arithmeticoperations. The second statement specifies a floating-point addition, with the integer operand 2 being connected to floating point before adding. The difference between thestatements would be recognized during code generation.Grammar can be written using a number of different notations. Backus-Naur Form (BNF) is one of the methods available. It is simple and widely used. It providescapabilities that are different for most purposes.A BNF grammar consists of a set of rules, each of which defines the syntax of some construct in the programming language.A grammar has four components. They are:
A set of tokens, known as terminal symbols non-enclosed in bracket.
READ, WRITE2.A set of terminals. The character strings enclosed between the angle brackets(<, >) are called terminal symbols. These are the names of the constructsdefined in the grammar.3.A set of productions where each production consists of a non-terminal calledthe left side of the production, as "is defined to be" (:: = ), and a sequence of token and/or non-terminal, called the right side of the product.
: < reads > : : = READ <id - list >.4.A designation of one of the non-terminals as the start symbol.This rule offers two possibilities separated by the symbol, for the syntax of an< id - list > may consist simply of a token
(the notation
denotes an identifier that isrecognized by the scanner). The second syntax.
ALPHAALPHA, BETAALPHA is an < id - list > that consist of another < id - list > ALPHA, followed by a comma, followed by an
It is also called parse tree or syntax tree. It is convenient to display theanalysis of a source statement in terms of a grammar as a tree.
READ (VALUE)GRAMMAR: (read) : : = READ ( < id -list>)
Assignment statement:
SUM : = 0 ;SUM : = + VALUE ;

Activity (27)

You've already reviewed this. Edit your review.
neha added this note
Tq it helped me a lot......<3
neha liked this
Miloni Mehta liked this
1 thousand reads
1 hundred reads
Kamini Girdhar liked this
perera.randika liked this
Naina Talwar liked this
AJAY liked this

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->