Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Students Names: Roll No's: Class: Date: Subject: Assignment Topic

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

Students Names:

Roll No’s: 160597, 160661


Class: BS(CS) VII-B
Date: 02-Oct-19
Subject: Compiler Construction
Assignment: 1
Topic: Lexical Analysis & Tokens

Question No.1

< Lexeme, Token >

< For, keyword > < (, operator > < int, keyword > < x, identifier >
< =, operator> < 0, number> <;,separator> < x ,identifier >
< <, operator> < = , operator > < 5 , number > <;,separator>
< x, identifier > < + , operator > < + , operator > < ),
operator >
< ‘\n’,specifier>
< B, identifier > < = , operator > < (, operator > < (, operator >
< c, identifier > < +, operator > < a, identifier > < ),
operator >
< *, operator > < d, identifier > < /, operator > < f, identifier
>
< ‘\n’,specifier>
< while, keyword > < (, operator > < a ,identifier > < < , operator>
< 5, number > < ), operator >
< ‘\n’,specifier> < ‘\t’,specifier> < ‘\t’,specifier> <
‘\t’,specifier>
< a ,identifier > < = , operator >
< a ,identifier > < +, operator > < 1 , number > < char,
keyword >
< MyCourse, identifier > < [, operator > < 5 , number > < ], operator >
< ‘\n’,specifier>
<;, separator> < if, keyword > < (, operator > < a ,identifier >
< <, operator> < b, identifier > <), operator >
< ‘\n’,specifier> < ‘\t’,specifier> < ‘\t’,specifier> <
‘\t’,specifier>
< a, identifier >
< =, operator > < a, identifier > < *, operator > <a
,identifier >
<;,separator> < ‘\n’,specifier>
< else, keyword > < ‘\n’,specifier>
‘\t’,specifier> < ‘\t’,specifier> < ‘\t’,specifier>
< b ,identifier > < = , operator >
< b ,identifier > < *, operator > < b ,identifier >
<;,separator>

Question No.2
A
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>

using namespace std;


char *removeSpaces(char *str)
{

if (*str != 32){
return str;
}

int main(){
char data ;
ifstream infile;
infile.open("file.txt");
cout << "Removed spaces: " << endl;

while(!infile.eof()){
infile>>data;
cout<<removeSpaces(&data);
//cout<<data;
}
cin.ignore();
}
B

#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;
}

C,D,E
#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","floa
t","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)){
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;
}

You might also like