0% found this document useful (0 votes)
42 views12 pages

System Programming: Design Problem

The document discusses implementing the steps of a compiler for a given program module. It includes: 1. Performing lexical analysis to identify tokens such as identifiers, keywords, operators, and special symbols. 2. Constructing syntax trees for two operations in the program. 3. Performing semantic analysis including type checking and symbol table construction. 4. Generating intermediate code in the form of a 3-address code. 5. Optimizing the code in terms of memory allocation through techniques like common subexpression elimination.

Uploaded by

Anjani Kunwar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views12 pages

System Programming: Design Problem

The document discusses implementing the steps of a compiler for a given program module. It includes: 1. Performing lexical analysis to identify tokens such as identifiers, keywords, operators, and special symbols. 2. Constructing syntax trees for two operations in the program. 3. Performing semantic analysis including type checking and symbol table construction. 4. Generating intermediate code in the form of a 3-address code. 5. Optimizing the code in terms of memory allocation through techniques like common subexpression elimination.

Uploaded by

Anjani Kunwar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

2010

SYSTEM PROGRAMMING
DESIGN PROBLEM

SUBMITTED TO:
SUBMITTED BY :
RUCHI MAM
ANJANI KUNWAR
RA1803A10
10807973
B.TECH(CSE)-H
DESIGN PROBLEM

Implements the steps of a compiler for the following program module

While(A<C AND B>D OR E>F)

If (A= =1) call ADD(A+B*-C,-D- -F*-G)

Else

W[I,J]=X[I,J]+Y[W[K,L]]+Z[I+J]

U[I,J,K]=V[I,J,K,L]

Expectaions:

1.Find out all the tokens generated by lexical analyzer.

2.Perform the syntax tree for last two operations used in the program.

3.Perform the semantic analysis over the syntax.

4.Evaluate the intermediate code(3 address code) for the given loop.

5.Optimize the code in terms of memory allocation.

(i) Lexical analysis


Lexical or scanning is the process where the stream of characters making up the
source program is read from left-to-right and grouped into tokens. Tokens are sequences of
characters with a collective meaning. There are usually only a small number of tokens for a
programming language: constants (integer, double, char, string, etc.), operators (arithmetic,
relational, logical), punctuation, and reserved words.

There are three tasks for the compiler to perform in the lexical analysis phase i.e.
1. To convert the source program into the basic elements; tokens of the
language.
2. To build a literal table and an identifier table.
3. To build a uniform symbol table.

Before it we declare n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,n15,n16,n17 which


stand for pointers to the symbol table entries for A,B,C,D,E,F,W,X,Y,Z,U,V,I,J,K,L,1.

We can write the expression in the form:-

While ([id,n1])<[id,n3] AND [id,n2] > [id,n4] OR [id,n5] > [id,n6] )


{
If ([id,n1] = = [const , n17]) call ADD([id,n1] + [id,n2] * -[id,n3] , -[id,n4] –[id,n6]* -
[id,n7]);
Else
[Id, n7] [[id, n13], [id, n14]] = [id, n8] [[id, n13], [id, n14]] + [id, n9] [[id, n7] [[id, n15],
[id, n16]]] + [id, n10] [[id, n13] + [id, n14]]
[id, n11] [[id, n13], [id, n14], [id, n15]] = [id, n12] [[id, n13], [id, n14], [id, n15], [id, n16]]
}

Lexeme Terminal Description


int T_INT reserved word
a T_IDENTIFIER variable name
; T_SPECIAL special symbol with value of ";"
A T_IDENTIFIER variable name
= T_OP operator with value of "="
a T_IDENTIFIER variable name
+ T_OP operator with value of "+"
2 T_INTCONSTANT integer constant with value of 2
; T_SPECIAL special symbol
We indicate parenthically the particular name of a constant to which each instance of token id
and constant refer.

According to the ques. All elements are divided into following categories:-
a) Identifier-it basically uses id as a symbol or token class code for the identifier. Numbers
can be used as per our requirements.

so ,here are all the identifiers and all the corresponding tokens used for them.

SR NO TOKEN

I.A id#1

II.B id#2

III.C id#3

IV.D id#4

V .E id#5

VI.F id#6

VII. G id#7

VIII .W id#8

IX .X id#9

X. Y id#10

XII . Z id#11

XIII .U id#12

XIV .V id#13

XV . I id#14

XVI .J id#15

XVII.K id#16

XVII.L id#17
b).keywords-it is a name that has a special meaning and functionality. It can be also used to
represent symbol or as an operator. In this the class code is a key.

SR NO TOKEN

I .if key#1

II .while key#2

III . ADD key#3

IV. else key#4

3.operator-their main purpose is to compare between the various variables and identifiers .they
are represented by the symbol (op)

SR NO TOKEN

I .< op#1

II. > op#2

III. AND op#3

IV. OR op#4

V. + op#5

VI. - op#6

VII. * op#7

VIII.== op#8
4.special symbol-these are special notations like array parameter etc…the class code is (ss)

SR NO TOKEN

I.( ss#1

II.) ss#2

III .[ ss#3

IV.] ss#4

V., ss#5
Expressing the TOKENS in EXPRESSION:-
While, A,(,<,C,ADD,B,>,D,OR,E,F,),if, = =, 1,ADD,C,+,*,-,else, W,[,I,J,K,L,],X,Y,Z,U,V

(ii) SYNTAX TREE:-


W [I, J] = X [I, J] + Y [W [K, L]] + Z [I, J]
U [I, J, K] = V [I, J, K, L]
(iii) SEMANTIC ANALYSIS:-

Semantic analysis, where we delve even deeper to check whether they form a sensible set of
instructions in the programming language. Whereas any old noun phrase followed by some verb
phrase makes a syntactically correct English sentence, a semantically correct one has subjectverb
agreement, proper use of gender, and the components go together to express an idea that
makes sense. For a program to be semantically valid, all variables, functions, classes, etc. must
be properly defined, expressions and variables must be used in ways that respect the type system,
access control must be respected, and so forth. Semantic analysis is the front end’s penultimate
phase and the compiler’s last chance to weed out incorrect programs. We need to ensure the
program is sound enough to carry on to code generation.
A large part of semantic analysis consists of tracking variable/function/type declarations and type
checking.

Major Semantic Tasks:-


1. Inheritance graph construction and checking.
2. Symbol table construction
a. Construct symbol table for features using inheritance tree.
b. Assign enclosing scope for each AST node
3. Scope Checking:-
a. Check program using symbol table
4. Check for main class and main method.
5. Type Checking
a. Uses inheritance graph and symbol tables.
b. Assign type for each AST node appearing in its input, which is the output of the
lexical analysis which occurs in patterns that are permitted by the specification for
the language. It also imposes on the tokens a tree like structure that is used by the
subsequent phase of the compiler.

(iv) INTERMEDIATE CODE: - On logical level the output of the syntax


analyser in somerepresentation of a parse tree. The intermediate code generation phase
transforms this parse treeinto an intermediate language representation of the source program.

L1: If A<C AND B>D OR E>F goto L2


goto L5

L2: if A== 1 goto L3


goto L4

L3: T1: B* (-C)


T2: A + T1
T3: (-F)*(-G)
T4: (-D) – T3
Call ADD (T2, T4)
Goto L1

L4: T5 = I+J
T6: X [I, J] + Y [W [K, L]]
T7: T6 + Z [T5]
W [I, J] = T7 goto L1
U [I, J, K] = V [I, J, K, L]

L5: END

(v) Optimization of code :-


Optimization is the process of transforming a piece of code to make more efficient (either in
terms of time or space) without changing its output or side-effects. The only difference visible to
the code’s user should be that it runs faster and/or consumes less memory. It is really a misnomer
that the name implies you are finding an "optimal" solution— in truth, optimization aims to
improve, not perfect, the result.

Optimization is the field where most compiler research is done today. The tasks of the front-end
(scanning, parsing, semantic analysis) are well understood and unoptimized code generation is
relatively straightforward. Optimization, on the other hand, still retains a sizable measure of
mysticism. High-quality optimization is more of an art than a science. Compilers for mature
languages aren’t judged by how well they parse or analyze the code—you just expect it to do it
right with a minimum of hassle—but instead by the quality of the object code they produce.

MEMORY ALLOCATION TABLES:-

You might also like