Please help me program this in C++ Objectives To program in the C/C++ or Java pr
ID: 3743061 • Letter: P
Question
Please help me program this in C++
Objectives To program in the C/C++ or Java programming language To understand the lexical analysis phase of program compilation Assignment: 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 Java, CIC++program 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. Optionally, your program can build a symbol table (a hash table is a good choice), 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 catagory appeared in the input. Sample token format: keyword ->if the else begin end identifier -> character character identifier integer-> digit | digit integer real -> integer.integer special ->I1+I--1,1 digit -> 0 1 2 3 4 5 6 7 8 9 character ->a bc .. zAB c... 2 More details . Case is not used to distinguish tokens. The delimiters can be space, tab, newline, and special characters Choice at least 5 token classes or use the sample ones aboveExplanation / Answer
The program is written in C++
Name of the program: lexical_analyzer.cpp
Program:
#include<iostream>
#include<fstream>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
using namespace std;
// function to check if the token is a keyword
int isKeyword(char stream[]){
// C++ reserved keywords
char keywords[32][10] = {"auto","break","case","char","const","continue","default",
"do","double","else","enum","extern","float","for","goto",
"if","int","long","register","return","short","signed",
"sizeof","static","struct","switch","typedef","union",
"unsigned","void","volatile","while"};
int pos_i, flag = 0;
for(pos_i = 0; pos_i < 32; ++i){
if(strcmp(keywords[pos_i], stream) == 0){
flag = 1;
break;
}
}
return flag; //returns true if the token is a keyword
}
int main(){
char ch, stream[50];
char operators[] = "+-/*%="; //standard operators; can modify if any other operators need to be included
fstream fin("test.txt"); // reads the input file
int pos_i,pos_j=0; // loop variables
if(!fin.is_open()){
cout<<"error while opening the file ";
exit(0);
}
while(!fin.eof()){
ch = fin.get();
// logic where token is checked if it is an operator
for(pos_i = 0; pos_i < 6; ++i){
if(ch == operators[pos_i])
cout<<ch<<" is operator ";
}
if(isalnum(ch)){
stream[pos_j++] = ch;
}
else if((ch == ' ' || ch == ' ') && (pos_j != 0)){
stream[pos_j] = '';
pos_j = 0;
if(isKeyword(stream) == 1)
cout<<stream<<" is keyword ";
else
cout<<stream<<" is indentifier ";
}
}
fin.close();
return 0;
}
/* End of the program */
Please use the below sample file for the input:
Name of the sample file: test.txt
File:
void main() {
String s = "Hello World!";
cout<<s;
}