Question

In: Computer Science

Write a C++ program that implements a simple scanner for a source file given as a...

Write a C++ 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 category appeared in the input.

Sample token format

        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

Solutions

Expert Solution

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<stdio.h>
#include<string>
#include<map>
#include<vector>
#include<fstream>
#include<cstring>
#include<string.h>
#include<functional>

using namespace std;

map<string, int> classes = {{"keyword",0},{"identifier",0},{"digit",0},{"integer",0},{"real",0},{"character",0},{"alpha",0}};
vector<string> ints = {"0","1","2","3","4","5","6","7","8","9"};
vector<string> key_words = {"if","else","then","begin","end"};
vector<string> identi_fiers = {"(",")","[","]","+","=",",","-",":"};
vector<string> alpha_bets = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
vector<std::string>::iterator iters;

bool is_keyword(string ch);
bool is_identifier(char ch);
bool is_integer(char ch);
bool is_digit(string ch);
bool is_real(string ch);
bool is_char(string ch);
bool is_alpha(char ch);

int cnt_key = 0,cnt_ident =0,cnt_digit = 0,cnt_int = 0,cnt_alpha = 0,cnt_char = 0,cnt_real =0;

bool is_keyword(string ch)
{
   iters = find(key_words.begin(),key_words.end(),ch);
   if(iters != key_words.end()){
       classes["keyword"]++;
       return true;
   }  
   else{
       return false
   }
}


bool is_identifier(string ch)
{
   iters = find(identi_fiers.begin(),identi_fiers.end(),ch);
   if(iters != identi_fiers.end()){
       classes["identifier"]++;
       return true;
   }  
   else{
       return false
   }
}  

bool is_digit(string ch)
{
   iters = find(ints.begin(),ints.end(),ch);
   if(iters != ints.end()){
       classes["digit"]++;
       classes["integer"]++;
       return true;
   }  
   else{
       return false
   }
}  

bool is_integer(string ch)
{
   char *p;
   strtol(ch.c_str(), &p , 10);
   if(*p == 0){
       classes["digit"]++;
       return true;
   }  
   else{
       return false
   }
}  

bool is_real(string ch)
{
   bool isreal=false;
   if(ch.find(".") != std::string::npos){
       for(int j=0;j<ch.length;j++){
           if(ch[j] != '.'){
               string search;
               search = ch[j];
               iters = find(ints.begin(),ints.end(),search);
               if(iters != ints.end()){
                   isreal = true;
               }
               else{
                   isreal = false;
               }
           }
   else{;}
       }
   }  
   else {;}
       if(isreal == true){
           classes["real"]++;
       return true;}
       else {return false;}
}

bool is_alpha(string ch)
{
   iters = find(alpha.begin(),alpha.end(),ch);
   if(iters != alpha.end()){
       classes["alpha"]++;
       classes["char"]++;
       return true;
   }  
   else{
       return false
   }
}  


