Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//main.cpp /* Simple integer arithmetic calculator according to the following BNF exps --> exp | exp NEWLINE exps exp --> term {addop term} addop -->

image text in transcribed

image text in transcribed

//main.cpp /* Simple integer arithmetic calculator according to the following BNF exps --> exp | exp NEWLINE exps exp --> term {addop term} addop --> + | - term --> factor {mulop factor} mulop --> * | / factor --> ( exp ) | INT */ #include  #include  #include  #include  #include  #include "tokens.h" #include "FlexLexer.h" using namespace std; string toknames[] = { "INT", "LPAREN", "RPAREN", "PLUS", "MINUS", "TIMES", "DIVIDE", "NEWLINE" }; string tokname(int tok) { return tok264 ? "BAD_TOKEN" : toknames[tok-257]; } yyFlexLexer lexer; YYSTYPE yylval; int nextToken; //global variable stores the token to be processed void readNextToken( void ); //read the next token into the variable nextToken void exps( void ); //process all expressions in the input int exp( void ); //returns the integer value of an expression int term ( void ); //returns the integer value of an term int factor( void ); //returns the integer value of an factor //If the next token matches expectedToken, read the next token and return true //otherwise, print an error message and return false bool match( int expectedToken ); //print the error message void error( string errorMsg ); //skip the rest of the line void skipline( void ); int main(int argc, char **argv) { ifstream ifs; if (argc!=2) { cerr  
//test0.txt.bak 10 - 5 - 20 / 3 -5 (10 + 5) / 3 (10 - 5 * 3) - 10 / 20 - 0 (10 - 5 * 3 - 10 / 20 - 0 10 + 5 * 3 / 2 - 9

image text in transcribed

I didn't print it correctly.

//This is what the output supposed to look like Lexcial Analysis of the file test0.txt expression 1 : -1 expression 2 : wrong syntax -- Error: Token LPAREN or INT expected! expression 3 : 5 expression 4 : -5 expression 5 : wrong syntax -- Error: Token RPAREN expected! expression 6 : 8

Can you help me with a recursive descent parser in C++?

CSCI 3210 Project 3 Due: see class calendar Goal: To know how to construct a recursive descent parser. Description: As we know, one way to detect if a program has syntax error is to check if a derivation or a parse tree can be constructed for the program. The process of constructing a derivation is called parsing. The group of software that implements the parsing is called parser. A recursive descent parser is a kind of top-down parser built from a set of mutually recursive procedures (or a non-recursive equivalent) where each such procedure implements one of the non-terminals of the grammar. Thus the structure of the resulting program closely mirrors that of the grammar it recognizes. In this assignment, you need to implement a recursive descent parser in C++ for the following CFG : 1.exps2.exp3.addop4.term5.mulop6.factor>exp|expNEWLINEexps>term{taddopterm}>+>factor{mulopfactor}>f/>(exp)|INT The 1st production defines exps as an individual expression, or a sequence of expressions separated by NEWLINE token. The 2nd production describes an expression as a term followed by addop term zero, one, or more times, i.e. exp can be: term, or term addop term, or term addop term addop term, or term addop term addop term addop term, ... The 4th production describes the definition of term, which is pretty much similar to 2nd production. The 6th production defines a factor either as an expression enclosed by a pair of parentheses or an integer. In recursive descent parsing, a function is defined for each non-terminal, i.e. exps, exp, term, and factor in our case. The non-terminals addop and mulop are too simple so that we will process them directly in functions of exp and term respectively. When implementing a function for a non-terminal, you should follow the productions defined for that non-terminal. In this assignment, each function should return an integer, which is the value of the (sub)expressions to be evaluated. For your convenience, a partial C sample solution is provided for the CFG at the end of this document. This sample is only for your reference. A java solution to the similar problem can also be found here: http://web.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/27.Recursive-Descent-Parsing.pdf. Please be noted that unlike the given example solutions, the solution of this assignment should be based on TOKENS, not characters. You should use tokens like PLUS instead of chars like '+'. All tokens are defined in token.h file. You should use yylval.ival to get the integer value of the INT token. How to compile the project? 1. Just build MainDriver project. 2. Then, you should be able to run the program. Remember, the program read expressions from test0.txt. What to do in this project? You only need to work on main.epp file by providing implementation of three functions: exp, term, and factor. Each function returns an integer as the value of sub-expression that has been parsed/evaluated so far. If there is a syntax error detected, please throw a runtime exception so that function exps can skip the rest of the expressions and continue the parsing of next expressions. The syntax of throwing a runtime exception is given below: throw runtime_error( a string of error information); In this CFG, you typically can detect errors in factor function. How to submit? - Submit main.cpp file only to D2L dropbox. Partial Sample C solution for the CFG. The functions for nonterminal factor and term are not shown. Your solution to the problem will be different, +1 term { imulop } mulop > * factor (exp) Number int temp =term();// recognize term //repeat as long as the next token is 't' or '-' d // token is the global variable to hold the token //to be processed while ( (token == '+') ||( token == ') ) \{ switch (token) \{ case ' + ': match (++); temp +=term(); break; case '- ": match (1); temp ==term(); break; \} return temp; \} // match the expected token with the current one and read /ext token to the global variable void match ( char expectedToken) f if ( token == expectedToken) token = getchar ();// read next token else error (); //report an error \} (base) jovyan@jupyter-mbh5j: /csci 3210\$ make g+t -Wall -g -w -I/homeewhall/include -I. -o main lex.yy.cc main.cpp FlexLexer.h tokens. h Project1 has been compiled (base) jovyan@jupyter-mbh5j: /csci 3210\$./main testo.txt.bak Lexcial Analysis of the file testo.txt.bak expression 1 : wrong syntax -- Error: Token LPAREN or INT expected! expression 2 : wrong syntax -- Error: Token LPAREN or INT expected! expression 3 : wrong syntax -- Error: Token LPAREN or INT expected! expression 4 : wrong syntax -- Error: Token LPAREN or INT expected! expression 5 : wrong syntax -- Error: Token LPAREN or INT expected! expression 6 : wrong syntax -- Error: Token LPAREN or INT expected! (base) jovyan@jupyter-mbh5j: /csci 3210$

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Horse Betting The Road To Absolute Horse Racing 2

Authors: NAKAGAWA,YUKIO

1st Edition

B0CFZN219G, 979-8856410593

More Books

Students also viewed these Databases questions

Question

What is cost-volume-pro fit analysis?

Answered: 1 week ago