Question

In: Computer Science

C++ langugae only The first phase of compilation is called scanning or lexical analysis. This phase...

C++ langugae only

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.

Your job is to write (in C++) a simple scanner for source code that obeys the language described below. You may assume that the input is syntactically correct.

Your program simply needs to read each token and then print out what type it is, based on the following language rules:

keyword -> if | then | else | begin | end | function | return 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<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 Solutions

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...
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...
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...
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...
1)Lexical heads are typically Select one: a. closed class lexical items b. phonologically null c. no...
1)Lexical heads are typically Select one: a. closed class lexical items b. phonologically null c. no class lexical items d. open class lexical items 2)The subject of the sentence `the woman with a big dog on a flimsy leash in the public park behind my house saw a horse' is: Select one: a. the woman with a big dog b. the woman c. the woman with a big dog on a flimsy leash in the public park behind my house...
C program only There is a documented prototype for a function called get_a_line in the .c...
C program only There is a documented prototype for a function called get_a_line in the .c file. Write a definition for get_a_line—the only function called from that definition should be fgetc. You can use the given main function to test your code. You should run the executable several times to check different cases: Type Ctrl-D in response to the prompt for input. get_a_line should fail to read any characters, put a ’\0’ character at the beginning of the array, and...
(a) How are rules for lexical analysis written? (b) What are these rules used for?
(a) How are rules for lexical analysis written? (b) What are these rules used for?
The overuse of antibiotics and CT scanning are not only unnecessary and wasteful, but it may...
The overuse of antibiotics and CT scanning are not only unnecessary and wasteful, but it may create potential harm for patients. According to Centers for Disease Control and Prevention, of the estimated 154 million prescriptions for antibiotics written in doctor’s offices and emergency departments each year, 30% are unnecessary (CDC, 2016). CDC researchers found that most of these unnecessary antibiotics prescribed are for common colds, viral sore throats, bronchitis, and sinus and ear infections that do not respond to antibiotics....
This is for a hotel project. The first phase is a restaurant and the 2nd phase...
This is for a hotel project. The first phase is a restaurant and the 2nd phase is the hotel. It will be located on a small island in the Caribbean in the United States Virgin Islands. Vision statement Mission statement Product line or services provided Philosophy of the business The business and its environment General environment characteristics Industry environment and competition Location description Distinctive competencies
Write two functions in C++ called readData() and findAverage(). Please answer it in C++ The first...
Write two functions in C++ called readData() and findAverage(). Please answer it in C++ The first function should take a reference to an ifstream object and a reference to an array of double, read data into the array from the file and return the number of values read. It should stop reading when the end of the file is reached. The data file contains prices of an unspecified item, one per line. The second function takes a reference to an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT