You are on page 1of 12

‫برمجة النظم‬

(١٢) ‫محاضرة‬ ‫رابعة حاسب‬

‫ أحمد الحربي‬/‫دكتور‬

Compilers

In this subject, we concentrate on the following two purposes.

1- To present a general model of a compiler that may be used as a basis

for designing and studying compilers.

2- To create an evaluation of the difficulty and ‘cost’ of implementing

and using particular features of languages.

To accomplish this, the above topic has been divided into three parts.

¾ PART1: presents a simple example and introduces a general

model of a compiler.

¾ PART2: study the model and explains its inner workings in

detail.

1
¾ PART3: uses the model to demonstrate the implementation of

advanced features, e.g., data structures, storage allocation,

block structure, and pointers.

A compiler accepts a program written in a higher level language as

input and produces its machine language equivalent as output.

In the part one, we examine a simple PL/1 program and become

familiar with the issues we must face in trying to compile it.

WCM: PROCEDURE (RATE, START, FINISH);


DECLARE (COST, RATE, START, FINISH) FIXED BINARY (31) STATIC;
COST = RATE * (START - FINISH) + 2 * RATE * (START – FINISH – 100);
RETURN (COST);
END;
Figure 8.1 MINI-PL/1 program example

What must the compiler do in order to produce the machine language

equivalent of WCM?

:‫يتم اإلجابة على ذلك من خالل معرفة أربع أشياء كما يلي‬

:‫ على سبيل المثال‬، ‫( معرفة العناصر األساسية‬Recognizing Basic Elements) -١

.operator ‫ وأن = تكون‬،label ‫ تكون‬WCM ‫ وأن‬،variable ‫ تكون‬COST

2
ً ‫( معرفة تجميع العناصر معا‬Recognizing Units and Interpreting Meaning) -٢

‫ مع ثالث‬procedure ‫ الجملة األولى تعرف‬:‫ على سبيل المثال‬،‫ وفھم معانيھم‬،‫كوحدات‬

FIXED BINARY ‫ لتكون أعداد‬variables ‫ تعرف أربع‬:‫ والجملة التالية‬،arguments

‫ والتي يتم تنفيذھا‬assignment statement ‫ تكون جملة تحديد‬:‫ والجملة الثالثة‬،31 bits ‫ذات طول‬

‫ حيث يتم الرجوع بقيمة‬return ‫ ھى جملة‬:‫ والجملة األخيرة‬.seven computations ‫من خالل‬

.cost ‫المتغير‬

.‫ لك المتغيرات في البرنامج‬locations ‫ وتعين‬storage ‫( تحديد‬Storage Allocation) -٣

.‫ المناسب‬object code ‫( تكوين‬Code Generation) -٤

:‫ھذه النقاط األربع سوف يتم دراستھم بالتفصيل كما يلي‬

Problem No. 1- Recognizing Basic Elements

The action of parsing the source program into the proper syntactic

classes is known as lexical analysis. The program is scanned and separated

as shown in Figure 8.2.

source ‫ حيث أن‬.‫ بسيطة‬string processing ‫التفاصيل لھذه الخطوة تتطلب عمل تقنيات‬

operators, ‫ وتعرف‬،‫ بمسافات‬basic elements or tokens ‫ وتحدد‬،‫ يفحص بالتتابع‬program

3
‫ كـ‬literals, or terminal symbols ‫ وكذلك تعرف‬،identifiers ‫ كـ‬and special symbols

.operators, or keywords

The basic elements (identifiers and literals) are placed into tables. As

other phases recognize the use and meaning of the elements, further

information is entered into these tables (e.g., precision, data type, length, and

storage class).

Other phases of the compiler use the attributes of each basic element

and must therefore have access to this information. Uniform symbols are of

fixed size and consists of the syntactic class and a pointer to the table entry of

the associated basic element. Figure 8.3 views uniform symbolic for users.

4
This lexical process can be done in one continuous pass through the

data by creating an intermediate form of the program consisting of a chain or

table of tokens.

Some compilers reduce the size of the token table by only parsing

tokens as necessary, and discarding those that are no longer needed. The

lexical phase also discards comments since they have no effect on the

processing of the program.

Notice that the uniform symbol is the same length whether the token is

1 or 31 characters long.

5
Problem No.2 – Recognizing Syntactic Units and Interpreting
Meaning

Once the program has been broken down into tokens or uniform

symbols, the compiler must (1) recognize the phases, each phase is a

semantic entity and is a string of tokens that has an associated meaning, and

(2) interpret the meaning of the constructions.

The program is scanned and separated as shown in Figure 8.4. There

are many ways of operationally recognizing the basic constructs and

interpreting their meaning.

6
Intermediate Form

object code ‫ فإن المترجم يستطيع تكوين‬syntactic construction ‫بعد تحديد‬

:‫ قد تكون جمل‬construction ‫ وھذه‬،construction ‫لكل‬

Arithmetic, nonarithmetic, or nonexecutable statements

Arithmetic statements

One intermediate form of an arithmetic statement is a parse tree. Figure

8.5 describe this method. The rules for converting an arithmetic statement

into a parse tree are:

1- Any variable is a terminal node of the tree.

2- For every operator, construct a binary tree whose left branch is the tree

for operand 1 and whose right branch is the tree for operand 2.

7
Although, the above technique makes it easy for us to visualize the

structure of the statement, it is not a practical method for a compiler. The

compiler may use an intermediate form as a linear representation of the

parse tree called a matrix, as shown in Figure 8.6.

‫ كما ھو محدد‬،M7 ‫ إلى‬M1 ‫يتم حساب المعادلة الرياضية على سبع مراحل متتالية من‬

:‫بالشكل التالي‬

8
Nonarithmetic Statement

The nonarithmetic statements, such as DO, IF, GO TO, END, etc., can

all be replaced by a sequential ordering of individual matrix, as shown in Fig.

8.7.

9
Nonexecutable Statements

These statements are like DECLARE, in our example the interpretation

phase would note the data type, precision, and storage class (FIXED

BINARY, 31 bits, STATIC) in the identifier table for each of the variables

COST, RATE, START, and FINISH (as shown in Fig. 8.8).

Problem No.3 – Storage Allocation

At some time, the amounts of storage required by our program must be

reserved. In our example the DECLARE statement gives us the proper

information:

10
The interpretation phase constructs the entries in the table of Fig. 8.8. In

the case of fixed binary numbers of 32 bits, it will assign the first to relative

location 0, the second to location 4, and so on. One bit is assigned for sign.

Similarly, storage is assigned for the temporary locations that will

contain intermediate results of the matrix (e.g., M1, M2, M3, M4, M5, M6,

and M7).

11
Problem No.4 – Code Generation

Once the compiler has created the matrix and tables of supporting

information, it may generate the object code.

12

You might also like