Compilers || 3rd stage Shahad Ali
Compilers Lab - Lecture 1
A compiler is a program that translates source code written in a high-level programming
language into machine code that can be executed by a computer. This process of translation is
known as compilation.
A computer program goes through many phases from its development to execution. From the
human readable format (source code) to binary encoded computer instructions (machine code).
Compilation Steps/Phases:
● Lexical Analysis Phase: Generates the “tokens” in the source program.
● Syntax Analysis Phase: Recognizes “sentences" in the program using the syntax of the
language.
● Semantic Analysis Phase: Infers information about the program using the semantics of
the language.
● Intermediate Code Generation Phase: Generates “abstract” code based on the
syntactic structure of the program and the semantic information from Phase 2.
● Optimization Phase: Refines the generated code using a series of optimizing
transformations.
● Final Code Generation Phase: Translates the abstract intermediate code into specific
machine instructions.
Compilers || 3rd stage Shahad Ali
Lexical Analyzer:
The Lexical Analyzer begins the analysis of the source program by reading the input,
character by character, grouping characters into individual words and [Link] Words
and Symbols are called Tokens. Blanks are eliminated during Lexical [Link]
Analyzer is also called as Scanner, because it scans the source Program and converts it into
Tokens.
Example:
int Num1 = 4;
cout>>Num1 + 2;
The lexical analyzer will split the code above into tokens as follows:
Token Type
int keyword
Num1 identifier
= operator
4 digit
; special character
cout keyword
Compilers || 3rd stage Shahad Ali
>> operator
Num1 identifier
+ operator
2 digit
; special character
For the first lab we will find out how compilers identify keywords which they’re special
words that are reserved to the programming language and each one have a certain job ro
do such as : int, if, else ,etc.
the code below is a simple c++ program to identify keywords from a given input
string “int a = 5;”
#include <iostream>
#include <string>
using namespace std;
int keyword(string token) {
string keywords[3] = {"if","else","int"};
bool z = false;
for (int i=0; i < 3 ; i++) {
if(keywords[i] == token)
z = true;
}
return z;
}
int main() {
char c;
string x, code = "int a = 5;";
for (int i=0; i<[Link](); i++){
c = code[i];
if(isalpha(c)) {
x=x+c;
}
if ((!isalpha(c) || i == [Link]() - 1) && x != "") {
if (keyword(x)) {
cout<<x<<"\tkeyword\n"<<endl;
}
x="";
}
}
return 0; }
Compilers || 3rd stage Shahad Ali
A simple c++ program to identify Digits from a given string :
“int fm5 = 44; int num = 6;”
#include <iostream>
#include <string>
using namespace std;
int main() {
char c;
string x,m;
string Code= "int fm5 = 44; int num = 6;";
for (char c : Code) {
if(isdigit(c)) {
m=m+c;
}
else if (isalpha(c)) {
x=x+c;
}
else {
if(m!="" && x=="")
cout<<m<<" digit\n";
m=""; x="";
}
}
if (m!="") {
cout << m << " digit\n";
}
}
Note// for (char c : code) is a range based loop it is new a feature added to c++11 to make
sure that dev c++ support this feature go to : tools > compiler options > check Add the
following commands when calling compiler and then write -std=c++11.
H.W // write c++ program to identify keywords from string
"if (a<b) { cout<<\"Hi\"}"?