Question: Syntax Analyzer (Parser) Recursive Descent Parser Language C++ Using the following grammar: P -> program DEC ST ; DEC -> TYPE VAR ; DEC |

Syntax Analyzer (Parser) Recursive Descent Parser

Language C++

Using the following grammar:

P -> program DEC ST ;

DEC -> TYPE VAR ; DEC | begin

TYPE -> integer | real

VAR -> idtoken

ST -> VAR := M ; ST | end

M -> E | OPERAND

OPERAND -> VAR | OP1

OP1 -> numtoken

E -> T E1 | T

E1 -> + T E1 | - T E1 | + T | - T

T -> F T1 | F

T1 -> * F T1 | / F T1 | * F | / F

F = ( E ) | OPERAND

Build a recursive descent parser for MYC language.

Below is the MYC language in text.myc format

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//this program has no errors program integer a; real b; begin a := 100; b := 10.128+a* 1000; c := a + 10; end;

------------------------------------------------------------------------------------------------------------------------------------------------------------------

You will now take the tokens generated by your scanner and using recursive descent parse to determine if they represent a syntactically valid program.

Below is my scanner code (please fix error if there is any):

//Header files #include #include #include using namespace std;

string removeSpaces(string str) // string to filter out spaces in file being read { int n = str.length();

int i = 0, j = -1;

bool spaceFound = false; // this continue to interate as long as no space is found and returns false while (++j < n && str[j] == ' ');

while (j < n) // iterate over the number of spaces in the string by making the spance in a line is < 1 {

if (str[j] != ' ') // this condition satify that if there is an empty space in a string line, it should be deleted. {

if ((str[j] == '.' || str[j] == ',' || // Unnecessary spaces after punctuaations are erased if the condition does not satify str[j] == '?') && i - 1 >= 0 && str[i - 1] == ' ') str[i - 1] = str[j++];

else

str[i++] = str[j++]; // if there are no space found continue to iterate the in the same scope of string. spaceFound = false; }

else if (str[j++] == ' ') {

if (!spaceFound) { str[i++] = ' '; spaceFound = true; // if there are spaces found, cosider the next condition to erase } } } if (i <= 1) str.erase(str.begin() + i, str.end()); // erase extra space else str.erase(str.begin() + i - 1, str.end()); return str; // return back to the next line to intereate over the bool condition to check fot next space }

string removeComments(string prgm) //string to filter out spaces in file being read { int n = prgm.length(); string res; bool s_cmt = false; bool m_cmt = false; for (int i = 0; i < n; i++) { if (s_cmt == true && prgm[i] == ' ') // if there are no space found continue to iterate the in the same scope of string s_cmt = false; else if (m_cmt == true && prgm[i] == '*' && prgm[i + 1] == '/')// ignore stings in a cout m_cmt = false, i++; else if (s_cmt || m_cmt) continue; else if (prgm[i] == '/' && prgm[i + 1] == '/') s_cmt = true, i++; else if (prgm[i] == '/' && prgm[i + 1] == '*') m_cmt = true, i++; else res += prgm[i]; } return res; } int main() // main function {//input/output file declaration string prgm; ifstream infile; infile.open("test2.myc"); // reading from file if (!infile) { cout << "Unable to open file" << endl; // message to display for inappropriate files apart from myc return 0; } string line; while (getline(infile, line)) // read in myc file { prgm += line; prgm.push_back(' '); // erases extra lines in the reading file of myc } return 0; // end program }

The program in MYC has the following format:

PROGRAM declarations BEGIN statements END; There can be any number of variable declarations and they have the following format: type variable; where type is either INTEGER or REAL and a variable is only one variable has to follow the format that we considered in our scanner. There can be any number of statements with each statement followed by a semicolon. There is only one type of statements: assignments. Its general form is: variable := math_expression; Math expressions can have either of the following forms: operand binary_op operand operand where the possible binary_op could be any of the following: '+', '-', '*', and '/'. An operand can be either a variable or a number. Note that expressions are prioritized as the following: expressions between parentheses, * and /, and then + and -, all from left to right associativity. Here is a sample program in MYC: PROGRAM INTEGER A; REAL B; BEGIN B := 2.3; B := B * 5.0; A := 10; A := A * (B * 10); END;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!