0% found this document useful (0 votes)
78 views

Compiler Assignment

The document discusses a group assignment for a Compiler Design course, which includes questions about the lexical, syntax, and semantic analysis of a line of C++ code, as well as the purpose of compilers and the main phases of compiler construction. It also provides sample answers explaining the lexical, syntax, and semantic analysis of a line of C code, and describing the role of compilers in translating high-level code to machine code.

Uploaded by

3d nat nati
Copyright
© © All Rights Reserved
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)
78 views

Compiler Assignment

The document discusses a group assignment for a Compiler Design course, which includes questions about the lexical, syntax, and semantic analysis of a line of C++ code, as well as the purpose of compilers and the main phases of compiler construction. It also provides sample answers explaining the lexical, syntax, and semantic analysis of a line of C code, and describing the role of compilers in translating high-level code to machine code.

Uploaded by

3d nat nati
Copyright
© © All Rights Reserved
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/ 11

AMBO UNIVERSITY WOLISO CAMPUS

Department of Computer science


COMPILER DESIGN
Group- assignment

 Course Title: Compiler Design


 Course Code: CoSc 3112

Student name I.D. Number


 NATNAEL TSEGAYE ------------------------------------------- UGR/50257/13
 TAMIRAT TADESSE ------------------------------------------- UGR/51940/13
 BIRUKTAWIT TADESSE --------------------------------------- UGR/51923/13

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

Subscript expression additive expression

Expression [ Expression ] ( Expression + Expression )

keyword identifier number number

Int index 4 2

c) Semantic analysis

Assign expression

Subscript expression (integer) additive expression(integer)

keyword identifier number number


int index 4 2
Array of integer integer integer integer

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.

3) Why designing a compiler is necessary?


The purpose of a compiler is to ensure that the programmer’s intentions
are correctly translated into a form that the computer can understand.
Code written in a high-level programming language, such as Python or Java,
must first be converted into machine language before it can be run on a
computer. This process is handled by compilers.
Without compilers, programmers would have to write code in machine
language, which is difficult and time-consuming. Compilers make it possible
for programmers to use high-level programming languages, which are
easier to learn and use than machine language.
Compilers also play an important role in software development and
debugging. They allow developers to test their code and locate errors
without running the program on a computer. This helps save time and
makes development more efficient.
Debugging tools rely heavily on compilers to perform their tasks correctly.
In short, compilers are an essential part of the software development
process and play a vital role in making coding easier and more efficient for
programmers everywhere.

Importance of a Compiler

 Typically, a programmer writes code in a high-level language which is


easier and faster to write than machine code. The code is then run through a
compiler which converts it into machine code, also called object code, which
the computer can understand.
 The advantage of using a compiler is that it allows programmers to write
code in a high-level language that is easier to read and understand. It also
makes it possible to write more portable code, meaning it can be easily
moved from one type of computer to another.
4) What are the main phases of compiler construction? Explain each
 There are two phases of compilation.
o Analysis (Machine Independent/Language Dependent)
o Synthesis (Machine Dependent/Language independent)
 Lexical Analyzer:- The lexical analyzer phase reads the character stream
from the source program and groups them into meaningful sequences by
identifying tokens. Scanning is another term for it.
 Syntax Analyzer:- This phase is also known as parsing. The tokens from
the previous phase are used to create an intermediate tree-like data
structure known as the syntax tree in this phase. Each node has an
operator, and the operator's operands are the node's children. This is
primarily done to ensure that the syntax of the given statements is correct
and adheres to the language's pre-defined rules. If the syntax is incorrect, it
generates a syntax error.
 Semantic Analyzer:- The semantic analyzer checks if the source code is
semantically consistent, that is, if it conveys the intended meaning, using
the syntax tree from the previous phase and the symbol table.
 Intermediate Code Generator:- After syntax and semantic analysis,
many compilers generate an explicit low-level, machine-like, intermediate
code. This code has two key characteristics:
1. It shouldn't take up much CPU time or memory.
2. It should be straightforward to convert to machine code.
After performing semantic analysis, the compiler generates an intermediate
code of the source code for the target machine. It's a programmer for some
kind of abstract machine. It's a cross between a high-level and a machine
language.

 Code Optimization: - The intermediate code is optimized in the next


phase. Optimization can be defined as removing unnecessary code lines
and rearranging statement sequences to speed up program execution
without wasting resources.
 Code Generation: - The code generator maps the optimized
representation of the intermediate code to the target machine language in
this phase. The code generator converts the intermediate code into a re-
locatable sequence of machine code. A machine code sequence of
instructions performs the same task as intermediate code.

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

2) Explain diagrammatically how high level languages can be changed to


machine understandable language(machine code). After that:
a) What is the role of compilers in this action?
b) What are another related programs used in this process and
what makes them different from compilers.
Answer
1) a) lexical analysis
int - keyword
[ - left bracket
Index - identifier(id1)
] - right bracket
= - assignment
( - left parenthesis
4 - number
+ - plus operator
2 - number
) - right parenthesis
; - semicolon
b) syntax analysis
Assign expression

Expression = Expression

Subscript expression additive expression

Expression [ Expression ] Expression - Expression

keyword identifier number number

float index a c

c) semantic analysis
Assign expression

Subscript expression(integer) additive expression(integer)

keyword identifier number number


float index a c
Array of integer integer integer integer

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.

Chapter two exercise


 Describe the languages denoted by the following regular
expressions:
1. a(a|b)*a
2. ((ε|a)b*)*
3. (a|b)*a(a|b)(a|b)
4. a*ba*ba*ba*

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

Digit digit E digit


- -
digit E digit

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy