Question

C++ Question The first phase of compilation is called scanning or lexical analysis. This phase interprets...

C++ Question

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 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

Homework Answers

Answer #1

ASSUMPTION

Hash function will return an integer from 0 to 19

vector <string> hashTable[20];

int hashTableSize=20;

Implementation of hash tables with separete chaining ( open hashing)

FOR SEARCH

void search(string s)

{

//Compute the index by using the hash function

int index = hashFunc(s);

//Search the linked list at that specific index

for( int i =0; i< hashTable[index].size() ; i++)

{

if(hashTable[index] [i] == s)

{

cout<< s << " is found !"<<endl;

return;

}

}

cout<< s<<" is not found"<<endl;

}

INSERT

void insert(string s)

{

// compute the index using hash function

int index = hashFunc(s);

//Insert the element in the linked list at the particular index

hashTable[index].push_back(s);

}

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Description: In this assignment, you need to implement a recursive descent parser in C++ for the...
Description: In this assignment, you need to implement a recursive descent parser in C++ for the following CFG: 1. exps --> exp | exp NEWLINE exps 2. exp --> term {addop term} 3. addop --> + | - 4. term --> factor {mulop factor} 5. mulop --> * | / 6. factor --> ( exp ) | INT The 1st production defines exps as an individual expression, or a sequence expressions separated by NEWLINE token. The 2nd production describes an...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in the file contains seven numbers,which are the sales number for one week. The numbers are separated by comma.The following line is an example from the file 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36. The program should display the following: . The total sales for each week . The average daily sales for each week . The total sales for all of the weeks .The average weekly sales .The week number...
I did already posted this question before, I did get the answer but i am not...
I did already posted this question before, I did get the answer but i am not satisfied with the answer i did the code as a solution not the description as my solution, so i am reposting this question again. Please send me the code as my solution not the description In this project, build a simple Unix shell. The shell is the heart of the command-line interface, and thus is central to the Unix/C programming environment. Mastering use of...