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
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);
}
}
Get Answers For Free
Most questions answered within 1 hours.