In: Computer Science
PLEASE WRITE IN C++ only .
The objective of this assignment is to gain an understanding of the lexical analysis phase of a compiler and the process of constructing a symbol table.
Problem: The first phase of compilation is called scanning or lexical analysis. This phase interprets the input program as a sequence of characters and produces a sequence of tokens, which will be used by the parser.
Write a program (in C, C++, C#, Java, or Python) that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Your program should build a symbol table which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a list of all the tokens that appeared in the input, the number of times each token appears in the input and the class of each token. Your program should also list how many times tokens of each class appeared in the input.
The grammar for producing tokens is as follows:
keyword ::= if | then | else | begin | end
identifier -> character | character identifier
integer -> digit | digit integer
real -> integer.integer
special -> ( | ) | [ | ] | + | - | = | , | ;
digit -> 0|1|2|3|4|5|6|7|8|9
character -> a|b|c ... |z|A|B|C ... |Z
More details:
Case is not used to distinguish keywords or identifiers.
The delimiters are space, tab, newline, and the special characters.
The token classes that should be recognized are keyword, identifier, integer, real and special.
PLEASE WRITE IN C++ ONLY
#include <iostream>
#include <bits/stdc++.h>
#include<ctype.h>
#include <string>
using namespace std;
char* tokens[] = { "if", "(", "5.2","if","abc","500","34"};
string tokenClasss(string token) { // function to return each
token class
int isFlag = 0;
if(token == "if" || token == "then" || token == "else"
|| token == "begin" || token == "end") {
return "keyword";
}
else if(token == "(" || token == "|" || token == ")"
|| token == "[" || token == "]" || token == "+" || token ==
"-" || token == "=" || token == "," || token == ";")
{
return "special";
}
for(int i = 0;i < token.length();i++) {
if(((int)token[i]-48) >= 0 &&
((int)token[i]-48) < 10) {
isFlag = 1;
} else {
isFlag = 0;
break;
}
}
if(isFlag == 1) {
return "integer";
}
else if(token.length() >= 1) {
isFlag = 0;
for(int i = 0;i < token.length();i++) {
if ((int)token[i] >= 65
&& ((int)token[i] >= 65 && (int)token[i] <=
90|| ((int)token[i] >= 97 && (int)token[i] <= 122)))
{
isFlag = 1;
}else {
isFlag =
0;
break;
}
}
if(isFlag == 1) return "identifier";
else return "real";
}
}
int main(int argc,char** argv)
{
argc = 7; // number of token
argv = tokens; // token list
string strArray[argc]; // store each token
int countTokens[argc]; // count token occurences
string classTokens[argc]; // store each token
class
cout << "You have entered " << argc
<< " arguments:"<<
"\n";
int count;
for (int i = 0; i < argc; i++) {
strArray[i] = argv[i];
classTokens[i] =
tokenClasss(strArray[i]);
count = 0;
for (int j = 0; j < argc; j++)
{
if(argv[i] ==
argv[j]) ++count;
}
countTokens[i] = count;
}
cout<<"Tokens "<<" Number of
occureness"<<" Token class"<<endl;
for (int i = 0; i < argc; i++) {
cout <<strArray[i] << "
"<<countTokens[i]<<"
"<<classTokens[i]<<endl;
}
cout<<"Tokens Class "<<" Number of
occureness"<<endl;
for (int i = 0; i < argc; i++) {
count=0;
for (int j = 0; j < argc; j++)
{
if(classTokens[i] == classTokens[j]) ++count;
}
cout <<classTokens[i]
<< " "<<count<<endl;
}
return 0;
}