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

Compilers Lab

Uploaded by

shahadali.123987
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Compilers Lab

Uploaded by

shahadali.123987
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

compilers || 3rd stage

lec 1
#define
Syntax for Defining Constant macro:
#define MACRO_NAME value

// C++ Program to calculate the area of the circle with a predefined


// macro
#include <iostream>
using namespace std;

#define PI 3.14

int main()
{
float r, area;
cout << "Enter the radius of a circle: ";
cin >> r;
area = PI * (r * r);
cout << "Area = " << area;
return 0;
}

Syntax for Defining Expression Macros:


#define MACRO_NAME(parameters) (expression)
// C++ Program to calculate the sum of two numbers using function
// macro
#include <iostream>
using namespace std;

#define SUM(x,y) ((x) + (y))

int main()
{
int x,y;
cout << "Enter the value of x and y: ";
cin >> x >> y;
int sum = SUM(x,y);
cout << "sum = " << sum;
return 0;
}
compilers || 3rd stage Shahad Ali

H.W // Write a C++ program that uses a macro SQUARE(x) to calculate the
square of a number and prints the result for x.

Compilation Steps Explained


1. Preprocessing:
● Command: g++ -E hello.cpp -o hello.i .
● This step processes directives like #include <iostream>, inserting the necessary
code.
● Output: The file hello.i contains the expanded code.
2. Compilation:
● Command: g++ -S hello.i -o hello.s.
● Converts preprocessed code to assembly language.
● Output: The file hello.s shows low-level assembly instructions.
3. Assembly:
● Command: g++ -c hello.s -o hello.o.
● Converts assembly code to machine-readable object code.
● Output: The file hello.o contains binary object code
4. Linking :
● Command: g++ hello.o -o hello.
● Links object code to create an executable.
● Output: The file hello is the executable program.
compilers || 3rd stage Shahad Ali

Compilers Lab - Lexical analysis part 1

write 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<code.length(); i++){


c = code[i];
if(isalpha(c)) {
x=x+c;
}
if ((!isalpha(c) || i == code.length() - 1) && x != "") {
if (keyword(x)) {
cout<<x<<"\tkeyword\n"<<endl;
}
x="";
}
}
return 0; }

H.W // write c++ program to identify keywords from string


"if (a<b) { cout<<\"Hi\"}"?
compilers || 3rd stage Shahad Ali

Compilers Lab - Lexical analysis part 2


write c++ program to analyze and identify identifiers. code = “if (a>b)”.

#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 = "if (a>b)";

for (int i = 0; i < code.length(); ++i) {

c = code[i];
if (isalpha(c) || (isdigit(c) && x!="")) {
x = x+c;
}
else {
if(x!="") {
if(!keyword(x)){
cout<<x<<"\tid\n";
}

x=""; }
}
}
if(x!="") {
if(!keyword(x))
cout<<x<<"\tid\n";
}
return 0; }

H.W // write c++ program to identify keywords and identifiers, code=”if


(a>b)”. expected output:
compilers || 3rd stage Shahad Ali

Compilers Lab - Lexical analysis part 3


Write a program that takes an expression 4 + 5 and identifies tokens as numbers
or operators.
#include <iostream>
#include <string>
using namespace std;

void tokenize(string expr) {


for (char c : expr) {
if (isdigit(c)) {
cout << "Number: " << c << endl;
}
else if (c == '+' || c == '-') {
cout <<"Operator: " << c << endl;
}
}
}

int main() {
tokenize("4 + 5");
return 0;
}

Write a program to detect parentheses in an expression 3 * (2 + 1).


#include <iostream>
#include <string>
using namespace std;

void find_P(string expr) {


for (char c : expr) {
if (c == '(' || c == ')') {
cout <<"parenthesis: " << c << endl; }
}
}

int main() {
find_P("3 * (2 + 1)");
return 0;
}

H.W // write program that takes an expression 3 * (2+1) and identifies


tokens as numbers ,operators or parenthesis.

You might also like