Question

In: Computer Science

PLEASE WRITE IN C++ only . The objective of this assignment is to gain an understanding...

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

Solutions

Expert Solution

#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;
}


Related Solutions

The objective of this assignment is to gain an understanding of the lexical analysis phase of...
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...
Programming Assignment 1 Performance Assessment Objective: To write a C program (not C++) that calculates the...
Programming Assignment 1 Performance Assessment Objective: To write a C program (not C++) that calculates the average CPI, total processing time (T), and MIPS of a sequence of instructions, given the number of instruction classes, the CPI and total count of each instruction type, and the clock rate (frequency) of the machine. The following is what the program would look like if it were run interactively. Your program will read values without using prompts. Inputs: • Clock rate of machine...
Programming Assignment 1 Performance Assessment Objective: To write a C program (not C++) that calculates the...
Programming Assignment 1 Performance Assessment Objective: To write a C program (not C++) that calculates the average CPI, total processing time (T), and MIPS of a sequence of instructions, given the number of instruction classes, the CPI and total count of each instruction type, and the clock rate (frequency) of the machine. The following is what the program would look like if it were run interactively. Your program will read values without using prompts. Inputs: • Clock rate of machine...
The purpose of this assignment is to gain a better understanding of the code sets used...
The purpose of this assignment is to gain a better understanding of the code sets used for medical billing. These sets can be complicated, but you will learn in the EHR#8 assignment this week, that the EHR practice management functions for billing can use the clinical information to help you with code selection. Coding Classification Sets for Medical Coding and Billing Code Set When is this Code Set used? Format Example Source CPT Category I CPT Category I codes are...
Please complete in only C++, using loops Assignment: For this assignment you’ll be designing a program...
Please complete in only C++, using loops Assignment: For this assignment you’ll be designing a program which can take the input of a decimal number and a numerical base, and convert the decimal number to that base. For example, if given the decimal number seven and the base two, your program should output it as 111, which is how seven is represented in binary. Another example, 8,943 in base 10, is 13,236 in base 9. You’ll need to perform these...
This assignment is to give you practice using struts, arrays, and sorting. (Objective C++ and please...
This assignment is to give you practice using struts, arrays, and sorting. (Objective C++ and please have a screenshot of output) In competitive diving, each diver makes dives of varying degrees of difficulty. Nine judges score each dive from 0 through 10 in steps of 0.5. The difficulty is a floating-point value between 1.0 and 3.0 that represents how complex the dive is to perform. The total score is obtained by discarding the lowest and highest of the judges’ scores,...
Objective: In this assignment you will apply your knowledge and understanding of the AD/AS model to...
Objective: In this assignment you will apply your knowledge and understanding of the AD/AS model to a hypothetical scenario. Instruction: Write a response to the following scenario on a word file or equivalent and submit it as an 'upload file'. Please note you can only submit the following file types, doc, docx, pdf, txt, png, and jpeg. Graphs can be hand-drawn or computer graphics. Remember to correctly label all your graphs! For example, what the x and y axis represent....
Please code in C# - (C - Sharp) Assignment Description Write out a program that will...
Please code in C# - (C - Sharp) Assignment Description Write out a program that will ask the user for their name; the length and width of a rectangle; and the length of a square. The program will then output the input name; the area and perimeter of a rectangle with the dimensions they input; and the area and perimeter of a square with the length they input. Tasks The program needs to contain the following A comment header containing...
Objective: In this assignment, you will apply your knowledge and understanding of poverty and income inequality...
Objective: In this assignment, you will apply your knowledge and understanding of poverty and income inequality to real world issues. I nstruction: Watch and read the following materials before you write your response: The Costs of Inequality - Joseph Stiglitz (Links to an external site.) Three main philosophies of redistribution of income: Utilitarianism: the government should chose policies that maximize the total utility of everyone in society. For example, giving $100 to a homeless person would have a bigger impact...
REVERSE POLISH CALCULATOR C++ ONLY. For this assignment, you are to write a program, which will...
REVERSE POLISH CALCULATOR C++ ONLY. For this assignment, you are to write a program, which will calculate the results of Reverse Polish expressions that are provided by the user. You must use a linked list to maintain the stack for this program (NO array implementations of the stack). You must handle the following situations (errors): Too many operators (+ - / *) Too many operands (doubles) Division by zero The program will take in a Polish expression that separates the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT