Compiler Assignment
Compiler Assignment
Submitted to
NAME: T. ABRHAM
DATE: may/2023
EXERCISE
1, Consider the line of C++ code: float[index] = a-c. write its:
A. Lexical analysis C. Semantic analysis E. Code generator
B. Syntax analysis D. Intermediate code generator
2) What is compiler?
3) Why designing a compiler is necessary?
4) What are the main phases of compiler construction? Explain each.
Answer
1) A)
int[index]=(4+2)
Int – identifier
[ - left bracket
Index – identifier
] - right bracket
= - assignment operator
( - left bracket
4 - number
+ - plus operator
2 - number
) - right bracket
b) Syntax analysis
Assign expression
Expression = Expression
Int index 4 2
c) Semantic analysis
Assign expression
d)
temp1(t1)=-c
temp2(t2)=a-t1
float[index]=temp2(t2)
e)
MovF R2,id3
MovF R1,id1
SUBF R1,R2
MovF id1,R1
2)what is compiler?
A compiler is a program that reads a program written in one language -the
source language and translates it into an equivalent program in another
language-the target language. The compiler reports to its user the presence
of errors in the source program.
In other word in computing, a compiler is a computer program that translates
computer code written in one programming language (the source language) into
another language (the target language). The name "compiler" is primarily used for
programs that translate source code from a high-level programming language to a
low-level programming language (e.g., assembly language, object code, or
machine code) to create an executable program.
There are many different types of compilers which produce output in different
useful forms. A cross-compiler produces code for a different CPU or operating
system than the one on which the cross-compiler itself runs. A bootstrap compiler
is often a temporary compiler, used for compiling a more permanent or better
optimized compiler for a language.
Importance of a Compiler
Assignment 1
1) Consider the line of C code: int[index] = (4 + 2) . write its:
A. Lexical analysis C. Semantic analysis E. Code generator
B. Syntax analysis D. Intermediate code generator
Expression = Expression
float index a c
c) semantic analysis
Assign expression
d)
temp1(t1)=2
temp2(t2)=4+t1
int[index]=temp2(t2)
e)
Mov R2,id3
Mov R1,id1
ADD R1,R2
Mov id1,R1
2) Code
written by programmer
High level language
Translate the code
Assembly language
Assembler translate the code in to
Machine language displays
Out put
a) The role of compiler in this action is to convert high level language code to
machine (object) language. compilers can take a while, because they have to
translate high level code to lower-level machine language all at once and then
save the executable object code to memory.
b) The another program used this process is an interpreter is a computer program
that directly executes instructions written in a programming or scripting language,
written out requiring them previously to have been compiled in to a machine
language program.
And it is differentiated from the compiler by -compiler take a program as a whole.
An interpreter takes a single line of code and the interpreter never generate only
intermediate code generator.
solution
1.String of a's and b's that start and end with a.
L={aa,aaa,aba....}
2.String of a's and b’s.
L={Ɛ,ab,abab,...}
3.String of a's and b's that the character third from the last is a.
L={aaa,aaba,.....}
4.String of a's and b's that only contains three b.
L={bbb,abbb,ababb...}
Exercise 2
Construct DFAs for the string matched by the following definition:
1.digit =[0-9]
2.nat=digit+
3.signednat=(+|-)?nat
4.number=signednat(“.”nat)?(E signedNat)?
Answer
1) digit
digit
2)
+ digit
Digit
-
3)
+ digit
Digit digit
-
digit
4) A DFA of number
+ digit digit + digit