A simple lexical analyzer for CCstyle variable declarations

 // 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> //  // 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 the 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. //  #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; } 

Solution

#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>
using namespace std;

/* The grammar follwed by this analyser is : you have to give a blank to differentite between two different entities, for eg
instead of writing \"int c=a+b;\", write it as \"int c = a + b ;\".

To execute this program first create an \"input.txt\" file where the program is written or specify the path of the file which has
to be analysed, after compiling an \"output.txt\" file will be created where you have stored the program.
*/

bool keycheck(string str)
{
string keyword[] ={\"int\",\"main()\",\"float\",\"if\"};
int flag=0;
if(!(str[0]>=97 && str[0]<=122))
return false;
for(int i=0;i<4;i++)
{
if(str == keyword[i])
{   
flag = 1;
break;
}
}


if(flag == 1)
return true;
else
return false;
}

string opcheck(string str)
{
string reloperators[8] ={\"=\",\"==\",\">\",\"<\",\">=\",\"<=\"};
string mathoperators[5] ={\"+\",\"*\",\"/\",\"-\",\"%\"};
string operators[4]={\"{\",\"}\",\";\",\",\"};
int flag1=0,flag2=0,flag=0;

  
for(int i=0;i<4;i++)
{
if(str == operators[i])
{
flag = 1;
break;
}
}
if(flag == 1)
{
return \"operator\";
}
else
{
  
for(int i=0;i<8;i++)
{   
if(str == reloperators[i])
{
flag1 = 1;
break;
}
}
if(flag1 == 1)
{
return \"relational operator\";
}
else
{
for(int i=0;i<5;i++)
{   
if(str == mathoperators[i])
{
flag2 =1;
break;
}
}
  
if(flag2 == 1)
return \"mathematical operator\";
else
return \"error\";
}
}
}



int ischar(char c)
{
if((c>=65 && c<=90) || (c>=97 && c<=122))
return 1;
else
return 0;
}

int isnum(char c)
{
if(c>=48 && c<=57)
return 1;
else
return 0;
}
  
int isnums(string str)
{
int flag=0;
for(int i = 0;i<str.length();i++)
{
if(!isnum(str[i]))
{
if(str[i] != 46)
{
flag=1;
break;
}
}
}
  
if(flag == 1)
return 0;
else
return 1;
}

int isidentifier(string str)
{
int flag =0;
for(int i=1;i<str.length();i++)
{
if(!ischar(str[i]))
{
if(!isnum(str[i]))
{
if(str[i] != 95)
{
if(str[i] == 91)
{
i++;
for(;str[i]!= 93;)
{
if(!isnum(str[i]))
{
flag =1;
break;
}
i++;
}
}
else
{   
flag = 1;

}

if(flag ==1)
break;
}
}
}
}
  
return flag;
}



  
int main()
{
ifstream ifs(\"input.txt\");
ofstream ofs(\"output.txt\");
int line=0,flag=0;
bool check;
string str=\"\",strch,strline;



  
while(!ifs.eof())
{
  
getline(ifs,strline);
line++;
ofs<<\"---------\ \";
ofs<<line<<\"\ \";

strline = strline + \" \";
for(int j=0;j<strline.length();j++)
{   
if(strline[j] ==\' \' || strline[j]==\'\\t\')
{
  
  
  
  
if(str != \"\")
{
if(ischar(str[0]))
{
check = keycheck(str);
  
if(check)
{
ofs<<str<<\"\\t --> reserved word\ \";
}
else
{
  
flag = isidentifier(str);
  
if(flag == 1)
{
ofs<<str<<\"\\t --> error\ \";
flag = 0;
  
}
else
{
ofs<<str<<\"\\t --> identifier\ \";
}
}

  

}
else
{
if(isnum(str[0]))
{
if(isnums(str))
ofs<<str<<\"\\t -->number\ \";
else
ofs<<str<<\"\\t -->error\ \";
}
else
{
strch = opcheck(str);
if(strch == \"error\")
ofs<<str<<\"\\t -->\"<<strch<<\"\ \";
else
ofs<<str<<\"\\t -->\"<<strch<<\"\ \";
}
}
  
}
  
str=\"\";
}
else
{
str=str+strline[j];
}
}
  
  
  
  
}
  
  
cout<<\"output file is generated : output.txt\";
  
  
getch();
return 0;
}
  
change code as simlar

 // A simple lexical analyzer for C/C++-style variable declarations. // The grammar for the declarations is as follows: // // <declaration> ::= <type&g
 // A simple lexical analyzer for C/C++-style variable declarations. // The grammar for the declarations is as follows: // // <declaration> ::= <type&g
 // A simple lexical analyzer for C/C++-style variable declarations. // The grammar for the declarations is as follows: // // <declaration> ::= <type&g
 // A simple lexical analyzer for C/C++-style variable declarations. // The grammar for the declarations is as follows: // // <declaration> ::= <type&g
 // A simple lexical analyzer for C/C++-style variable declarations. // The grammar for the declarations is as follows: // // <declaration> ::= <type&g

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site