Need help completing the C++ scanner program // A simple lexical analyzer for C/
ID: 3816712 • Letter: N
Question
Need help completing the C++ scanner program
// A simple lexical analyzer for C/C++-style variable declarations.
// The grammar for the declarations is as follows:
//
// <declaration> ::= <type> <var> ’;’ | <type> <var> ’=’ <number> ’;’
// <type> ::= int | float
// <var> ::= A | B | C | D | E
// <number> ::= <integer> | <float>
// <integer> ::= <integer> <digit> | <digit>
// <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
// <float> ::= <integer> ‘.’ <integer>
//
// The task of this exercise is to write a lexical analyzer (or
// scanner) for the tokens used in the above grammar. The following
// is the regular expression that defines the tokens:
//
// = | ; | int | float | A | B | C | D | E | [0-9]+ | [0-9]+.[0-9]+
//
// The program will read a declaration from the keyboard, and the
// scanner you design should recognize and print out all tokens
// included in the input. For example, given the following declaration:
//
// int A = 123;
//
// your program should print:
//
// int
// A
// =
// 123
// ;
//
// Make sure your program print out the token one per line in the order
// they appear in the input. Once an erroneous token is encountered, your
// scanner should print out an error message and stopped scanning. For
// example, given the following input:
//
// int A = 0#;
//
// your program should print:
//
// int
// A
// =
// 0
// #: Error: Unrecognizable token
//
// Note that tokens may NOT be separated by spaces. For example, the above
// input:
//
// int A = 123;
//
// does not have a space to separate 123 and ;. Also, the following inputs
// are also legal and generate the same output:
//
// int A=123;
// intA=123;
//
// However, a whole token cannot be separated by spaces. For example, the
// following input will cause 12 and 3 to be regarded as two distinct tokens.
//
// int A = 12 3;
//
// The ouput will look like:
// int
// A
// =
// 12
// 3
// ;
//
// Also note that the scanner doesn't check for syntactic errors. Therefore the
// above input is legal to this program.
//
// Important!!! Save your GetToken program. We are going to use it in future
// labs.
#include <iostream>
#include <string>
using namespace std;
string GetToken();
void error(int);
int main() {
string token;
cout << "Please enter a declaration in format "
<< "<type> <variable> [= number];" << endl;
cout << "The following are the tokens in the input:" << endl;
token = GetToken();
while (token != "") {
cout << token << endl;
token = GetToken();
}
cout << "Done!" << endl;
return 0;
}
string GetToken() {
string token;
char ch;
// Write the code here. Read the next token and store it in variable "token".
// The token must be read character by character. Use the regular expression
// defined above to extract tokens from the input.
//
// To read a character from keyboard, use:
//
// cin.get(ch);
//
// where "ch" is a character variable.
return token;
}
Explanation / Answer
# include <iostream.h>
# include <fstream.h>
# include <string.h>
# include <stdlib.h>
# include <stdio.h>
# include <conio.h>
# include <ctype.h>
constint iColumns = 12;
int iTT[iRows][iColumns] = {0};
{
fstream File("tt.txt", ios::in|ios::nocreate);
if (!File)
{
cout << " Unable to open the input file." << endl;
cout << " Press any key to exit.";
getch( );
exit(0);
}
char sInput[100]={NULL};
for (int i = 0; i < iRows; i ++)
{
strset(sInput, NULL);
File.getline(sInput, 80);
char *sPtr=NULL;
sPtr = strtok(sInput, " ");
iTT[i][0] = atoi(sPtr);
for(int j = 1; j < iColumns; j ++)
{
sPtr=strtok(NULL, " ");
iTT[i][j] = atoi(sPtr);
}
}
File.close( );
}
{
if (isalpha(cChar))
return iTT[iState][1];
elseif (isdigit(cChar))
return iTT[iState][2];
elseif (cChar == '.')
return iTT[iState][3];
elseif (cChar == '"')
return iTT[iState][4];
elseif (cChar == '_')
return iTT[iState][6];
elseif (cChar == '+')
return iTT[iState][7];
elseif (cChar == '=')
return iTT[iState][8];
elseif (cChar == '-')
return iTT[iState][9];
elseif (cChar == '%')
return iTT[iState][10];
elseif (cChar == '!')
return iTT[iState][11];
elseif (cChar == '>')
return iTT[iState][12];
elseif (cChar == '<')
return iTT[iState][13];
elseif (cChar == '/')
return iTT[iState][14];
return iTT[iState][0];
}
{
if (strlen(sToken) > 16 || strlen(sToken) == 0)
return 0;
char sKeywords[44][15] = {
"asm","auto","bool","break","case","catch",
"char","class","const","const_cast",
"continue","default","delete","do","double",
"dynamic_cast","else","enum","explicit",
"static_cast","struct","switch","template",
"this","throw","true","try","typedef",
"typeid","typename","union","unsigned","using",
"virtual","void","volatile","wchar_t","while"
};
for(int iCount = 0; iCount < 64; iCount ++)
{
if (strcmpi(sKeywords[iCount], sToken) == 0)
return 1;
}
return 0;
}
{
clrscr( );
loadTransitionTable( );
fstream File("input.txt", ios::in|ios::nocreate);
if (!File)
{
cout<<" Unable to open the input file."<<endl;
cout<<" Press any key to exit.";
getch( );
exit(0);
}
char sToken[255] = {NULL};
int iTokenIndex = 0;
char cChar = NULL;
int iState = 0;
int iFlag = 0;
char cTemp = File.get( );
do
{
Start:
if (iFlag == 0)
{
cChar = cTemp;
cTemp = File.get( );
}
else
iFlag = 0;
if (cChar == '/' && cTemp == '/')
{
while(File.get( ) != ' ')
{
if (File.eof( ))
goto End;
}
cout<<' ';
cTemp = File.get( );
goto Start;
}
if (cChar == '/' && cTemp == '*')
{
cTemp = File.get( );
do
{
cChar = cTemp;
cTemp = File.get( );
if (File.eof( ))
goto End;
}
while(cChar != '*' && cTemp != '/');
cout<<' ';
cTemp = File.get( );
goto Start;
}
iState = getNextState(iState, cChar);
switch (iState)
{
case 0 : cout << cChar;
iState = 0;
iTokenIndex = 0;
strset(sToken, NULL);
break;
case 1 :
case 3 :
case 5 :
case 7 :
case 10 :
case 14 :
case 18 :
case 25 :
case 26 : sToken[iTokenIndex] = cChar;
iTokenIndex ++;
break;
case 2 : if (isKeyword(sToken))
cout << sToken;
else
cout << "<ID>";
iState = 0;
iTokenIndex = 0;
iFlag = 1;
strset(sToken, NULL);
break;
case 4 : cout << "<INT>";
iState = 0;
iTokenIndex = 0;
iFlag = 1;
strset(sToken, NULL);
break;
case 6 : cout << "<FLOAT>";
iState = 0;
iTokenIndex = 0;
iFlag = 1;
strset(sToken, NULL);
break;
case 8 : cout << "<STR>";
iState = 0;
iTokenIndex = 0;
strset(sToken, NULL);
break;
case 9 :
case 24 :
case 27 :
case 28 : cout << "<OPR>";
if (cChar != '+' && cChar != '-' && cChar != '/'
&& cChar != '>' && cChar != '<' && cChar != '=')
iFlag = 1;
iState = 0;
iTokenIndex = 0;
strset(sToken, NULL);
break;
case 30 :
case 33 : iState = 0;
iTokenIndex = 0;
strset(sToken, NULL);
break;
}
}
while(!File.eof( ));
End:
getch( );
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.