COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program...

51
COMPILERS BASIC COMPILER FUNCTIONS 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 the language. Example: Assignment statement in Pascal is defined as: < variable > : = < Expression > The compiler has to match statement written by the programmer to the structure defined by the grammars and generates appropriate object code for each statement. The compilation process is so complex that it is not reasonable to implement it in one single step. It is partitioned into a series of sub-process called phases. A phase is a logically cohesive 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 separates characters of the source language into groups that logically belong together. These groups are called tokens. The usual tokens are: Keyword: such as DO or IF, Identifiers: such as x or num, Operator symbols: such as <, =, or, +, and Punctuation symbols: such as parentheses or commas. The output of the lexical analysis is a stream of tokens, which is passed to the next phase; the syntax analyzer or parser. Syntax Analyzer: It groups tokens together into syntactic structure. For example, the three tokens representing A + B might be grouped into a syntactic structure called as expression. Expressions might further be combined to form statements. Often the syntactic structures can be regarded as a tree whose leaves are the tokens. The interior nodes of the tree represent strings of token that logically belong together. Fig. 1 shows the syntax tree for READ statement in PASCAL (read)

Transcript of COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program...

Page 1: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

COMPILERS

BASIC COMPILER FUNCTIONS

A compiler accepts a program written in a high level language as input andproduces its machine language equivalent as output. For the purpose of compilerconstruction, 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.

Example: 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 andproduces 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. Forexample, 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 interiornodes of the tree represent strings of token that logically belong together. Fig. 1 showsthe syntax tree for READ statement in PASCAL

(read)

Page 2: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 185

(id - list)

READ ( id ){value}

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 compilationprocess, illustrating this application to the example program in fig. 2.

PROGRAM STATSVAR

SUM, SUMSQ, I, VALUE, MEAN, VARIANCE : INTEGERBEGIN

SUM : = 0 ;SUMSQ : = 0 ;FOR I : = 1 to 100 Do

BEGINREAD (VALUE) ;SUM : = SUM + VALUE ;SUMSQ : = SUMSQ + VALUE * VALUE

END;MEAN : = SUM DIV 100;VARIANCE : = SUMSQ DIV 100 - MEAN * MEAN ;WRITE (MEAN, VARIANCE)

ENDFig. 2 Pascal Program

GRAMMARSA 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:

VAR X, Y : REAL VAR I, J, K : INTEGERI : INTEGER

X : = I + Y ; I : = J + K ;

Fig .3

Page 3: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software186

