You are on page 1of 9

Bahria University Lahore Campus

Department of Computer Science


BS(CS) Program, Semester 05 Spring 2022
Course: Compiler Construction (CSC-323)
INSTRUCTOR: Nadeem Sarwar
Student name: ID: Section: A

Date: 14/03/2022 Assignment # 01 Marks: 5

Name: Abdullah Arshad

Enrollment:03-134201-030
Q1. Identify the <token ,lexeme> pairs

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

Tokens Lexeme

For keyword

( symbols

int keyword

x identifier

= operators

0 constants

; symbols

x identifier

< operators

= operators

5 constants

; symbols

x identifier

+ operators
+ operators

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

Tokens Lexeme
B Identifier
= operators
( symbol
( symbol
c identifier
+ operators
a identifier
) symbol
* operators

d Identifier
) symbol
/ symbol
f identifier
3. While ( a < 5 )
a= a+1

Tokens Lexeme
While keyword
( symbol
a identifier
< operators
5 constant
) symbol

a identifier
= operators
a identifier
+ operator
1 constant

4. Char MyCourse[5];

Tokens Lexeme
Char keyword
MyCourse identifier
[ symbol
5 constants
] symbol
; symbol
5. if ( a< b)
a=a*a;
else
b=b*b;

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

1. Removal of white space


2. Removal of comments
3. Recognizes constants
4. Recognizes Keywords
5. Recognizes identifiers
6. Recognize operators
7. Recognize numbers
8.
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

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 operator\n";
}

if (isalnum(ch)) {
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
cout << buffer << " is indentifier\n";
}

}
fin.close();
return 0;

}#include<iostream>
#include<string>
using namespace std;
int main()
{
char a;
//if the portion is symbol
bool isSymobol(char a)
{
if (a == ' ' || a == '+' || a == '-' || a == '*'
|| a == ' /' || a == '>' || a == '<' || a == '['
|| a == ']' || a == '{' || a == '}' || a == ','
|| a == ';' || a == '(' || a == ')')
return (true);
return (false);
}
bool isoperator(char a)
{
if ( a == '+' || a == '-' || a == '*'
|| a == ' /' || a == '>' || a == '<' || a == '%')
return (true);
return (false);
}
bool Identifier(char *str)
{
if (str[0] == '0' || str[0] == '1' || str[0] == '2'
|| str[0] == '3' || str[0] == '4' || str[0] == '5' || str[0] ==
'6'|| str[0] == '7'||str[0] == '8'||str[0] == '9'|| isSymobol(str[0]==true))
return (false);
return (true);

}
bool isKeyword(char* str)
{
if (!strcmp(str, "if") || !strcmp(str, "else") ||
!strcmp(str, "while") || !strcmp(str, "do") ||
!strcmp(str, "break") ||
!strcmp(str, "continue") || !strcmp(str, "int")
|| !strcmp(str, "double") || !strcmp(str, "float")
|| !strcmp(str, "return") || !strcmp(str, "char")
|| !strcmp(str, "case") || !strcmp(str, "char")
|| !strcmp(str, "sizeof") || !strcmp(str, "long")
|| !strcmp(str, "short") || !strcmp(str, "typedef")
|| !strcmp(str, "switch") || !strcmp(str, "unsigned")
|| !strcmp(str, "void") || !strcmp(str, "static")
|| !strcmp(str, "struct") || !strcmp(str, "goto"))
return (true);
return (false);
}
bool isRealNumber(char* str)
{
int i, 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);
}
void parse(char* str)
{
int left = 0, right = 0;
int len = strlen(str);

while (right <= len && left <= right) {


if (isDelimiter(str[right]) == false)
right++;

if (isDelimiter(str[right]) == true && left == right) {


if (isOperator(str[right]) == true)
printf("'%c' IS AN OPERATOR\n", str[right]);
right++;
left = right;
}
else if (isDelimiter(str[right]) == true && left != right
|| (right == len && left != right)) {
char* subStr = subString(str, left, right - 1);

if (isKeyword(subStr) == true)
printf("'%s' IS A KEYWORD\n", subStr);

else if (isInteger(subStr) == true)


printf("'%s' IS AN INTEGER\n", subStr);

else if (isRealNumber(subStr) == true)


printf("'%s' IS A REAL NUMBER\n", subStr);

else if (validIdentifier(subStr) == true


&& isDelimiter(str[right - 1]) == false)
cout<<"'%s' IS A VALID IDENTIFIER\n", subStr);
&& isDelimiter(str[right - 1]) == false)
printf("'%s' IS NOT A VALID IDENTIFIER\n", subStr);
left = right;
}
}
return;
}

else if (validIdentifier(subStr) == false

char a[100];
cout << "Lines of code";
cin >> a;
return (0);
}

Deadline:
Sunday, 20-March-2022 before 11:45 PM (Via LMS)

You might also like