You are on page 1of 4

LAB-3: LEXICAL ANALYZER DESIGN -1 AND PREPROCESSOR

1. Remove comment lines from a program (a task of preprocessor):


// C program to remove comments from a program
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int main()
{
//Variable declaration and initialization
char * program = 0, * modified_program=0;
int file_length=0, i=0, j=0;
bool s_cmt = false; //single line comment
bool m_cmt = false; //multiple line comment

//File opening
FILE *fp = fopen("input.c","r");
if(!fp)
{
printf("Source can't be opened");
exit(-1);
}

//File length
fseek(fp, 0, SEEK_END);
file_length = ftell (fp);
fseek(fp, 0, SEEK_SET);

//File reading
program = (char *) malloc (file_length + 1);
modified_program = (char *) malloc (file_length + 1);
fread(program, 1, file_length, fp);
program[file_length]='\0';

//File closing
fclose (fp);

//Writing output before comment removal


printf("Before removing comment:\n");
for(i=0;i<file_length;i++)
{
printf("%c",program[i]);

//printf("file length: %d", file_length);


//Removing comment
for (int i=0; i<file_length; i++)
{
// Check for beginning of comments and set the approproate
flags
if (program[i] == '/' && program[i+1] == '/')
LAB-3: LEXICAL ANALYZER DESIGN -1 AND PREPROCESSOR

{
s_cmt = true;
i++;
}

else if (program[i] == '/' && program[i+1] == '*')


{
m_cmt = true;
i++;
}

// If single line comment flag is on, then check for end of


it
else if (s_cmt == true && program[i] == '\n')
{
s_cmt = false;
}
// If multiple line comment is on, then check for end of it
else if (m_cmt == true && program[i] == '*' && program[i+1]
== '/')
{
m_cmt = false;
i++;
}
// If this character is in a comment, ignore it
else if (s_cmt || m_cmt)
continue;

// If current character is a non-comment character


else
{
modified_program[j]= program[i];
j++;
}
}
modified_program[j] = '\0';

//Writing output after comment removal


printf("\n\n\n\nAfter removing comment:\n");
j=0;
while(modified_program[j]!='\0')
{
printf("%c",modified_program[j]);
j++;
}
return 0;
}
LAB-3: LEXICAL ANALYZER DESIGN -1 AND PREPROCESSOR

2. To identify tokens:
#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)){
LAB-3: LEXICAL ANALYZER DESIGN -1 AND PREPROCESSOR

   buffer[j] = '\0';
   j = 0;
     
   if(isKeyword(buffer) == 1)
   cout<<buffer<<" is keyword\n";
   else
   cout<<buffer<<" is indentifier\n";
   }
  
}

fin.close();

return 0;
}

Lab Task:

1. Read program from a file and detect all the tokens (e.g. valid or invalid identifier, keyword,
operator) from the file. In fact we have to correct the limitations of the above program.

H.W.

1. Read program from a file and detect all the delimiters, integer, real number from the file.

You might also like