The program for Assignment 2 implements a lexical analyzer. The calling sequence
ID: 3863987 • Letter: T
Question
The program for Assignment 2 implements a lexical analyzer. The calling sequence for the lexical analyzer, a definition for all of the tokens, and a class definition for Token, is included in the startercode for the assignment. The assignment is to write the lexical analyzer function and some test code around it. The test code takes several command line arguments: v (optional) if present, every token is printed when it is seen -stats (optional) if present, statistics are printed -sum (optional) if present, summary information is printed filename (optional) if present, read from the filename, otherwise read from standard in Note that no other flags (arguments that begin with a dash) are permitted. If an unrecognized flag is present, the program should print "Invalid argument (arg) where (arg) is whatever flag was given, and it should stop running. At most one filename can be provided, and it must be the last command line argument. If more than one filename is provided, the program should print "Too many file names" and it should stop running. If the program cannot open a filename that is given, the program should print "Could not open {arg)", where targ) is the filename given, and it should stop running. The program should repeatedly call the lexical analyzer function until it returns DONE or ERR. If it returns DONE, the program proceeds to handling the -stats and -sum options, if any, and then exits. it returns ERR, the program should print "Error on line N (lexeme)", where Nis the line number in the token and lexeme is the lexeme from the token, and it should stop running. If the -v option is present, the program should print each token as it is read and recognized, one token per line. The output format for the token is the token name in all capital letters (for example, the token LPAREN should be printed out as the string LPAREN. In the case of token ID, ICONST, FCONST and STRING, the token name should be followed by a space and the lexeme in parens. For example, if the identifier "hello" is recognized, the-v output for it would be ID (hello) If the -stats option is present, the program should, after seeing the DONE token, print out the following report: Total I N List of IDs: X Where N is the number of times the ID token was seen and X is a comma separated list of identifier lexemes. If N is 0, then List of IDs is not printedExplanation / Answer
Code for the above thing look like
#include<iostrngeam>
#include<conio.h>
#include<strnging>
#include<fstrngeam>
using namespace std;
/* Create input.txt file to execute the program and specify the path of file
*/
bool CheckKey(string strng)
{
strnging key[] ={"int","main()","float","if"};
int flg=0;
if(!(strng[0]>=97 && strng[0]<=122))
return false;
for(int i=0;i<4;i++)
{
if(strng == key[i])
{
flg = 1;
break;
}
}
if(flg == 1)
return true;
else
return false;
}
strnging operationcheck(string strng)
{
strnging relopert[8] ={"=","==",">","<",">=","<="};
strnging mathopert[5] ={"+","*","/","-","%"};
strnging Operts[4]={"{","}",";",","};
int flg1=0,flg2=0,flg=0;
for(int i=0;i<4;i++)
{
if(strng == Operts[i])
{
flg = 1;
break;
}
}
if(flg == 1)
{
return "Opert";
}
else
{
for(int i=0;i<8;i++)
{
if(strng == relopert[i])
{
flg1 = 1;
break;
}
}
if(flg1 == 1)
{
return "relational Opert";
}
else
{
for(int i=0;i<5;i++)
{
if(strng == mathopert[i])
{
flg2 =1;
break;
}
}
if(flg2 == 1)
return "mathematical Opert";
else
return "error";
}
}
}
int char(char c)
{
if((c>=65 && c<=90) || (c>=97 && c<=122))
return 1;
else
return 0;
}
int num(char c)
{
if(c>=48 && c<=57)
return 1;
else
return 0;
}
int nums(string strng)
{
int flg=0;
for(int i = 0;i<strng.length();i++)
{
if(!num(strng[i]))
{
if(strng[i] != 46)
{
flg=1;
break;
}
}
}
if(flg == 1)
return 0;
else
return 1;
}
int identifier(string strng)
{
int flg =0;
for(int i=1;i<strng.length();i++)
{
if(!char(strng[i]))
{
if(!num(strng[i]))
{
if(strng[i] != 95)
{
if(strng[i] == 91)
{
i++;
for(;strng[i]!= 93;)
{
if(!num(strng[i]))
{
flg =1;
break;
}
i++;
}
}
else
{
flg = 1;
}
if(flg ==1)
break;
}
}
}
}
return flg;
}
int main()
{
inputst ifs("input.txt");
outputst ofs("output.txt");
int line=0,flg=0;
bool check;
string strng="",strngch,strngline;
while(!ifs.eof())
{
getline(ifs,strngline);
line++;
ofs<<"--------- ";
ofs<<line<<" ";
strngline = strngline + " ";
for(int j=0;j<strngline.length();j++)
{
if(strngline[j] ==' ' || strngline[j]==' ')
{
if(strng != "")
{
if(char(strng[0]))
{
check = CheckKey(strng);
if(check)
{
ofs<<strng<<" --> reserved word ";
}
else
{
flg = identifier(strng);
if(flg == 1)
{
ofs<<strng<<" --> error ";
flg = 0;
}
else
{
ofs<<strng<<" --> identifier ";
}
}
}
else
{
if(num(strng[0]))
{
if(nums(strng))
ofs<<strng<<" -->number ";
else
ofs<<strng<<" -->error ";
}
else
{
strngch = operationcheck(strng);
if(strngch == "error")
ofs<<strng<<" -->"<<strngch<<" ";
else
ofs<<strng<<" -->"<<strngch<<" ";
}
}
}
strng="";
}
else
{
strng=strng+strngline[j];
}
}
}
cout<<"file generated : output.txt";
getch();
return 0;
}
Please create one input file and the location of file which you wish to analyse.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.