Professional Documents
Culture Documents
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);
{
s_cmt = true;
i++;
}
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;
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.