CS 382 Project 1 Lexcial Analyzer due on the class on 030317
CS 382 Project 1: Lexcial Analyzer (due on the class on 03/03/17) In this project, you will implement a simple lexcial analyzer to scan a small set of expressions. This set of expressions has the following following tokens: identifier, integer constant, left parenthesis, right parenthsis, addition operator, and multiplication operator. Specifically, assume we have the following lexical definition. LETTER -> a | b | ... | z | A | ... | Z DIGIT -> 0 | 1 | ... | 9 We define a set of tokens as follows. ID -> LETTER { LETTER | DIGIT } INT -> DIGIT { DIGIT } LEFT_PARENTHESIS -> ( RIGHT_PARENTHESIS -> ) ADD_OP -> ‘+’ | ‘-’ MUL_OP -> ‘+’ | ‘/’ Note that blank is a charactor that should be skipped by the program. Given an expression as input, your program should accomplish the following requirements: 1. If the expression has error, for example, undefined token, unequal numbers of left parenthesis and right parenthsis, the program should output an error message. 2. Print out all identifiers and all integer constants; 3. Counting how many left parenthesis, right parenthesis, addition, and multiplication operators. For example, for the input expression (number1 + number2) * (number3 + 1000) your program should output ID: number1, number2, number3 INT: 1000 The numbers of LEFT PARENTHESIS, RIGHT PARENTHESIS, ADD_OP and MUL_OP: 2, 2, 2, and 1
Solution
Solution :: This code is in \'C\' language.
// header files //
#include<stdio.h>
#include<stringing.h>
#include<stdlib.h>
void remove_duplicate();
void final();
int Isidentifier(char ch);
int Isoperator(char ch);
int Isdelimiter(char ch);
int Iskey(char * string);
void remove_duplicate();
// variable declarations //
char op[8]={\'+\',\'-\',\'*\',\'/\',\'=\',\'<\',\'>\',\'%\'};
char del[8]={\'}\',\'{\',\';\',\'(\',\')\',\'[\',\']\',\',\'};
char *key[]={\"int\",\"void\",\"main\",\"char\",\"float\"};
// char *operato[]={\"+\",\"-\",\"/\",\"*\",\"<\",\">\",\"=\",\"%\",\"<=\",\">=\",\"++\"}; //
// variable declaration //
int idi=0,idj=0,k,opi=0,opj=0,deli=0,uqdi=0,uqidi=0,uqoperatoreri=0,kdi=0,liti=0,ci=0;
int uqdelimitor[20],uqoperatori[20],uqidentifier[20],l=0,j;
char uqdel[20],uqiden[20][20],uqoperator[20][20],keyword[20][20];
char iden[20][20],oper[20][20],delemiter[20],litral[20][20],lit[20],constant[20][20];
// function for lexical analysis //
void lexanalysis(char *string)
{
int i=0;
while(string[i]!=\'\\0\')
{
if(Isidentifier(string[i])) // for the identifiers
{
while(Isidentifier(string[i]))
{
iden[idi][idj++]=string[i++];
}
iden[idi][idj]=\'\\0\';
idi++;idj=0;
}
else
if(string[i]==\'\"\') // for the literals
{
lit[l++]=string[i];
for(j=i+1;string[j]!=\'\"\';j++)
{
lit[l++]=string[j];
}
lit[l++]=string[j];lit[l]=\'\\0\';
stringcpy(litral[liti++],lit);
i=j+1;
}
else
if(Isoperator(string[i])) // for the operators
{
while(Isoperator(string[i]))
{
oper[opi][opj++]=string[i++];
}
oper[opi][opj]=\'\\0\';
opi++;opj=0;
}
else
if(Isdelimiter(string[i])) // for the delemitereters
{
while(Isdelimiter(string[i]))
{
delemiter[deli++]=string[i++];
}
}
else
{
i++;
}
}
remove_duplicate();
final();
}
int Isidentifier(char ch)
{
if(is_alpha(ch)||ch==\'_\'||is_digit(ch)||ch==\'.\')
return 1;
else
return 0;
}
int Isoperator(char ch)
{
int f=0,i;
for(i=0;i<8&&!f;i++)
{
if(ch==op[i])
f=1;
}
return f;
}
int Isdelimiter(char ch)
{
int f=0,i;
for(i=0;i<8&&!f;i++)
{
if(ch==del[i])
f=1;
}
return f;
}
int Iskey(char * string)
{
int i,f=0;
for(i=0;i<5;i++)
{
if(!stringcmp(key[i],string))
f=1;
}
return f;
}
void remove_duplicate()
{
int i,j;
for(i=0;i<20;i++)
{
uqdelimitor[i]=0;
uqoperatori[i]=0;
uqidentifier[i]=0;
}
for(i=1;i<deli+1;i++) // removing the duplicate delemitereters
{
if(uqdelimitor[i-1]==0)
{
uqdel[uqdi++]=delemiter[i-1];
for(j=i;j<deli;j++)
{
if(delemiter[i-1]==delemiter[j])
uqdelimitor[j]=1;
}
}
}
for(i=1;i<idi+1;i++) // removing the duplicate identifiers
{
if(uqidentifier[i-1]==0)
{
stringcpy(uqiden[uqidi++],iden[i-1]);
for(j=i;j<idi;j++)
{
if(!stringcmp(iden[i-1],iden[j]))
uqidentifier[j]=1;
}
}
}
for(i=1;i<opi+1;i++) // removing the duplicate operators
{
if(uqoperatori[i-1]==0)
{
stringcpy(uqoperator[uqoperatoreri++],oper[i-1]);
for(j=i;j<opi;j++)
{
if(!stringcmp(oper[i-1],oper[j]))
uqoperatori[j]=1;
}
}
}
}
void final()
{
int i=0;
idi=0;
for(i=0;i<uqidi;i++)
{
if(Iskey(uqiden[i])) // identifying the keywords
stringcpy(keyword[kdi++],uqiden[i]);
else
if(is_digit(uqiden[i][0])) // identifying the constants
stringcpy(constant[ci++],uqiden[i]);
else
stringcpy(iden[idi++],uqiden[i]);
}
// print the outputs //
printf(\"\ \\tdelemitereter are : \ \");
for(i=0;i<uqdi;i++)
printf(\"\\t%c\ \",uqdel[i]);
printf(\"\ \\tOperators are : \ \");
for(i=0;i<uqoperatoreri;i++)
{
printf(\"\\t\");
puts(uqoperator[i]);
}
printf(\"\ \\tIdentifiers are : \ \");
for(i=0;i<idi;i++)
{
printf(\"\\t\");
puts(iden[i]);
}
printf(\"\ \\t The keywords are : \ \");
for(i=0;i<kdi;i++)
{
printf(\"\\t\");
puts(keyword[i]);
}
printf(\"\ \\t The constants are :\ \");
for(i=0;i<ci;i++)
{
printf(\"\\t\");
puts(constant[i]);
}
printf(\"\ \\t The literals are :\ \");
for(i=0;i<liti;i++)
{
printf(\"\\t\");
puts(litral[i]);
}
}
void main()
{
char string[50];
printf(\"\ Enter any stringing : \");
scanf(\"%[^\ ]c\",string);
lexanalysis(string);
}
/// *** Thank You *** ///