These 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 integeroperand 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-NaurForm (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 ofsome construct in the programming language.

A grammar has four components. They are:

1. A set of tokens, known as terminal symbols non-enclosed in bracket.

Example: READ, WRITE

2. 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 oftoken and/or non-terminal, called the right side of the product.

Example: < 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 id (the notation id denotes an identifier that isrecognized by the scanner). The second syntax.

Example: ALPHAALPHA, BETA

ALPHA is an < id - list > that consist of another < id - list > ALPHA, followedby a comma, followed by an id BETA.

Tree: 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.

Example: READ (VALUE)GRAMMAR: (read) : : = READ ( < id -list>)

Example: Assignment statement:

SUM : = 0 ;SUM : = + VALUE ;SUM : = - VALUE ;

Page 4: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 187

Grammar: < assign > : : = id : = < exp >< exp > : : = < term > | < exp > - < term >< term > : : = < factor > | < term > * < factor > | < term > DIV < factor >< factor > : : = id | int | ( < exp > )

Assign consists of an id followed by the token : = , followed by an expression<exp > Fig. 4(a). Show the syntax tree.

Expressions are sequence of <terms> connected by the operations + and - Fig.4(b). Show the syntax tree.

Term is a sequence of < factor > S connected by * and DIV Fig. 4(c).

A factor may consists of an identifies id or an int (which is also recognized by thescan) or an < exp > enclosed in parenthesis. Fig. 4(d).

< assign > < exp >

id : = <exp > < term > + < exp >{variance }

Fig. 4 (a) Fig. 4 (b)

< term > factor| |

< factor > Dir < term > id

X < factor > int

IdFig.4 (c)

(< exp > ) Fig. 4 (d)

Fig. 4 Parse Trees

For the statement Variance : = SUMSQ Div 100 - MEAN * MEAN ;The list of simplified Pascal grammar is shown in fig.5.

1. < prog > : : = PROGRAM < program > VAR <dec - list >BEGIN < stmt > - list > END.

2. < prog - name >: : = id3. < dec - list > : : = < dec > | < dec - list > ; < dec >4. < dec > : : = < id - list > : < type >5. < type > : : = integer6. < id - list > : : = id | < id - list > , id7. <stmt - list > : : = < stmt > <stmt - list > ; < stmt >8. < stmt > : : = < assign > | <read > | < write > | < for >

Page 5: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software188

9. < assign > : : = id : = < exp >10. < exp > : : = < term > | < exp > + < term > | < exp > - < term >11. < term > : : = < factor > | < term > <factor> | <term> DIV <factor>12. < factor > : : = id ; int | (< exp >)

13. < READ > : : = READ ( < id - list >)14. < write > : : = WRITE ( < id - list >)15. < for > : : = FOR < idex - exp > Do < body >16. < index - exp> : : = id : = < exp > To ( exp >17. < body > : : = < start > | BEGIN < start - list > END

Fig. 5 Simplified Pascal Grammar

( < prog >)|

PROGRAM < prog - name > VAR dec - list BEGIN <Stmt - list > END

Id < dec >{STATS}

< stmt - list > ; < stmt >< id - list > : < type >

INTEGER < write >

(id - list) , id

{VARIANCE} < stmt - list > ; <stmt > WRITE ( <id - list > )

(id - list ) ; id < stmt - list > ; <stmt > < assign > (id - list ) . id(MEAN) {VARIANCE}

id(id - list ) , id id : = <MEAN>

<VALUE > < stmt - list > ; <stmt > {VARIANCE} < exp >

< assign >(id - list ) , id

{I} <stmt > ; < start >id : = <exp> <term>

<id -list > , id {mean} <exp>{SMSQ} < stmt > < assign > | |

<term> <term> <term> * <factor>id | |

{SUM} < assign > id : = <exp> | | <factor> id{SUMSQ} | | <term> Div <factor> | [MEAN]

| | | term | >term> Div <factor > idid : < exp > | factor {MEAN}

| factor | int< term > | id {100} <factor> int

| int {SUM} | {100}< factor > { 0} id

| {SUMSQ}int {0}

NextPage

Page 6: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 189

|< for >

FOR <index - exp > Do < body >

Id : = <exp> To <exp> BEGIN <stm - list> END{I} | |

< term > <term>| |

<factor> <factor> <stmt - list > ; < stmt >| | |

int int{I} {100} <symt - list> ; <stmt> <assign >

| |< stmy > <assign >

| id : = <emp>< read > (SUMSQ

id : = <exp>{SUM}

READ ( < id - list > ) < exp > + < term >< exp > + < term >

id | | |{VALUE? <term > < factor > < term > <term> * <factor>

| | | | |< factor >. id <factor > <factor > id

| { value} | | {value}id id id

{SUM} {SUMSQ} {value}

Fig. 6 Parse tree for the Program 1

Parse tree for Pascal program in fig.1 is shown in fig. 6

1 (a) Draw parse trees, according to the grammar in fig. 5 for the following <id-list> S:

(a) ALPHA < id - list >|id

{ ALPHA }

(b) ALPHA, BETA, GAMMA < id - list >

id< id - list > , {GAMMA}

id< id - list > , {BETA}

id[ ALPHA ]

Page 7: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software190

2 (a) Draw Parse tree, according to the grammar in fig. 5 for the following < exp > S :

(a) ALPHA + BETA < exp >|

< term >

< term >|

< factor > + < factor >| |

id id{ALPHA} {BETA}

(b) ALPHA - BETA + GAMMA< exp

< exp > - term

< term > < term > * factor| |

< factor > < factor > id| {GAMMA}

id id{ALPHA} {BETA}

(c) ALPHA DIV (BETA + GAMMA) = DELTA< exp >

< exp > - < term >

< term > < factor >|

< term > Div < factor > {DELTA}

< factor >( < exp > )

id{ALPHA}

< exp > + < term >

< term > factor

id id{BETA} {GAMMA}

Page 8: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 191

3. Suppose the rules of the grammar for < exp > and < term > is as follows:

< exp > :: = < term > | < exp > * < term> | < exp> Div < term >< term > :: = <factor> | < term > + < factor > | < term > - < factor >

Draw the parse trees for the following:

(a) A1 + B1 (b) A1 - B1 * G1 (c) A1 + DIV (B1 + G1) - D1

< exp >|

(a) A1 + B1 term

< term > + < factor >factor

|id id

{A1} {B1}(b) A1 - B1 * G1 < exp >

|teerm

teerm - < factor >

factorterm * factor

|id factor id

{A1} id {B1} {G1}(c) A1 DIV (B1 + A1) - D1

< exp >

< exp > DIV < term >

< term > < term > - < factor >

< factor > < factor > id| {D1}

id{A1} ( < exp > )

< term >

< term > + < factor >

< factor > id

Page 9: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software192

{G1}id

{B1}

LEXICAL ANALYSIS

Lexical Analysis involves scanning the program to be compiled. Scanners aredesigned to recognize keywords, operations, identifiers, integer, floating point numbers,character strings and other items that are written as part of the source program. Items arerecognized directly as single tokens. These tokens could be defined as a part of thegrammar.

Example: <ident> : : = <letter> | <ident> <letter> | <ident> <digit><letter> : : = A | B | C | . . . | Z<digit> : : = 0 | 1 | 2 | . . . | 9

In a such a case the scanner world recognize as tokens the single characters A, B,. . . Z,, 0, 1, . . . 9. The parser could interpret a sequence of such characters as thelanguage construct < ident >. Scanners can perform this function more efficiently. Therecan be significant saving in compilation time since large part of the source programconsists of multiple-character identifiers. It is also possible to restrict the length ofidentifiers in a scanner than in a passing notion. The scanner generally recognizes bothsingle and multiple character tokens directly.

The scanner output consists of sequence of tokens. This token can be consideredto have a fixed length code. The fig. 7 gives a list of integer code for each token for theprogram in fig. 5 in such a type of coding scheme, the PROGRAM is represented bythe integer value 1, VAR has the integer value 2 and so on.

Token Program VAR BEGIN END END INTEGER FORCode 1 2 3 4 5 6 7

Token READ WRITE To Do ; : ,Token : = + - K DIV ( )

Token : = + - K DIV ( )Code 15 16 17 18 17 20 21

Token Id IntCode 22 23

Fig. 7 Token Coding Scheme

For a keyword or an operator the token loading scheme gives sufficientinformation. In the case of an identifier, it is also necessary to supply particular identifiername that was scanned. It is true for the integer, floating point values, character-stringconstant etc. A token specifier can be associated with the type of code for such tokens.This specifier gives the identifier name, integer value, etc., that was found by the scanner.

Some scanners enter the identifiers directly into a symbol table. The tokenspecifier for the identifiers may be a pointer to the symbol table entry for that identifier.

The functions of a scanner are:

Page 10: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 193

The entire program is not scanned at one time. Scanner is a operator as a procedure that is called by the processor when it needs

another token. Scanner is responsible for reading the lines of the source program and possible

for printing the source listing. The scanner, except for printing as the output listing, ignores comments. Scanner must look into the language characteristics.

Example: FOTRAN : Columns 1 - 5 Statement number: Column 6 Continuation of line: Column 7 . 22 Program statement

PASCAL : Blanks function as delimiters for tokens: Statement can be continued freely: End of statement is indicated by ; (semi column)

Scanners should look into the rules for the formation of tokens.

Example: 'READ': Should not be considered as keyword as it is within quotes.i.e., all string within quotes should not be considered as token.

Blanks are significant within the quoted string. Blanks has important factor to play in different language

Example 1: FORTRAN Statement:

Do 10 I = 1, 100 ; Do is a key word, I is identifier, 10 is the statement numberStatement: Do 10 I = 1 ;It is an identifier Do 10 I = 1

Note: Blanks are ignored in FORTRAN statement and hence it is a assignmentstatement. In this case the scanner must look ahead to see if there is acomma (,) before it can decide in the proper identification of the charactersDo.

Example 2: In FORTRAN keywords may also be used as an identifier. Wordssuch as IF, THEN, and ELSE might represent either keywords orvariable names.

IF (THEN .EQ ELSE) THENIF = THEN

ELSETHEN = IF

ENDIF

Modeling Scanners as Finite Automata

Finite automatic provides an easy way to visualize the operation of a scanner.Mathematically, a finite automation consists of a finite set of states and a set of transitionfrom one state to another. Finite automatic is graphically represented. It is shown in fig,State is represented by circle. Arrow indicates the transition from one state to another.

Page 11: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software194

Each arrow is labeled with a character or set of characters that can be specified fortransition to occur. The starting state has an arrow entering it that is not connected toanything else.

State Final State TransitionFig. 8

Example: Finite automata to recognize tokens is gives in fig. 9. Thecorresponding algorithm is given in fig. 10

0 - 9A - Z

B A - Z

Fig. 9

Get first Input-characterIf Input-character in [ 'A' . . ' Z' ] then

beginwhile Input - character in [ 'A' . . 'Z', ' 0'. . ' 9' ] do

beginget next input - character

End {while}end {if first is [ 'A' .. ' Z' ] }

elsereturn (token-error)

Fig. 10

SYNTACTIC ANALYSIS

During syntactic analysis, the source programs are recognized as languageconstructs described by the grammar being used. Parse tree uses the above process fortranslation of statements, Parsing techniques are divided into two general classes:

-- Bottom up and -- Top down.Top down methods begin with the rule of the grammar that specifies the goal of

the analysis ( i.e., the root of the tree), and attempt to construct the tree so that theterminal nodes match the statement being analyzed.

Bottom up methods begin with the terminal nodes of the tree and attempt tocombine these into successively high - level nodes until the root is reached.

OPERATOR PRECEDENCE PARSING

The bottom up parsing technique considered is called the operator precedencemethod. This method is loaded on examining pairs of consecutive operators in the sourceprogram and making decisions about which operation should be performed first.

1

1 221

3

Page 12: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 195

Example: A + B * C - D (1)

The usual procedure of operation multiplication and division has higherprecedence over addition and subtraction.

Now considering equation (1) the two operators (+ and *), we find that + has

lower precedence than *. This is written as +⋖ * [+ has lower precedence *]

Similarly ( * and - ), we find that * ⋗ - [* has greater precedence -].The operation precedence method uses such observations to guide the parsing

process.

A + B * C - D (2)

⋖ ⋗

VA

RB

EG

INE

ND

EN

D

INT

EG

ER

FOR

RE

AS

WR

ITE

TO

DO

: : , : = + - * DIV

) ( Id Int

PROGRAMVARBEGINEND

≐≐

≐ ≐⋗ ⋗

⋖ ⋖ ⋖ ⋖ ⋖⋖⋗

<⋖⋖⋖

INTEGERFORREADWRITE

⋗ ⋗≐

≐≐

TODO

;:

⋖ ⋗ ⋗⋗ ⋗ ⋗⋗

⋖ ⋖ ⋖⋖ ⋖ ⋖

⋗⋗⋗ ⋖⋗

⋖ ⋖

⋖ ⋖ ⋖ ⋖ ⋖ ⋖⋖⋖

,: =

+-

⋗ ⋗⋗ ⋗⋗ ⋗⋗ ⋗

≐ ⋗⋗ ⋗ ⋗⋗ ⋗ ⋗⋗ ⋗ ⋗

⋖ ⋖⋗ ⋗⋗ ⋗⋗ ⋗

⋖ ⋖ ⋖⋖ ⋖ ⋖ ⋗⋖ ⋖ ⋖ ⋗⋗ ⋗ ⋗ ⋖

≐⋖ ⋖⋖ ⋖⋖ ⋖

*

DIV

)

(

⋗ ⋗⋗ ⋗

⋗ ⋗

⋗ ⋗ ⋗⋗ ⋗ ⋗⋗ ⋗ ⋗⋗ ⋗ ⋗

⋗ ⋗⋗ ⋗

⋖⋗ ⋗

⋗ ⋗ ⋖ ⋗⋗ ⋗ ⋖ ⋗⋖ ⋖ ⋖ ≐⋗ ⋗ ⋗

⋖ ⋖⋖ ⋖⋖ ⋖

idInt

⋗ ⋗ ⋗⋗ ⋗

⋗ ⋗ ⋗ ⋗⋗ ⋗ ⋗

⋗ ≐ ⋗ ⋗⋗ ⋗

⋗ ⋗ ⋗⋗ ⋗ ⋗

Fig 11 Precedence Matrix for the Grammar for fig 5

Equation (2) implies that the sub expression B * C is to be computed beforeeither of the other operations in the expression is performed. In times of the parse treethis means that the * operation appears at a lower level than does either + or -. Thus abottom up parses should recognize B * C by interpreting it in terms of the grammar,

Page 13: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software196

before considering the surrounding terms. The first step in constructing an operator-precedence parser is to determine the precedence relations between the operators of thegrammar. Operator is taken to mean any terminal symbol (i.e., any token). We also haveprecedence relations involving tokens such as BEGIN, READ, id and ( . For thegrammar in fig. 5, the precedence relations is given in the fig. 11.

Example: PROGRAM ≐ VAR ; These two tokens have equal precedence

Begin ⋖ FOR ; BEGIN has lower precedence over FOR. There are some valueswhich do not follows precedence relations for comparisons.

Example: ; ⋗ END and END ⋗ ;

i.e., when ; is followed by END, the ' ; ' has higher precedence and when ENDis followed by ; the END has higher precedence.

In all the statements where precedence relation does not exist in the table, twotokens cannot appear together in any legal statement. If such combination occurs duringparsing it should be recognized as error.

Let us consider some operator precedence for the grammar in fig. 5.

Example: Pascal Statement: BEGINREAD (VALUE);

These Pascal statements scanned from left to right, one token at a time. For eachpair of operators, the precedence relation between them is determined. Fig. 12(a) showsthe parser that has identified the portion of the statement delimited by the precedence

relations ⋖ and ⋗ to be interpreted in terms of the grammar.(a) . . . BEGIN READ ( id )

⋖ ≐⋖ ⋗(b) . . . BEGIN READ ( < N1 > ) ;

⋖ ≐ ≐ ⋗(c) . . . BEGIN < N2 > ;

(d) ... < N2 >

READ ( <N1 > )

id(VALUE)

Fig. 12

According to the grammar id may be considered as < factor > . (rule 12),<program > (rule 9) or a < id-list > (rule 6). In operator precedence phase, it is notnecessary to indicate which non-terminal symbol is being recognized. It is interpreted asnon-terminal < N1 >. Hence the new version is shown in fig. 12(b).

Page 14: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 197

An operator-precedence parser generally uses a stack to save token that havebeen scanned but not yet parsed, so it can reexamine them in this way. Precedencerelations hold only between terminal symbols, so < N1 > is not involved in this processand a relationship is determined between (and).

READ (<N1>) corresponds to rule 13 of the grammar. This rule is the only onethat could be applied in recognizing this portion of the program. The sequence is simplyinterpreted as a sequence of some interpretation < N2 >. Fig. 12(c) shows thisinterpretation. The parser tree is given in fig. 12(d).

Note: (1) The parse tree in fig. 1 and fig. 12 (d) are same except for the name ofthe non-terminal symbols involved.

(2) The name of the non-terminals is arbitrarily chosen.

Example: VARIANCE ; = SUMSQ DIV 100 - MEAN * MEAN

(i) . . id 1 : = id 2 Div . . . <N1>

⋖ ≐ ⋖ ⋗<id 2>

(ii) . . . id 1 : = <N1> Div int -

⋖ ≐ ⋖ ⋖ ⋗ {SUMSQ}

(iii) . . . id 1 : = <N1> Div <N2> - <N1> <N2>

⋖ ≐ ⋖ ⋗<id 2> int

{SUMSQ} {100}

(iv) . . . . id 1 : = <N3> - id 3 * <N3>

⋖ ≐ ⋖ ⋖ ⋗<N1> DIV <N2>id2 int

{SUMSQ} {100}

v) . . . . id 1 : = <N3> - <N4> * id 4 ; <N4>

⋖ ≐ ⋖ ⋖ ⋖ ⋗id 3

{MEAN}(vi) . . . id 1 : = <N3> - <N4> * <N5> <N5>

⋖ ≐ ⋖ ⋖ ⋗id 4

{MEAN}

(vii) . . . id 1 : = <N3> - <N6> <N6>

⋖ ≐ ⋖ ⋗<N4> * <N5>

id 3 id 4{MEAN} {MEAN}

Page 15: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software198

(viii) . . . id : = <N7> <N7>

⋖ ≐ ⋗<N3> - <N6>

(ix) . . . <N8><N8>

<N7>

<N3> <N6>

id 1 : = <N1> <N2> <N4> <N5>{VARIANCE} DIV *

id 2 int - id 3 id 4{SUMSQ} {100} {MEAN} {MEAN}

SHIFT REDUCE PARSING

The operation procedure parsing was developed to shift reduce parsing. Thismethod makes use of a stack to store tokens that have not yet been recognized in terms ofthe grammar. The actions of the parser are controlled by entries in a table, which issomewhat similar to the precedence matrix. The two main actions of shift reducingparsing are

Shift: Push the current token into the stack.

Reduce: Recognize symbols on top of the stack according to a rule of a grammar

Example: BEGIN READ ( id ) . . .

Steps Token Stream Stack1. . . . BEGIN READ ( id ) . . .

2. . . . BEGIN READ ( id )

BEGIN

3. . . . BEGIN READ ( id ) . . .

READBEGIN

Shift

Shift

Shift

Page 16: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 199

4. . . BEGIN READ ( id ) . . .(

READ

BEGIN5. . . . BEGIN READ ( id ) . . .

id(

READBEGIN

6. . . . BEGIN READ ( id ) . . ..

< id-list >(

READBEGIN

Explanation

1. The parser shift (pushing the current token onto the stack) when it encountersBEGIN

2 to 4. The shift pushes the next three tokens onto the stack.5. The reduce action is invoked. The reduce converts the token on the top of the

stack to a non-terminal symbol from the grammar.6. The shift pushes onto the stack, to be reduced later as part of the READ

statement.

Note: Shift roughly corresponds to the action taken by an operator – precedence

parses when it encounters the relation ⋖ and ≐. Reduce roughly corresponds to

the action taken when an operator precedence parser encounters the relation ⋗.RECURSIVE DESCENT PARSING

Recursive-Descent is a top-down parsing technique. A recursive-descent parser ismade up of a precedence for each non-terminal symbol in the grammar. When aprecedence is called it attempts to find a sub-string of the input, beginning with thecurrent token, that can be interpreted as the non-terminal with which the procedure isassociated. During this process it may call other procedures, or call itself recursively tosearch for other non-terminals. If the procedure finds the non-terminal that is its goal, itreturns an indication of success to its caller. It also advances the current-token pointerpast the sub-string it has just recognized. If the precedence is unable to find a sub-stringthat can be interpreted as to the desired non-terminal, it returns an indication of failure.

Example: < read > : : = READ ( < id - list > )

The procedure for < read > in a recursive descent parser first examiner the nexttwo input, looking for READ and (. If these are found, the procedures for < read > thencall the procedure for < id - list >. If that procedure succeeds, the < read > procedureexamines the next input token, looking for). If all these tests are successful, the < read >procedure returns an indication of success. Otherwise the procedure returns a failure.There are problems to write a complete set of procedures for the grammar of fig. 15.

Shift

Shift

Shift

Page 17: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software200

Example: The procedure for < id - list >, corresponding to rule 6 would be unableto decide between its alternatives since id and < id-list > can begin with id. <id-list > : : =id | < id-list >, id

If the procedure somehow decided to try the second alternative <id-list>, it wouldimmediately call itself recursively to find an <id-list>. This causes unending chain. Top-down parsers cannot be directly used with a grammar that contains this kind of immediateleft recursion.

Similarly the problem occurs for rules 3, 7, 10 and 11. Hence the fig. 13 showsthe rules 3, 6, 7, 10 and 11 modification.

3 < dec - list > : : = < dec > { ; <dec > }6 < id - list > : : = id {; id }7 < stmt - list > : : = < stmt > { ; < stmt > }10 < exp > : : = < term > { + < term . | -- < term > }11 < term > : : = < factor > { + < factor > | Div < factor >.}

Fig. 13

Fig. 14 illustrates a recursive-descent parse of the READ statement: READ (VALUE);The modified grammar is considered in the procedure for the non-terminal <read > and < id-list >.It is assumed that TOKEN contains the type of the next input token.

PROCEDURE READBEGIN

ROUND : = FALSEIf TOKEN + 8 { read } THEN

BEGINadvance to next tokenIF TOKEN + 20 { ( } THEN

BEGINadvance to next tokenIF IDLIST returns success THEN

IF token = 21 { ) } THENBEGIN

FOUND : = TRUEadvance to next token

END { if ) }END { if READ }

IF FOUND = TRUE THENreturn success

else failureend (READ)

Fig. 14

Procedure IDLISTbegin

FOUND = FALSE

Page 18: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 201

if TOKEN = 22 {id} thenbegin

FOUND : = TRUE advance to Next tokenwhile (TOKEN = 14 {,}) and (FOUND = TRUE) do

beginadvance to next tokenif TOKEN = 22 {id} then

advance to next tokenelse

FOUND = FALSEEnd {while}

End {if id}if FOUND : = TRUE then

return successelse

return failureend {IDLIST}

Fig. 15The fig. 15 IDLIST procedure shows an error message if ( , ) is not followed by

a id. It indicates the failure in the return statement. If the sequence of tokens such as " id,id " could be a legal construct according to the grammar, this recursive-descenttechnique would not work properly.

Fig. 16 shows a graphic representation of the recursive parsing process for thestatement being analyzed.

(i) In this part, the READ procedure has been invoked and has examined thetokens READ and ' ( " from the input stream (indicated by the dashedlines).

(ii) In this part, the READ has called IDLIST (indicated by the solid line),which has examined the token id.

(iii) In this part, the IDLIST has returned to READ indicating success; READhas then examined the input token.

Note that the sequence of procedure calls and token examinations has completelydefined the structures of the READ statement. The parser tree was constructed beginningat the root, hence the term top-down parsing.

(i) (II) (iii)

READ READ READ( ( (

id id{ Value } { Value

Fig. 16Fig. 17 illustrates a recursive discard parse of the assignment statement.

READ READ READ

IDLIST IDLIST

Page 19: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software202

Variance: = SUNSQ DIVISION - MEAN * MEAN

The fig. 17 shows the procedures for the non-terminal symbols that areinvolved in parsing this statement.

Procedure ASSIGNbegin

FOUND = FALSEif TOKEN = 22 {id} then

beginadvance to Next token

if TOKEN = 15 {: =} thenbegin

advance to next tokenif EXP returns success then

FOUND : = TRUEend {if : =}

if FOUND : = TRUE thenreturn success

elsereturn failure

end {ASSIGN}Procedure EXP

beginFOUND = FALSE

If TERM returns success thenbegin

FOUND: = TRUEwhile ((TOKEN = 16 {+ } ) or (TOKEN = 17 { - } ) )

and (FOUND = TRUE) dobegin

advance to next tokenif TERM returns success then

FOUND = FALSEend {while}

end {if TERM}if FOUND : = TRUE then

return successelse

return failureend {EXP}

Procedure TERMbegin

FOUND : = FALSEIf FACTOR returns success then

begin

Page 20: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 203

FOUND : = TRUEwhile ((TOKEN = 18 { * }) or (TOKEN = 19 {DIV })

and (FOUND = TRUE) dobegin

advance to next tokenif TERM returns failure then

FOUND : = FALSEend {while}

end {if FACTOR}if FOUND : = TRUE then

return successelse

return failureend {TERM}

Procedure FACTORbegin

FOUND : = FALSEif (TOKEN = 22 { id } ) or (TOKEN = 23 {int } ) then

beginFOUND : = TRUEadvance to next token

end { if id or int }else

if TOKEN = 20 { ( } thenbegin

advance to next tokenif EXP returns success then

if TOKEN = 21 { ) } thenbegin

(FOUND = TRUE)advance to next token

end { if ) }end {if ( }

if FOUND : = TRUE thenreturn success

elsereturn failure

end {FACTOR}

Fig. 17 Recursive-Descent Parse of an Assignment Statement

A step-by-step representation of the procedure calls and token examination isshown in fig. 1

Page 21: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software204

(i) (ii) (iii)

id 1 : = id 1 : = id 1 : ={ VARIANCE } { VARIANCE } {VARIANCE}

(iv) (v) (vi)

id 1 : = id 1 : = id 1 : ={VARIANCE} {VARIANCE} {VARIANCE}

-

DIV DIV

id 2 id 2 int id 2 int{SUMSQ} {SUMSQ} {100} {SUMSQ} {100}

(vii)

id 1 : ={VARIANCE}

-

DIV

id 2 int id 3{SUMSQ} {100} {MEANS}

(viii)

id 1 : =(VARIANCE}

-

*DIV DIV

id 2 int id 3 id 4{SUMSQ} {100} {MEANS} {MEANS}

Fig. 18 Step by step Representation for Variance : = SUMSQ Div 100 - MEAN * Mean

ASSIGN ASSIGN

EXP

ASSIGN ASSIGN

EXP EXP

TERM

ASSIGN

EXP

TERMTERMTERM

FACTOR FACTOR FACTOR FACTOR FACTOR

ASSIGN

TERM

EXP

ASSIGN

FACTOR FACTOR

TERM

FACTOR

TERM

EXP

FACTOR FACTOR FACTOR FACTOR

TERM TERM

EXP

Page 22: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 205

GENERATION OF OBJECT CODE

After the analysis of system, the object code is to be generated. The codegeneration technique used in a set of routine, one for each rule or alternative rule in thegrammar. The routines that are related to the meaning of he compounding construct in thelanguage is called the semantic routines.

When the parser recognizes a portion of the source program according to somerule of the grammar, the corresponding semantic routines are executed. These semanticroutines generate object code directly and hence they are referred as code generationroutines. The code generation routines that is discussed are designed for the use with thegrammar in fig. .5. This grammar is used for code generations to emphasize the point thatcode generation techniques need not be associated with any particular parsing method.

The parsing technique discussed in 1.3 does not follow the constructs specifiedby this grammar. The operator precedence method ignores certain non-terminal and therecursive-descent method must use slightly modified grammar.

The code generation is for the SIC/XE machine. The technique use two datastructure:

(1) A List (2) A Stack

List Count: A variable List count is used to keep a count of the number of itemscurrently in the list. The token specifiers are denoted by ST (token)

Example: id ST (id) ; name of the identifierint ST (int) ; value of the integer, # 100

The code generation routines create segments of object code for the compiledprogram. A symbolic representation is given to these codes using SIC assemblerlanguage.

LC (Location Counter): It is a counter which is updated to reflect the nextvariable address in the compiled program (exactly as it is in an assembler).

Application Process to READ Statement:

(read)+ JSUB XREAD

WORD 1< id - list > WORD VALUE

READ ( ){VALUE}

Fig. 19(a) Parse Tree for Read

Using the rule of the grammar the parser recognizes at each step the left mostsub-string of the input that can be interpreted. In an operator precedence parse, therecognition occurs when a sub-string of the input is reduced to some non-terminal <N i>.

In a recursive-descent parse, the recognition occurs when a procedure returns toits caller, indicating success. Thus the parser first recognizes the id VALUE as an < id -list >, and then recognizes the complete statement as a < read >.

Page 23: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software206

The symbolic representation of the object code to be generated for the READstatement is as shown in fig. 19(b). This code consists of a call to a statement XREAD,which world be a part of a standard library associated with the compiler. The subroutineany program that wants to perform a READ operation can call XREAD. XREAD islinked together with the generated object program by a linking loader or a linkage editor.The technique is commonly used for the compilation of statements that performvoluntarily complex functions. The use of a subroutine avoids the repetitive generation oflarge amounts of in-line code, which makes the object program smaller.

The parameter list for XREAD is defined immediately after the JSUB that callsit. The first word is the number of variable that will be assigned values by the READ.The following word gives the addresses of three variables.

Fig. 19(c) shows the routines that might be used to accomplish the codegeneration.

1. < id - list > : : = idadd ST (id) to listadd 1 to List_count

2. < id - list > : : = < id - list >, idadd ST (id) to listadd 1 to LC List_Current

3. < read > : : = READ (< id - list >)generate [ + JSUB XREAD ]record external reference to XREADgenerate [WORD List - count]for each item on list of do

beginremove ST (ITEM) from listgenerate [WORD ST (ITEM)]

endList _count : = 0

Fig. 19 (c) Routine for READ Code Generation

The first two statements (1) and (2) correspond to alternative structure for < id -list >, that is < id - list > : : = id | < id - list >, id.

In each case the token specifies ST (id) for a new identifier being called to the <id - list > is inserted into the list used by the code-generation routine, and list-count isupdated to reflect the insertion. After the entire < id-list > has been parsed, the listcontains the token specifiers for all the identifiers that are part of the < id- list >. Whenthe < read > statement is recognized, the token specifiers are removed from the list andused to generate the object code for the READ.

Code-generation Process for the Assignment Statement

Example: VARIANCE: = SUMSQ DIV 100 - MEAN * MEAN

The parser tree for this statement is shown in fig. 20. Most of the work of parsinginvolves the analysis of the < exp > on the right had side of the " : = " statement.:

Page 24: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 207

< assign >

< exp >

< exp > < exp >(term)

< term >

< term > < term > < factor >

< factor >< factor > < factor >

id : = id DIV int _ id * id{VARIANCE} { SUMSQ } {100} {MEAN} {MEAN}

Fig. 20

The parser first recognizes the id SUMSQ as a < factor > and < term > ; then itrecognizes the int 100 as a < factor >; then it recognizes SUNSQ DIV 100 as a < term >,and so forth. The order in which the parts of the statements are recognized is the same asthe order in which the calculations are to be performed. A code-generation routine iscalled for each portion of the statement is recognized.

Example; For a rule < term >1: : = < term > 2 * < factor > a code is to be generated.

The subscripts are used to distinguish between the two occurrences of < term > .The code-generation routines perform all arithmetic operations using register A.

Hence the multiple < term >2 * < factor > after multiplication is available in register A.Before multiplication one of the operand < term >2 must be located in A-register. Theresults after multiplication will be left in register A. So we need to keep track of theresult left in register A by each segment of code that is generated. This is accomplishedby extending the token-specifier idea to non-terminal nodes of the parse tree.

The node specifier ST (< term1>) would be set to rA, indicating that the result ofthe completion is in register A. the variable REGA is used to indicate the highest levelnode of the parse tree when value is left in register A by the code generated so far.Clearly there can be only one such node at any point in the code-generation process. Ifthe value corresponding to a node is not in register A, the specifier for the node is similarto a token specifier: either a pointer to a symbol table entry for the variable that containsthe value or an integer constant.

Fig. 21 shows the code-generation routine considering the A-register of themachine.

1. < assign > : : = id := < exp >GETA (< exp >)generate [ STA ST (id)]REGA : = null

Page 25: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software208

2. <exp> :: =< term >ST < exp > : = ST (< term >)if ST < exp > = rA then

REGA : = < exp >3. < exp >1 : : = < exp >2 + < term >

if SR (< exp >2) = rA thengenerate [ADD ST (< term >)]

else if ST (< term >) = rA thengenerate [ADD ST (< exp >2)]

elsebegin

GETA (< EXP >2)generate [ADD ST(< term >)]

endST (< exp >1) : = rAREGA : = < exp >1

4. < exp >1 : : = < exp >2 - < term >if ST (< exp >2) = rA then

generate [SUB ST (< term >)]else

beginGETA (< EXP >2)generate [ SUB ST (< term >)]

endSR (< exp >1) : = rAREGA : = < exp >1

5. < term > : : = < factor >ST (< term >) : = ST (< factor >)if ST (<term >) = rA thenREGA : = < term >

6. < term >1 : : = < term >2 * < factor >if ST (< term >2) = rA then

generate [ MUL ST (< factor >)]else if S (< factor >) = rA then

generate [ MUL ST (< term >2)]else

beginGETA (< term >2)generate [ MUL SrT(< factor >)]

endST (< term >1) : = rAREGA : = < term >1

7. < term > : : = < term >2 DIV < factor >if SR (< term >2) = rA then

generate [DIV ST(< factor >)]

Page 26: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 209

elsebegin

GETA (< term >2)generate [ DIV ST (< factor >)]

endSR (< term >1) : = rAREGA : = < term >1

< factor > : : = idST (< factor >) : = ST (id)

9. < factor > : : = intST (< factor >) : = ST (int)

10. < factor > : : = < exp >ST (< factor >) : = ST (< exp >)if ST (< factor >) = rA thenREGA : = < factor >

Fig. 21 Code Generation Routines

If the node specifies for either operand is rA, the corresponding value is alreadyin register A, the routine simply generates a MUL instruction. The node specifier for theother operand gives the operand address for this MUL. Otherwise, the procedure GETAis called. The GETA procedure is shown in fig. 22.

Procedure - GETA (NODE)begin

if REGA = null thengenerate [LDA ST (NODE) ]

else if ST (NODE) π rA thenbegin

creates a new looking variable Tempi

generate [STA Tempi]record forward reference to Tempi

ST (REGA) : = Tempi

Generate [LDA ST (NODE)]end (if ≠ rA)ST(NODE) : = rAREGA : = NODE

end {GETA }Fig. 22

The procedure GETA generates a LDA instruction to load the values associatedto <term> 2 into register A. Before loading the value into A-register, it confirms whetherA is null. If it is not null it generates STA instruction to save the contents of register-Ainto Temp-variable. There can be number of Temp variable like Temp1, Temp2 . . . etc.The temporary variables used during a completion will be assigned storage location at theend of the object program. The node specifies for the node associated with the value

Page 27: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software210

previously in register A, indicated by REGA is reset to indicate the temporary variableused.

After the necessary instructions are generated, the code-generation routine setsST (< term >1) and REGA to indicate that the value corresponding to < terms >1 is now inregister A. This completes the code-generation action for the * operation.

The code-generation routine for ' + ' operation is the same as the ' * ' operation.The routine ' DIV ' and ' - ' are similar except that for these operations it is necessary forthe first operand to be in register A. The code generation for < assign > consists ofbringing the value to be assigned into register A (using GETA) and then generating aSTA instruction.

The remaining rules in fig. 21 do not require the generation of any instructionsince no computation and data movement is involved.

The object code generated for the assignment statement is shown in fig. 22.

LDA SUMSQDIV * 100STA TMP1

LDA MEANMUL MEANSTA TMP2

LDA TMP1

SUB TMP2

STA VARIABLE

Fig. 22

For the grammar < prog > the code-generation routine is shown in fig. 23. When<prog> is recognized, storage locations are assigned to any temporary (Temp) variablesthat have been used. Any references to these variables are then fixed in the object codeusing the same process performed for forward references by a one-pass assembler. Thecompiler also generates any modification records required to describe external referencesto library subroutine.

< prog > : : = PROGRAM < prog-name > VAR < dec list >BEGIN < stmp -- list > END.

generate [LDL RETADR]generate [RSUB]

for each Temp variable used dogenerate [ Temp RESW 1]

insert [ J EXADDR ] {jump to first executable instruction}in bytes 3 - 5 of object program. fix up forward reference toTemp variables generate modification records for externalreferences generates [END].

The < prog-name > generates header information in the object program that issimilar to that created from the START and EXTREF as assembler directives. It also

Page 28: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 211

generates instructions to save the return address and jump to the first executableinstruction in the compiled program. Fig. 24 shows the code generation routine for thegrammar < prog-name >.

< Program > : : = idgenerate [START 0]generate [EXTREF XREAD, XWRITE]generate [STL RETADR]

add 3 to LC {leave room for jump to first executable instruction}generate [RETADR RESW 1]

Fig. 24

Similar to the previous code-generation routine fig. 25 shows the code-generation for < dec - list >, < dec > , < write >, < for > , < index - exp > and body.

< dec - list > : : = { alternatives }

save LC as EXADDR {tentative address of first executableinstruction}

< dec > : : = > id - list > : < type >for each item on list do

beginremove ST (NAME) from listenter LC symbol table as address for NAMEgenerate [ST (NAME) RESW 1]end

LIST COUNT : = 0

< write > : : = WRITE ( < id - list > )generate [ + JSUB XWRITE]record external reference to XWRITEgenerate [WORD LISTCOUNT]

for each item on list dobegin

remove ST (ITEM) from listgenerate [WORD ST (ITEM)]

endLIST COUNT : = 0

< for > : : = FOR < id ex -- exp > Do < body >POP JUMPADDR from stack {address of jump out ofloop}

POP ST (INDEX) from stack {index variable}POP LOOPADDR from stack {beginning address of loop}

generate [LDA ST (INDEX)]generate [ADD #1]

Page 29: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software212

generate [ J LOOPADDR]insert [ JGT LC ] at location JUMPADDR

< index - exp > : : = id : = < exp > | TO < exp >2

GETA (< exp >;)Push LC onto stack {beginning addressing loop}Push ST (id) onto stack {index variable}Generate [STA ST (id)]Generate [ COMP ST (< exp > 2)]Push LC onto stack {address of jump out of loop}and 3 to LC [ leave room for jump instruction]REGA : = null

Fig. 25

There are no code-generation for the statements

< type > : : = INTEGER< stmt - list > : : = {either alternative}< stmt > : : = {any alternative}< body > : : = {either alternative}

For the Pascal program in fig. 1 the complete code-generation process is shown infig. 26.

1 STATS START 0 {Program Header}EXTREF XREAD, XREAD, XWRITESTL TETADR {Save return address}J {EXADDR}

2 RETADDR RESW 13 SUM RESW 1

SUMSQ RESW 1I RESW 1

VALUE RESW 1MEAN RESW 1

VARIANCE RESW 15 {EXADDR} LDA # 0 {SUM = 0}

STA SUM6 LDA # 0 {SUMSQ : = 0}

STA SUMSQ7 LDA # 1 {FOR I : = 1 TO 100}

{L1}STA ICOMP # 100JGT {L2}

9 + JSUB X READ {READ (VALUE) }WORD 1WORD VALUE

10 LDA SUM {SUM : = SUM + VALUE}ADD VALUESTA SUM

11 LDA VALUE {SUMSQ : = SUMSQ * VALUE * VALUE}

Page 30: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 213

MUL VALUEADD SUMSQSTA SUMSQLDA I {END OF FOR LOOP}ADD # 1J {L1}

13 {L2} LDA SUM {MEAN : = SUM DIVISION}DIV # 100STA MEAN

14 LDA SUM {VARIABLE : = SUMSQ DIVDIV # 100 100 - MEAN * MEAN}STA TEMP1LDA MEANMUL MEANSTA TEMP2LDA TEMP1SUB TEMP2STA VARIANCE

15 +JSUB XWRITE {WRITE (MEAN, VARIANCE) }WORD 2WORD MEANWORD VARIABLELDL RETADRRSUB

TEMP 1` RESW 1 {WORKING VARIABLE USED}TEMP 2 RESW 1

END

Fig. 25 Object Code Generated for Pascal Program

8.1 MACHINE DEPENDENT COMPILER FEATURES

At an elementary level, all the code generation is machine dependent. This isbecause, we must know the instruction set of a computer to generate code for it. There aremany more complex issues involved. They are:

Allocation of register Rearrangement of machine instruction to improve efficiency of execution

Considering an intermediate form of the program being compiled normally doessuch types of code optimization. In this intermediate form, the syntax and semantics ofthe source statements have been completely analyzed, but the actual translation intomachine code has not yet been performed. It is easier to analyze and manipulate thisintermediate code than to perform the operations on either the source program or themachine code. The intermediate form made in a compiler, is not strictly dependent on themachine for which the compiler is designed.

8.1.1 INTERMEDIATE FORM OF THE PROGRAM

The intermediate form that is discussed here represents the executable instructionof the program with a sequence of quadruples. Each quadruples of the form

Page 31: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software214

Operation, OP1, OP2, result.Where

Operation - is some function to be performed by the object codeOP 1 & OP2 - are the operands for the operation andResult - designation when the resulting value is to be placed.

Example 1: SUM : = SUM + VALUE could be represented as+ , SUM, Value, i, i1

: = i1 , , SUM

The entry i1, designates an intermediate result (SUM + VALUE); the secondquadruple assigns the value of this intermediate result to SUM. Assignment is treated as aseparate operation ( : =).

Example 2 : VARIANCE : = SUMSQ, DIV 100 -- MEAN * MEANDIV, SUMSQ, #100, i1

*, MEAN, MEAN, i2

- , i1, i2, i3

: : = i3 , VARIABLE

Note: Quadruples appears in the order in which the corresponding object codeinstructions are to be executed. This greatly simplifies the task ofanalyzing the code for purposes of optimization. It is also easy to translateinto machine instructions.

For the source program in Pascal shown in fig. 1. The corresponding quadruplesare shown in fig. 27. The READ and WRITE statements are represented with a CALLoperation, followed by PARM quadruples that specify the parameters of the READ orWRITE. The JGT operation in quadruples 4 in fig. 27 compares the values of its twooperands and jumps to quadruple 15 if the first operand is greater than the second. The Joperation in quadruples 14 jumps unconditionally to quadruple 4.

Line Operation OP 1 OP 2 Result Pascal Statement

1. : = # 0 SUM SUM : = 02. : = # 0 SUMSQ SUMSQ : = 03. : = # 1 I FOR I : = 1 to 1004. JGT I #100 (15)5. CALL XREAD READ (VALUE)6. PARAM VALUE7. + SUM VALUE i1 SUM : = SUM + VALUE

; = i1 SUM9. * VALUE VALUE i2 SUMSQ : = SUMSQ +

VALUE10. + SUMSQ i2 i3

* VALUE11. : = i3 SUMSQ

Page 32: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 215

12. + I #1 i4 End of FOR loop13. : = i4 I14. J (4)15. DIV SUM #100 i5 MEAN : = SUM DIV 10016. : = i5 MEAN17. DIV SUMSQ #100 i6 VARIANCE : =1 * MEAN MEAN i7 SUMSQ DIV 10019. - i6 i7 i8 - MEAN * MEAN20. : = i8 VARIANCE21. CALL XWRITE WRITE (MEAN, VALIANCE22. PARAM MEAN23. PARAM VARIANCE

Fig. .27 Intermediate Code for the Pascal Program

8.1.2 MACHINE - DEPENDENT CODE OPTIMIZATION

There are several different possibilities for performing machine-dependent codeoptimization .

-- Assignment and use of registers: Here we concentrate the use of registers asinstruction operand. The bottleneck in all computers to perform with high speed is theaccess of data from memory. If machine instructions use registers as operands the speedof operation is much faster. Therefore, we would prefer to keep in registers all variablesand intermediate result that will be used later in the program.

There are rarely as many registers available as we would like to use. The problemthen becomes which register value to replace when it is necessary to assign a register forsome other purpose. On reasonable approach is to scan the program for the next point atwhich each register value would be used. The value that will not be needed for thelongest time is the one that should be replaced. If the register that is being reassignedcontains the value of some variable already stored in memory, the value can simply bediscarded. Otherwise, this value must be saved using a temporary variable. This is one ofthe functions performed by the GETA procedure. In using register assignment, a compilermust also consider control flow of the program. If they are jump operations in theprogram, the register content may not have the value that is intended. The contents maybe changed. Usually the existence of jump instructions creates difficulty in keeping trackof registers contents. One way to deal with the problem is to divide the problem intobasic blocks.

A basic block is a sequence of quadruples with one entry point, which is at thebeginning of the block, one exit point, which is at the end of the block, and no jumpswithin the blocks. Since procedure calls can have unpredictable effects as registercontents, a CALL operation is usually considered to begin a new basic block. Theassignment and use of registers within a basic block can follow as described previously.When control passes from one block to another, all values currently held in registers aresaved in temporary variables.

For the problem is fig. .27, the quadruples can be divided into five blocks. They are:

Page 33: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software216

Block -- A Quadruples 1 - 3

Block -- B Quadruples 4

Block -- C Quadruples 5 - 14

Block -- D Quadruples 15 - 20

Block -- E Quadruples 21 - 23

Fig. 28

Fig. 28 shows the basic blocks of the flow group for the quadruples in fig. 27. Anarrow from one block to another indicates that control can pass directly from onequadruple to another. This kind of representation is called a flow group.

-- Rearranging quadruples before machine code generation:

Example : 1) DIV SUMSQ # 100 i1

2) * MEAN MEAN i2

3) - i1 i2 i3

4) : = i3 VARIANCE

LDA SUMSQ LDA T1

DIV # 100 SUB T2

STA T1 STA VARIANCELDA MEANMUL MEANSTA T2

Fig. 29

Fig. 29 shows a typical generation of machine code from the quadruples usingonly a single register.

Note that the value of the intermediate result, is calculated first and stored intemporary variable T1. Then the value of i2 is calculated subtracting i2 from ii.

Even though i2 value is in the register, it is not possible to perform the subtractionoperation. It is necessary to store the value of i2 in another temporary variable T2 and thenload the value of i1 from T1 into register A before performing the subtraction.

The optimizing compiler could rearrange the quadruples so that the secondoperand of the subtraction is computed first. This results in reducing two memoryaccesses. Fig. 29 shows the rearrangements.

* MEAN MEAN i2

DIV SUMSQ # 100 i1

A : 1 - 3

B : 4

C : 5 - 14

D : 15 - 20

E : 21 - 23

Page 34: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 217

- i1 i2 i3

:= i3 VARIANCE

LDA MEANMUL MEANSTA T1

LDA SUMSQDIV # 100SUB T1

STA VARIANCE

Fig. 29 Rearrangement of Quadruples for Code Optimization

-- Characteristics and Instructions of Target Machine: These may be special loop- control instructions or addressing modes that can be used to create more efficient objectcode. On some computers there are high-level machine instructions that can performcomplicated functions such as calling procedure and manipulating data structures in asingle operation.

Some computers have multiple functional blocks. The source code must berearranged to use all the blocks or most of the blocks concurrently. This is possible if theresult of one block does not depend on the result of the other. There are some systemswhere the data flow can be arranged between blocks without storing the intermediate datain any register. An optimizing compiler for such a machine could rearrange object codeinstructions to take advantage of these properties.

Machine Independent Compiler Features

Machine independent compilers describe the method for handling structuredvariables such as arrays. Problems involved in compiling a block-structured languageindicate some possible solution.

3.1 STRUCTURED VARIABLES

Structured variables discussed here are arrays, records, strings and sets. Theprimarily consideration is the allocation of storage for such variable and then thegeneration of code to reference then.

Arrays: In Pascal array declaration -

(i) Single dimension array: A: ARRAY [ 1 . . 10] OF INTEGER

If each integer variable occupies one word of memory, then we require 10 wordsof memory to store this array. In general an array declaration is ARRAY [ l .. u ] OFINTEGER

Memory word allocated = ( u - l + 1) words.

(ii) Two dimension array : B : ARRAY [ 0 .. 3, 1 . . 3 ] OF INTEGER

In this type of declaration total word memory required is 0 to 3 = 4 ; 1 - 3 = 3 ;4 x 3 = 12 word memory locations.

Page 35: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software218

In general: ARRAY [ l1 .. u1, l2 . . u2.] OF INTEGER Requires ( u1 - l1 + 1) * (u2 - l2 + 1) Memory words

The data is stored in memory in two different ways. They are row-major andcolumn major. All array elements that have the same value of the first subscript are storedin contiguous locations. This is called row-major order. It is shown in fig. 30(a). Anotherway of looking at this is to scan the words of the array in sequence and observe thesubscript values. In row-major order, the right most subscript varies most rapidly.

0,1 0,2 0,3 0,4 0,5 0,1 1,2 1,3 1,4 1,5 2,1 2,2 2,3 2,4 2,5 . . .

Row 0 Row 1 Row 2

Fig. 30 (a)

Fig. 30(b) shows the column major way of storing the data in memory. Allelements that have the same value of the second subscript are stored together; this iscalled column major order. In other words, the column major order, the left mostsubscript varies most rapidly.

To refer to an element, we must calculate the address of the referenced elementrelative to the base address of the array. Compiler would generate code to place therelative address in an index register. Index addressing mode is made easier to access thedesired array element.

(1) One Dimensional Array: On a SIC machine to access A [6], the address iscalculated by starting address of data + size of each data * number of preceding data.

i.e. Assuming the starting address is 1000HSize of each data is 3 bytes on SIC machineNumber of preceding data is 5

Therefore the address for A [ 6 ] is = 1000 + 3 * 5 = 1015. In general for A:ARRAY [ l . . u ] of integer, if each array element occupies W bytes of storage and if thevalue of the subscript is S, then the relative address of the referred element A[ S ] is givenby W * ( S - l ).

The code generation to perform such a calculation is shown in fig. 31.The notation A[ i2 ] in quadruple 3 specifies that the generated machine code

should refer to A using index addressing after having placed the value

A: ARRAY [ 1 . . 10 ] OF INTEGER...A[ I ] : = S

(1) - I # 1 i1

+ i1 # 3 i2

: = #5 A [ i1 ]

Fig. 31 Code Generation for Single Dimension Array of i2 in the Index Register

Page 36: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 219

(2) Multi-Dimensional Array: In multi-dimensional array we assume rowmajor order. To access element B[ 2,3 ] of the matrix B[ 6, 4 ], we must skip over twocomplete rows before arriving at the beginning of row 2. Each row contains 6 elements sowe have to skip 6 x 2 = 12 array elements before we come to the beginning of row 2 toarrive at B[ 2, 3 ]. To skip over the first two elements of row 2 to arrive at B[ 2, 3 ]. Thismakes a total of 12 + 2 = 14 elements between the beginning of the array and elementB[2, 3 ]. If each element occurs 3 byte as in SIC, the B[2, 3] is located relating at 14 x 3 =42 address within the array.

Generally the two dimensional array can be written as

B ; ARRAY [ l1 . . . u1, l1 . . . u1, ] OF INTEGERThe code to perform such an array reference is shown in fig. 32.

B : ARRAY [ 0 . . 3, 1 . . 6 ] OF INTEGER..B[I, J] : = 5

1) * I # 6 i1

-- j # 1 i2

+ i1 i2 i3

* i3 #3 i4

: = # 5 A [ i1 ]Fig. 32 Code Generation for Two Dimensional Array

The symbol - table entry for an array usually specifies the following:

The type of the elements in the array The number of dimensions declared The lower and upper limit for each subscript.

This information is sufficient for the compiler to generate the code required forarray reference. Some of the languages line FORTRAN 90, the values of ROWS andCOLUMNS are not known at completion time. The compiler cannot directly generatecode. Then, the compiler create a descriptor called dope vector for the array. Thedescriptor includes space for storing the lower and upper bounds for each array subscript.When storage is allocated for the array, the values of these bounds are computed andstored in the descriptor. The generated code for one array reference uses the values fromthe descriptor to calculate relative addresses as required. The descriptor may also includethe number of dimension for the array, the type of the array elements and a pointer to thebeginning of the array. This information can be useful if the allocated array is passed as aparameter to another procedure.

In the compilation of other structured variables like recode, string and sets thesame type of storage allocations are required. The compiler must store informationconcerning the structure of the variable and use the information to generate code toaccess components of the structure and it must construct a description for situation inwhich the required conformation is not known at compilation time.

Page 37: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software220

8.3.1 MACHINE - INDEPENDENT CODE OPTIMIZATION

One important source of code optimization is the elimination of common sub-expressions. These are sub-expressions that appear at more than one port in the programand that compute the same value. Let us consider the example in fig. 33.

x, y : ARRAY [ 0 . . 10, 1 . . 10 ] OF INTEGER...FOR I : = 1 TO 10 DO

X [ I, 2 * J - 1 ] : = [ I, 2 * J }

Fig. 33(a)

The sub-expression 2 * J is calculated twice. An optimizing compiler shouldgenerate code so that the multiplication is performed only once and the result is used inboth places. Common sub-expressions are usually detected through the analysis of anintermediate form of the program. This intermediate form is shown in fig. 33(b).

Line Operation OP 1 OP 2 Result Pascal Statement

1. : = #1 I [Loop initialization]2. JGT I #10 (20)3. - I #1 i1 [Subscript calculation for x]4. * i1 #10 i2

5. * #2 J i3

6. -- i3 #1 i4

7. -- i4 #1 i5

+ i2 i5 i6

9. * i6 #3 i7

10. -- I #1 i8 [Subscript Calculation for y]11. * i8 #10 i9

12. * #2 J i10

13. -- i10 3 1 i11

14. + i9 i11 i12

15. * i12 # 3 i13

16. : = y [ i13 } x [ i17 ] [Assignment Operation]17. + #1 I i17 [End of Loop]1 : = i14 I19. J (2)20. [Next Statement]

Fig. 33(b)

Examining fig. 33(b), the sequence of quadruples, we observe that quadruples 5and 12 are the same except for the name of the intermediate result produced. The operand

Page 38: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 221

J is not changed in value between quadruples 5 and 12. It is not possible to reachquadruple 12 without passing through quadruple 5 first because the quadruples are part ofthe same basic block. Therefore, quadruples 5 and 12 compute the same value. Thismeans we can delete quadruple 12 and replace any reference to its result ( i10 ), with thereference to i3, the result of quadruple 5. this information eliminates the duplicatecalculation of 2 * J which we identified previously as a common expression in the sourcestatement.

After the substitution of i3 for i10 , quadruples 6 and 13 are the same except forthe name of the result. Hence the quadruple 13 can be removed and substitute i4 for i11

wherever it is used. Similarly quadruple 10 and 11 can be removed because they areequivalent to quadruples 3 and 4.

Line Operation OP 1 OP 2 Result Pascal Statement

1. : = #1 I [Loop initialization]2. JGT I #10 (16)3. - I #1 i1 [Subscript calculation for x]4. * i1 #10 i2

5. * # 2 J i3

6. - i3 #1 i4

7. - i4 # 1 i5

+ i2 i5 i6

9. * i6 # 3 i7

10. + i2 i4 i12 [Subscript Calculation for y]

11. * i12 # 3 i13

12. : = y [ i13 ] x [i7 ] [assignment Operation]13. + #1 I i14 [End of Loop]14. : = i14 I15. J (2)16. [Next Statement]

Fig. 34

Names i1 have been left unchanged, except for the substitutions first described, tomake the compromise with fig. 33(b) easier. This optimized code has only 15 quadruplesand hence the time taken is reduced.

Another method of code optimization is the removal of loop invariants. Thereare sub-expressions within the loop whose values do not change from one iteration of theloop to the next. Thus the values can be calculated once, before the loop is entered, ratherthan being recalculated for each iteration. In the example shows in fig. 33(a), the loop-invariant computation is the term 2 * J [quadruple 5 fig. 34]. The result of thiscomputation depends only on the operand J, which does not change the value during theexecution of the loop. Thus we can move quadruple 5 in fig. 34 to a point immediatelybefore the loop is entered. A similar arrangement can be applied to quadruples 6 and 7.Fig. 35 shows the sequence of quadruples that result from these modification.

Page 39: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software222

The total number of quadruples remains the same as fig. 34, however, the numberof quadruples within the body of the loop has been reduced from 14 to 11. Ourmodification have reduced the total number of quadruples for one execution of the FORfrom 181 [Fig. 23 (b) ], to 114 [Fig 25], which saves a substantial amount of time.

Line Operation OP 1 OP 2 Result Pascal Statement

1. * #2 J i3 {Commutation of invariants}2. - i3 #1 i4

3. - i4 #1 i5

4. : = #1 I {Loop Initialization}5. JGT I #10 (16)6. - I #1 i1

7. * i1 #10 i2 {Subscript calculation for x}+ i2 i5 i6

9. * i6 #3 i7

10. + i2 i4 i12 {Subscript Calculation for y}11. * i12 #3 i13

12. : = y [ i13 ] x [i7 ] {assignment Operation}13. + #1 I i14 {End of Loop}14. : = i14 I15. J (5)16. {Next Statement}

Fig. 35

-- The optimization can be obtained by rewriting the source program.

Example; The statement in fig. 36(a) could be written as shown in fig. 36 (b).

FOR I : = 1 To 10 Dox [ I, 2 * J - 1 ] : = y [ I, 2 * J ]

Fig. 36(a)

T1 : = 2 * J ;T2 : = T1 -- 1 ;FOR : = 1 To 10 Dox [ I, T2 ] : = y[ I, T1]

Fig. 36(b)

This would achieve only a part of the benefits realized by the optimizationprocess described. Some time the statement in fig. 36(a) is preferable because it is clearerthan the modified version involving T1 and T2. An optimizing compiler should allow theprogrammer to write source code that is clearer and easy to read and it shouldcompile such a program into machine code that is efficient to execute.

-- Code optimization of another source is the substitution of a more efficientoperation for a less efficient one.

Page 40: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 223

Example: The FORTRAN statement:

Do 10 I = 1, 20 ; To calculate the first 20 power of 2 and store it inTABLE ( I ) = 2 * * I ; TABLEIn each iteration of the loop, the constant 2 is raised to the power I. The

quadruples are shown in fig. 37(a). Exponentiation is represented with the operationEXP.

This computation can be performed more efficiently. Here, in each iteration ofthe loop, the value of I is incremented by 1. The value of 2 * * I for the current iterationcan be found by multiplying the value for the previous iteration by 2. This method ofcomputing 2 * * I is much more efficient than performing series of multiplication orusing a logarithms technique.

This technique is shown in fig. 37(b).

Line Operation OP 1 OP 2 Result Pascal Statement

1. : = #1 I {Loop Initialization}2. EXP #2 I i1 { Calculation of 2 *3. -- I #1 i5 {Subscript calculation }4. * i2 #3 i3

5. : = i1 TABLE [ i2] {Assignment Operation}6. + I #1 i4 {End of the Loop}7. : = i4 I

JLE I #20 i3

Fig. 37(a)

Line Operation OP 1 OP 2 Result Pascal Statement

1. : = #1 i1 {Initialize temporaries}2. :: = # (-3) i3

3. : = #1 I {Loop Initialization}4. * i1 #2 i1 { Calculation of 2 * * I }5. + i3 #3 i3 {Subscript calculation }6. : = i1 TABLE [ i3] {Assignment Operation}7. + I #1 i4 {End of the Loop}

: = i4 I9. JLE I #20 (4)

Fig. 37(b)

STORAGE ALLOCATION

All the program defined variable, temporary variable, including the location usedto save the return address use simple type of storage assignment called static allocation.

When recursively procedures are called, static allocation cannot be used. This isexplained with an example. Fig. 38(a) shows the operating system calling the program

Page 41: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software224

MAIN. The return address from register 'L' is stored as a static memory locationRETADR within MAIN.

SYSTEM SYSTEM SYSTEM

(1) MAIN (1) MAIN (1) MAIN

(2) (2)(a) SUB

(b) (3)

(c) (c)

Fig. 38

In fig. 38(b) MAIN has called the procedure SUB. The return address for the callhas been stored at a fixed location within SUB (invocation 2). If SUB now calls itselfrecursively as shown in fig. 38(c), a problem occurs. SUB stores the return address forinvocation 3 into RETADR from register L. This destroys the return address forinvocation 2. As a result, there is no possibility of ever making a correct return to MAIN.

There is no provision of saving the register contents. When the recursive call ismade, variable within SUB may set few variables. These variables may be destroyed.However, these previous values may be needed by invocation 2 or SUB after the returnfrom the recursive call. Hence it is necessary to preserve the previous values of anyvariables used by SUB, including parameters, temporaries, return addresses, register saveareas etc., when a recursive call is made. This is accomplished with a dynamic storageallocation technique. In this technique, each procedure call creates an activation recordthat contains storage for all the variables used by the procedure. If the procedure is calledrecursively, another activation record is created. Each activation record is associatedwith a particular invocation of the procedure, not with the itself. An activation record isnot deleted until a return has been made from the corresponding invocation.

Activation records are typically allocated on a stack, with the correct record atthe tip of the stack. It is shown in fig. 39(a). Fig. 39(a) corresponds to fig. 39(b). Theprocedure MAIN has been called; its activation record appears on the stack. The baseregister B has been set to indicate the starting address of this correct activation record.The first word in an activation record would normally contain a pointer PREV to theprevious record on the stack. Since the record is the first, the pointer value is null. Thesecond word of the activation record contain a portion NEXT to the first unused word ofthe stack, which will be the starting address for the next activation record created. The

CALL SUB

RETADR

CALL SUB

RETADR

RETADR

CALL SUB

RETADR

RETADR

Page 42: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 225

third word contain the return address for this invocation of the procedure, and thenecessary words contain the values of variables used by the procedure.

SYSTEMS

MAIN

RETADRNEXT

0Stack

Fig. 39 (a)

SYSTEM(1) MAIN

B

SUBStack

Fig. 39(b)

In fig. 39 (b), MAIN has called the procedure SUB. A new activation record hasbeen created on the top of the stack, with register B set to indicate this new currentrecord. The pointers PREV and NEXT in the time records have been set as shown.

SYSTEM(1) MAIN

B

Fig. 39 (c) Stack

CALL SUB

VariablesFor SUB

RETADRNEXTPREV

VariableFor MAINRETADR

NE XT0

stacl

CALL SUB

VariablesFor SUB

RETADRNEXT

PREVVariable

For MAIN

RETADR

NEXT

PREVVariable

For MAINRETADR

NEXT0

CALL SUB

Page 43: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software226

In fig. 39(c), SUB has called itself recursively another activation record has beencreated for this current invocation for SUB. Note that the return address and variablevalues for the two invocations of SUB are kept separate by this process.

When a procedure returns to its caller, the current activation record (whichcorresponds to the most recent invocation) is deleted. The pointer PREV in the deletedrecord is used to reestablish the previous activation record as the current one, andexecution continues.

SYSTEM(1) MAIN

B

SUB

Fig. 39(d) Stack

Fig. 39(d) shows the stack as it would appear after SUB returns from therecursive call. Register B has been reset to point to the instruction record for the previousinvocation of SUB. The return address and all the variable values in this activation recordare exactly the same as they were before the recursive call.

This technique is called automatic allocation of storage. When the technique isused the compiler must generate code for the reference to variables using some sort ofrelative addressing. In our example the compiler assigns to each variable an address thatis relative to the beginning of the activation record, instead of an actual location withinthe object program. The address of the current activation record is, by conventioncontained in register B, so a reference to a variable is translated as an instruction that usesbase relative addressing. The displacement in this instruction is the relative address of thevariable within the activation record.

The compiler must also generate additional code to manage the activation recordsthemselves. At the beginning of each procedure there must be code to create a newactivation record, linking it to the previous one and setting the appropriate pointers asshown in fig. 39. This code is often called a prologue for the procedure. At the end of theprocedure, there must be code to delete the current activation record, resultingpointers as needed. This code is called an epilogue.

Example: IN FOTRAN 90 :ALLOCATE (MATRIX (ROWS, COLUMNS) )allocation storage for the dynamic array MATRIX with the specified dimensions.

DE-ALLOCATE MATRIXreleases the storage assigned to MATRIX by a previous ALLOCATE.

IN PASCAL: NEW (P)allocates storage for a variable and sets the pointer P to indicate the variable justcreated.

CALL SUB

VariablesFor SUB

RETADRNEXTPREV

VariableFor MAINRETADR

NEXT0

Page 44: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 227

DISPOSE (P)releases the storage that was previously assigned to the variable pointed to by P.

In C : MALLCO (SIZE) ; allocate a block of specified size...

FREE (P) ; frees the storage indicated by pointer P.

A variable that is dynamically allocated in this way does not occupy a fixedlocation in an activation record, so it cannot be referenced directly using base relativeaddressing. Such a variable is usually accessed using indirect addressing through apointer variable P. Since P does occupy a fixed location in the activation record, it can beaddressed in the usual way.

The mechanism to allocate a storage memory to a variable can be done in any ofthe following ways:

A NEW or MALLOC statement would be translated into a request bythe operating system for an area of storage of the required size.

The required allocation is handled through a run-time support procedureassociated with the compiler. With this method, a large block of freestorage called a heap is obtained from the operating system at thebeginning of the program. Allocations of the storage from the heap aremanaged by the run-time procedure.

In some systems, the program need not free memory for storage. A run-time garbage collection procedure scans the pointer in the program andreclaims areas from the heap that are no longer used.

8.3.3 BLOCK - STRUCTURED LANGUAGE

A block is a unit that can be divided in a language. It is a portion of a programthat has the ability to declare its own identifiers. This definition of a block is also meetthe units such as procedures and functions.

Let us consider a Pascal program with number of procedure blocks as shown infig. 40.

Each procedure corresponds to a block. Note that blocks are rested within otherblocks. Example: Procedures B and D are rested within procedure A and procedure C isrested within procedure B. Each block may contain a declaration of variables. A blockmay also refer to variables that are defined in any block that contains it, provided thesame names are not redefined in the inner block. Variables cannot be used outside theblock in which they are declared.

In compiling a program written in a blocks structured language, it is convenientto number the blocks as shown in fig. 40. As the beginning of each new block isrecognized, it is assigned the next block number in sequence. The compiler can thenconstruct a table that describes the block structure. It is illustrated in fig. 41. The block-level entry gives the nesting depth for each block. The outer most block numberthat is one greater than that of the surrounding block.

Page 45: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software228

PROCEDURE A ;VAR X, Y, Z : INTEGER ;

:PROCEDURE B ;VAR W, X, Y : REAL ;

:PROCEDURE C ;

VAR W, V : INTEGER ; 1: 3 2

END { C };:

END { B };:

PROCEDURE D ;VAR X, Z : CHAR ;

. 2

.END { D};

END { A};

Fig. 40 Nested Blocks in a Program

Block SurroundingName Number Level Block

ABCD

1234

1232

--121

Fig. 41

Since a name can be declared more than once in a program (by different blocks),each symbol-table entry for an identifier must contain the number of the declaring block.A declaration of an identifier is legal if there has been no previous declaration of thatidentifier by the current block, so there can be several symbolic table entries for the samename. The entries that represent declaration of the same name by different blocks can belinked together in the symbol table with a chain of pointers.

When a reference to an identifier appears in the source program, the compilermust first check the symbol table for a definition of that identifier by the current block. Ifnot such definition is found, the compiler looks for a definition by the block thatsurrounds the current one, then by the block that surrounds that and so on. If theoutermost block is reached without finding a definition of the identifier, then thereference is an error.

The search process just described can easily be implemented within a symboltable that uses hashed addressing. The hashing function is used to locate one definition ofthe identifier. The chain of definitions for that identifier is then searched for theappropriate entry.

Most block-structured languages make use of automatic storage allocation. Thevariables that are defined by a block are stored in an activation record that is created each

Page 46: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 229

time the block is entered. If a statement refers to a variable that is declared within thecurrent block, this variable is present in the current activation record, so it can beaccessed in the usual way. It is possible to refer to a variable that is declared in somesurrounding block. In that case, the most recent activation record for that block must belocated to access the variable.

Stack(a) (b)

Fig. 42 Use of Display for Procedure

A data structure called display is used to access a variable in surrounding blocks.The display contains pointers to the most recent activation records for the current blockand for all blocks that surround the current one in the source program. When a blockrefers to a variable that is declared in some surrounding block, the generated object codeuses the display to find the activation record that contains this variable.

Example:

When a procedure calls itself recursively thus an activation record is created onthe stack as a result of the call. Assume procedure C calls itself recursively. It is shown infig. 42(b) the record for C is created on the stack as a result of the call. Any reference to avariable declared by C should use this most recent activation record ; the display pointerfor C is changed accordingly. Variables that correspond to the previous invocation of Care not accessible for the movement, so there is no display pointer to thisactivation record.

Stack DisplayFig 42(c)

ActivationRecord for C

ActivationRecord for B

ActivationRecord for A

ActivationRecord for C

ActivationRecord for C

ActivationRecord for B

ActivationRecord for A

ActivationRecord for B

ActivationRecord for A

C

B

A

C

B

A

ActivationRecord for CActivation

Record for B

ActivationRecord for AActivationRecord for B

ActivationRecord for B

D

A

A

Page 47: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software230

Now if procedure 'C' call procedure D the resulting stack and display are asillustrated in fig. 42(c) . An activation record for D has been created in the usual way andadded to the stack. Note, that the display now contains only two pointers: one each to theactivation records for D and A. This is because procedure D cannot refer to variable in Bor C, except through parameters that are passed to it, even though it is called from C.According to the rules for the scope of names in as block-structured language, procedureD can refer only to variable that are declared by D or by some block that contains D inthe source program.

8.4 COMPILER DESIGN OPTIONS

The compiler design is briefly discussed in this section. The compiler is dividedinto single pass and multi pass compilers.

4.1. COMPILER PASSES

One pass compiler for a subset of the Pascal language was discussed in section 1.In this design the parsing process drove the compiler. The lexical scanner was calledwhen the parser needed another input token and a code-generation routine was invoked asthe parser recognized each language construct. The code optimization techniquesdiscussed cannot be applied in total to one-pass compiler without intermediate code-generation. One pass compiler is efficient to generate the object code.

One pass compiler cannot be used for translation for all languages. FORTRANand PASCAL language programs have declaration of variable at the beginning of theprogram. Any variable that is not declared is assigned characteristic by default.

One pass compiler may fix the formal reference jump instruction withoutproblem as in one pass assembler. But it is difficult to fix if the declaration of anidentifier appears after it has been used in the program as in some programminglanguages.

Example: X : = Y * Z

If all the variables x, y and z are of type INTEGER, the object code for thisstatement might consist of a simple integer multiplication followed by storage of theresult. If the variable are a mixture of REAL and INTEGER types, one or moreconversion operations will need to be included in the object code, and floating pointarithmetic instructions may be used. Obviously the compiler cannot decide what machineinstructions to generate for this statement unless instruction about the operands isavailable. The statement may even be illegal for certain combinations of operand types.Thus a language that allows forward reference to data items cannot be compiled in onepass.

Some programming language requires more than two passes. Example :ALGOL-98 requires at least 3 passes.

There are a number of factors that should be considered in deciding between onepass and multi pass compiler designs.

(1) One Pass Compiles: Speed of compilation is considered important.Computer running students jobs tend to spend a large amount of time performingcompilations. The resulting object code is usually executed only once or twice for each

Page 48: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 231

compilation, these test runs are not normally very short. In such an environment,improvement in the speed of compilation can lead to significant benefit in systemperformance and job turn around time.

(2) Multi-Pass Compiles: If programs are executed many times for eachcompilation or if they process large amount of data, then speed of executive becomesmore important than speed of compilation. In a case, we might prefer a multi-passcompiler design that could incorporate sophisticated code-optimization technique.

Multi-pass compilers are also used when the amount of memory, or othersystems resources, is severely limited. The requirements of each pass can be kept smallerif the work by compilation is divided into several passes.

Other factors may also influence the design of the compiler. If a compiler isdivided into several passes, each pass becomes simpler and therefore, easier tounderstand, read and test. Different passes can be assigned to different programmers andcan be written and tested in parallel, which shortens the overall time require for compilerconstruction.

INTERPRETERS

An interpreter processes a source program written in a high-level language. Themain difference between compiler and interpreter is that interpreters execute a version ofthe source program directly, instead of translating it into machine code.

An interpreter performs lexical and syntactic analysis functions just like compilerand then translates the source program into an internal form. The internal form may alsobe a sequence of quadruples.

After translating the source program into an internal form, the interpreterexecutes the operations specified by the program. During this phase, an interpreter can beviewed as a set of subtractions. The internal form of the program drives the execution ofthis subtraction.

The major differences b/w interpreter and compiler are:

Interpreters Compilers

Page 49: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software232

Most programming languages can be either compiled or interpretedsuccessfully. However, some languages are particularly well suited to the use ofinterpreter. Compilers usually generate calls to library routines to perform function suchas I/O and complex conversion operations. In such cases, an interpreter might beperformed because of its speed of translation. Most of the execution time for the standardprogram would be consumed by the standard library routines. These routines would bethe same, regardless of whether a compiler or an interpreter is used.

In some languages the type of a variable can change during the execution of aprogram. Dynamic scoping is used, in which the variable that are referred to by afunction or a subroutines are determined by the sequence of calls made during execution,not by the nesting of blocks in the source program. It is difficult to compile suchlanguage efficiently and allow for dynamic changes in the types of variables and thescope of names. These features can be more easily handled by an interpreter that providesdelayed binding of symbolic variable names to data types and locations.

4.3 P-CODE COMPILERS

P-Code compilers also called byte of code compilers are very similar in conceptto interpreters. A P-code compiler, intermediate form is the machine language for ahypothetical computers, often called pscudo-machine or P-machine. The process of usingsuch a P-code is shown in fig, 43.

The main advantage of this approach is portability of software. It is not necessaryfor the compiler to generate different code for different computers, because the P-codeobject program can be executed on any machine that has a P-code interpreter. Even thecompiler itself can be transported if it is written in the language that it compiles. Toaccomplish this, the source version of the compiler is compiled into P-code; this P-codecan then be interpreted on another compiler. In this way P-code compiler can be usedwithout modification as a wide variety of system if a P-code interpreter is written foreach different machine.

Source Program

1) The process of translating a sourceprogram into some internal form issimpler and faster

The process of translating a sourceprogram into some internal form isslower than interpreter.

2) Execution of the translated programis much slower.

Executing machine code is much faster.

3) Debugging facilities can be easilyprovided.

Provision of bugging facilities aredifficult and complicated.

4) During execution the interpreterproduce symbolic dumps of datavalues, trace of program executionrelated to the source statement.

The compiler does not producesymbolic dumps of date value.Debugging tools are required fortrace the program.

5) Program testing can be doneeffectively using interpreter as theoperation on different data can betraced.

It is difficult to test as the compilerexecution file gives the final result.

6) Easy to handle dynamic scoping Difficult to handle dynamic scooping

Page 50: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

Compilers 233

Object ProgramP - Code

Fig. 43

The design of a P-machine and the associated P-code is often related to therequirements of the language being compiled. For example, the P-code for a Pascalcompiler might include single P-instructions that perform:

Array subscript calculation Handle the details of procedure entry and exit and Perform elementary operation on sets

This simplifies the code generation process, leading to a smaller and moreefficient compiler.

The P-code object program is often much smaller than a corresponding machinecode program. This is particularly useful on machines with severely limited memory size.

The interpretive execution of P-code program may be much slower than theexecution of the equivalent machine code. Many P-code compilers are designed for asingle user running on a dedicated micro-computer systems. In that case, the speed ofexecution may be relatively insignificant because the limiting factor is systemperformance may be the response time and " think time " of the user.

If execution speed is important, some P-code compilers support the use ofmachine-language subtraction. By rewriting a small number of commonly used routinesin machine language, rather than P-code, it is often possible to improve the performance.Of course, this approach sacrifices some of the portability associated with the use of P-code compilers.

8.4.2 COMPILER-COMPILERS

Compiler-Compiler is software tool that can be used to help in the task ofcompiler construction. Such tools are also called Compiler Generators or Translator -writing system.

The process of using a typical compiler-compiler is shown in fig. 44. Thecompiler writer provides a description of the language to be translated. This descriptionmay consists of a set of lexical rules for defining tokens and a grammar for the sourcelanguage. Some compiler-compilers use this information to generate a scanner and aparses directly. Others create tables for use by standard table-driven scanning and parsingroutines that are supplies by the compiler - compiler.

Compiler P-CodeCompiler

P - CodeInterpreterExecute

Page 51: COMPILERS - Atria | e-Learning · COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent

System Software234

Lexical Ruler

Grammar

SemanticRoutines

Fig. 44

The compiler writer also provides a set of semantic or code-generation routines.There is one such routine for each rule of the grammar. The parser each time itrecognizes the language construct described by the associated rule calls this routine.Some compiler-compiler can parse a longer section of the program before calling asemantic routine. In that case, an internal form of the statements that have been analyzed,such as a portion of the parse tree, may be passed to the semantic routine. This approachis often used when code optimization is to be performed. Compiler-compilers frequentlyprovide special languages, notations, data structures, and other similar facilities that canbe used in the writing of semantic routines.

The main advantage of using a compiler-compiler is case of compilerconstruction and testing. The amount of work required from the user varies considerablyfrom one compiler to another depending upon the degree of flexibility provided.Compilers that are generated in this way tend to require more memory and compileprograms more slowly than hand written compilers. However, the object code generatedby the compiler may actually be better when a compiler-compiler is used. Because of theautomatic construction of scanners and parsers and the special tools provided for writingsemantic routines, the compiler writer is freed from many of the mechanical details ofcompiler construction. The write can therefore focus more attention on good codegeneration and optimization.

Compiler-Compiler Scanner

Parser

CodeGeneratorCompiler