The first phase of the compilation is called scanning or lexical analysis. This
ID: 3753886 • Letter: T
Question
The first phase of the 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, C/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. 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
- A 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 above.
Explanation / Answer
#include<iostream>
#include<string>
#include<ctype.h>
using namespace std;
//scan function definition
void scan(string code[], int len)
{
//Loop variable
int c, d;
//To store a word
string wo;
//Flag status for a word
int f = 0;
//Loops till end of the code entered by the user
for(c = 0; c < len; c++)
{
//Extract a word
wo = code[c];
//Checks for the keyword
if(wo == "if" | wo == "then" | wo == "else" | wo == "begin" | wo == "end" | wo == "function" | wo == "return" )
{
cout<<wo<<": is a keyword"<<endl;
}
else
{
//Loops till end of the word
for(d = 0; d < wo.length(); d++)
{
//Checks for the parenthesis
if(wo[d] == '(' || wo[d] == ')')
{
f = 0;
break;
}
//Checks for the fractional number if true set the flag to 3
else if(isdigit(wo[d]) && wo[d+1] == '.')
{
f = 1;
break;
}
//Checks for the digit if true set the flag to 2
else if(isdigit(wo[d]))
{
f = 2;
break;
}
//Checks for the identifiers if true set the flag to 1
else if(isalnum(wo[d]))
{
f = 3;
break;
}
//Checks for the special character if true set the flag to 4
else if(wo[d] == '+' || wo[d] == '+' || wo[d] == '-' || wo[d] == '=' || wo[d] == '<' || wo[d] == '>')
{
f = 4;
break;
}
}//End of inner loop
if(f == 1)
cout<<wo<<": is a Real number"<<endl;
else if(f == 2)
cout<<wo<<": is a Integer digit"<<endl;
else if(f == 3)
cout<<wo<<": is a Identifier"<<endl;
else if(f == 4)
cout<<wo<<": is a Special character"<<endl;
}//End of else
}//End of outer loop
}//End of function
//Main function definition
int main()
{
//Declares a string array
string data[100];
//Declares a string
string st;
//To keep the length
int len = 0;
cout<<"Enter you code (To stop enter "finish"): ";
do
{
cin>>st;
//If the entered string is finish then stop
if(st == "finish")
break;
//Stores data if it is not finish
data[len] = st;
///Increases the length
len++;
}while(1);
//Calls the scan method
scan(data, len);
}
Output:
Enter you code (To stop enter "finish"): function sum
a = 2
b = 2.5
if ( a > b )
then a = b
else b = a
return a + b
end
finish
function: is a keyword
sum: is a Identifier
a: is a Identifier
=: is a Special character
2: is a Integer digit
b: is a Identifier
=: is a Special character
2.5: is a Real number
if: is a keyword
a: is a Identifier
>: is a Special character
b: is a Identifier
then: is a keyword
a: is a Identifier
=: is a Special character
b: is a Identifier
else: is a keyword
b: is a Identifier
=: is a Special character
a: is a Identifier
return: is a keyword
a: is a Identifier
+: is a Special character
b: is a Identifier
end: is a keyword
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.