bool is_char(string ch)
{
   bool ischar = false;
   for(int i=0;i<ch.length();i++){
       if(isalpha(ch[i])
           ischar = true;
       else
           ischar = false;
   }
   if(ch.length() >1 && ischar ==true)
   {
       classes["char"]++;
   return true;}      
   else{
       return false
   }
}  

int main(int argc, char *argv[])
{
   int token_cnt = 0; //used to count tokens as they are read.
  
   //at least one command line argument must be supplied
   if(argc < 2)
   {
       cerr <<"Error: filename argument not given" <<endl;
       exit(1);
   }
  
   ifstream in_File(argv[1], ios::in) //open file for input.
  
   //check for errors in opening the file
   if(!in_File){
       cerr <<"File" <<argv[1] << "couldn't not be opened" <<endl;
       exit(1);
   }
  
  
   char lines[100]; //char array used to hold lines of text input
   char *ps; //pointer used to tokenize a string
  
   while(in_File.getline(lines,100)){ //reads upto 100 chars into lies array
       ps = strtok(lines, " ~'!@#$^&*_{}:<>|?"); //assigns token pointer to first token in line
       while(ps != NULL){
           if(is_keyword(ch)){
               cnt_key++;
           }
           else if (isIdentifier(ps)) {
               cnt_ident++; }
           else if (is_char(ps)) {
               cnt_char++; }
           else if (is_digit(ps)) {
               cnt_digit++; }
           else if (is_integer(ps)) {
               cnt_int++; }
           else if (is_real(ps)) {
               cn++; }
           else if (is_alpha(ps)) {
               cnt_alpha++; }
           else {;}
           ps = strtok(lines, " ~'!@#$^&*_{}:<>|?"); //assigns tokenpointer to first token in line
       }
   }
  
std::cout << "CLASS" <<"keyword" <<":" <<classes["keyword"] <<"\n";
std::cout << "CLASS" <<"identifier" <<":" <<classes["identifier"] <<"\n";
std::cout << "CLASS" <<"digit" <<":" <<classes["digit"] <<"\n";
std::cout << "CLASS" <<"integer" <<":" <<classes["integer"] <<"\n";
std::cout << "CLASS" <<"character" <<":" <<classes["char"] <<"\n";
std::cout << "CLASS" <<"alphabet" <<":" <<classes["alpha"] <<"\n";
std::cout << "CLASS" <<"real" <<":" <<classes["real"] <<"\n";
  

in_File.close();
return 0;
}  
  

  
  


Related Solutions

Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ 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...
Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ 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...
Write this program in C++ You are given a source file in your work area for...
Write this program in C++ You are given a source file in your work area for this assignment. It consists of a declaration for a Val node. There are declarations for three overloaded operator functions, which you must fill in: operator+, operator*, and operator!. operator+ should implement addition. operator* should implement multiplication. operator! should reverse the digits of its operand (i.e., of "this") and return the reversed integer. So for example !123 should return 321. It should be straightforward to...
write a program in c++ that opens a file, that will be given to you and...
write a program in c++ that opens a file, that will be given to you and you will read each record. Each record is for an employee and contains First name, Last Name hours worked and hourly wage. Example; John Smith 40.3 13.78 the 40.3 is the hours worked. the 13.78 is the hourly rate. Details: the name of the file is EmployeeNameTime.txt Calculate the gross pay. If over 40 hours in the week then give them time and a...
Write an x86 assembly language program that performs equivalently to the C++ source code file shown...
Write an x86 assembly language program that performs equivalently to the C++ source code file shown below.Please note that commented out behavior must be implemented in x86 assembly language. There is no standard, portable way to perform some of these actions in C++. #include void main() { // Use registers for these in your x86 assembly language program // Only use the .data segment for string (character array) variables int eax; int esi; int ecx; int edi; // Loop the...
C++ The program implements the Number Guessing Game. However, in that program, the user is given...
C++ The program implements the Number Guessing Game. However, in that program, the user is given as many tries as needed to guess the correct number. Rewrite the program so that the user has no more than five tries to guess the correct number. Your program should print an appropriate message, such as “You win!” or “You lose!”.
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++ Rules: -Use fork(), exec(), wait(), and exit() _______________________________________________________________________________________________________________________________________________ -A line of input represents a token group. -Each token group will result in the shell forking a new process and then executing the process. e.g. cat –n myfile.txt // a token group -Every token group must begin with a word that is called the command(see example above). The words immediately following a command are calledarguments(e.g....
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++ Rules: -Use fork(), exec(), wait(), and exit() _______________________________________________________________________________________________________________________________________________ -A line of input represents a token group. -Each token group will result in the shell forking a new process and then executing the process. e.g. cat –n myfile.txt // a token group -Every token group must begin with a word that is called the command(see example above). The words immediately following a command are calledarguments(e.g....
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Write a program in Java with a Scanner. Given an array and a number k where...
Write a program in Java with a Scanner. Given an array and a number k where k is smaller than the size of the array, write a program to find the k'th smallest element in the given array. It is given that all array elements are distinct. Example: Input: arr[] = {7,10,4,3,20,15} k = 3 Output: 7
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT