Assignment-1 CC 03-134192-025 Muhammad Hamza

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Bahria University Lahore Campus

Department of Computer Science


BS(CS) Program, Semester 05 Fall 2021
Course: Compiler Construction (CSC-323)
INSTRUCTOR: Nadeem Sarwar
Student name: ID: Section: A/B

Date: 18/10/2021 Assignment # 01 Marks: 5


Q1. Identify the <token ,lexeme> pairs

1. For ( int x= 0; x<=5; x++)

2. B= (( c + a) * d ) / f

3. While ( a < 5 )
a= a+1

4. Char MyCourse[5];

5. if ( a< b)
a=a*a;
else
b=b*b;
Q2. Write a program in C++ or Java that reads a source file and performs the followings operations:

1. Removal of white space


#include <iostream>
using namespace std;
void removeSpaces(char *str)
{
int count = 0;
for (int i = 0; str[i]; i++)
if (str[i] != ' ')
str[count++] = str[i];
str[count] = '\0';
}
int main()
{
char str[] = "i will re move space ";
removeSpaces(str);
cout << str;
return 0;
}
2. Removal of comments
#include<iostream>
#include<fstream>
using namespace std;
void remove_multi_comment(char source[], char dest[])
{
ifstream fin(source);
ofstream fout(dest);
char ch;
while(!fin.eof())
{
fin.get(ch);
if(ch=='/')
{
fin.get(ch);
if(ch=='*')
while(!fin.eof())
{
fin.get(ch);
if(ch=='/')
break;
}
}
else
fout<<ch;
}
fin.close();
fout.close();
}
int main()
{
char ch;
remove_multi_comment("remove_multi_line_comment.cpp","output.cpp"); /*
function call */
ifstream fin("output.cpp");
while(fin.get(ch))
cout<<ch;
fin.close();
return 0;
}

3. Recognizes constants
4. Recognizes Keywords
5. Recognizes identifiers
#include<iostream>
#include<fstream>
#include<stdlib.h> // random number generate
#include<string.h>
#include<ctype.h> // handle of character.
using namespace std;
int isKeyword(char buffer[]){
char keywords[32][10] = {"auto","break","case","char","const","continue","default",
"do","double","else","enum","extern","float
","for","goto",
"if","int","long","register","return","short","
signed",
"sizeof","static","struct","switch","typedef",
"union",
"unsigned","void","volatile","while"};
int i, flag = 0;
for(i = 0; i < 32; ++i){
if(strcmp(keywords[i], buffer) == 0){
flag = 1;
break;
}
}
return flag;
}
int main()
{
char ch, buffer[15], operators[] = "+-*/%=";
ifstream fin("program.txt");
int i,j=0;
if(!fin.is_open()){
cout<<"error while opening the file\n";
exit(0);
}
while(!fin.eof()){
ch = fin.get();

for(i = 0; i < 6; ++i){


if(ch == operators[i])
cout<<ch<<" is constant\n";
}

if(isalnum(ch)){ // check char alphabet or not


buffer[j++] = ch;
}
else if((ch == ' ' || ch == '\n') && (j != 0)){
buffer[j] = '\0';
j = 0;

if(isKeyword(buffer) == 1)
cout<<buffer<<" is keyword\n";
else

}
fin.close();
return 0;
}
6. Recognize operators
#include<iostream>
#include<stdbool.h>
using namespace std;
bool isOperator(char ch)
{
if (ch == '+' || ch == '-' || ch == '*' ||
ch == '/' || ch == '>' || ch == '<' ||
ch == '=')
return (true);
return (false);
}

7. Recognize numbers
#include<iostream>
#include<stdbool.h>
using namespace std;
bool isRealNumber(char* str)
{
int i,strlen, len = strlen(str);
bool hasDecimal = false;

if (len == 0)
return (false);
for (i = 0; i < len; i++) {
if (str[i] != '0' && str[i] != '1' && str[i] != '2'
&& str[i] != '3' && str[i] != '4' && str[i] != '5'
&& str[i] != '6' && str[i] != '7' && str[i] != '8'
&& str[i] != '9' && str[i] != '.' ||
(str[i] == '-' && i > 0))
return (false);
if (str[i] == '.')
hasDecimal = true;
}
return (hasDecimal);
}

Deadline:
Tuesday, 24-Oct-2021 before 09:00 PM (Via LMS)

You might also like