Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE HELP , SHOW YOUR WORK, USE THE CODE GIVEN TO YOU, SHARE SCREENSHOTS OF YOU ACHIEVING THE EXAMPLES BELOW, this code evaluates around the

PLEASE HELP, SHOW YOUR WORK, USE THE CODE GIVEN TO YOU, SHARE SCREENSHOTS OF YOU ACHIEVING THE EXAMPLES BELOW, this code evaluates around the use of implementing a simple polynomial-parse tree evaluator. This is executed through the following files (please read and/or copy the following code, (You are OPEN Modify the files to your content):

parser.cc (this file includes #include "parser.h" & #include "lexer.h")

image text in transcribed

parser.h

#ifndef __PARSER_H__ #define __PARSER_H__

#include #include

#include "lexer.h"

using namespace std;

class Parser { public: void ConsumeAllInput();

private: LexicalAnalyzer lexer; void syntax_error(); Token expect(TokenType expected_type); //Token myParser(); };

string reserved[] = { "END_OF_FILE", "POLY", "EVAL", "INPUT", "EQUAL", "ID", "POWER", "NUM", "PLUS", "SEMICOLON", "ERROR", "MULT" };

#define KEYWORDS_COUNT 3 string keyword[] = { "POLY", "EVAL", "INPUT" };

void Token::Print() { cout lexeme token_type] line_no

// The constructor function will get all token in the input and stores them in an // internal vector. This faciliates the implementation of peek() LexicalAnalyzer::LexicalAnalyzer() { this->line_no = 1; tmp.lexeme = ""; tmp.line_no = 1; tmp.token_type = ERROR;

Token token = GetTokenMain(); index = 0;

while (token.token_type != END_OF_FILE) { tokenList.push_back(token); // push token into internal list token = GetTokenMain(); // and get next token from standatd input } // pushes END_OF_FILE is not pushed on the token list }

bool LexicalAnalyzer::SkipSpace() { char c; bool space_encountered = false;

input.GetChar(c); line_no += (c == ' ');

while (!input.EndOfInput() && isspace(c)) { space_encountered = true; input.GetChar(c); line_no += (c == ' '); }

if (!input.EndOfInput()) { input.UngetChar(c); } return space_encountered; }

bool LexicalAnalyzer::IsKeyword(string s) { for (int i = 0; i

TokenType LexicalAnalyzer::FindKeywordIndex(string s) { for (int i = 0; i

Token LexicalAnalyzer::ScanNumber() { char c;

input.GetChar(c); if (isdigit(c)) { if (c == '0') { tmp.lexeme = "0"; } else { tmp.lexeme = ""; while (!input.EndOfInput() && isdigit(c)) { tmp.lexeme += c; input.GetChar(c); } if (!input.EndOfInput()) { input.UngetChar(c); } } tmp.token_type = NUM; tmp.line_no = line_no; return tmp; } else { if (!input.EndOfInput()) { input.UngetChar(c); } tmp.lexeme = ""; tmp.token_type = ERROR; tmp.line_no = line_no; return tmp; } }

Token LexicalAnalyzer::ScanIdOrKeyword() { char c; input.GetChar(c);

if (isalpha(c)) { tmp.lexeme = ""; while (!input.EndOfInput() && isalnum(c)) { tmp.lexeme += c; input.GetChar(c); } if (!input.EndOfInput()) { input.UngetChar(c); } tmp.line_no = line_no; if (IsKeyword(tmp.lexeme)) tmp.token_type = FindKeywordIndex(tmp.lexeme); else tmp.token_type = ID; } else { if (!input.EndOfInput()) { input.UngetChar(c); } tmp.lexeme = ""; tmp.token_type = ERROR; } return tmp; }

// GetToken() accesses tokens from the tokenList that is populated when a // lexer object is instantiated Token LexicalAnalyzer::GetToken() { Token token; if ((size_t)index == tokenList.size()) { // return end of file if token.lexeme = ""; // index is too large token.line_no = line_no; token.token_type = END_OF_FILE; } else{ token = tokenList[index]; index = index + 1; } return token; }

// peek requires that the argument "howFar" be positive. Token LexicalAnalyzer::peek(int howFar) { if (howFar

int peekIndex = index + howFar - 1; if ((size_t)peekIndex > (tokenList.size()-1)) { // if peeking too far Token token; // return END_OF_FILE token.lexeme = ""; token.line_no = line_no; token.token_type = END_OF_FILE; return token; } else return tokenList[peekIndex]; }

Token LexicalAnalyzer::GetTokenMain() { char c;

SkipSpace(); tmp.lexeme = ""; tmp.line_no = line_no; tmp.token_type = END_OF_FILE;

if (!input.EndOfInput()) input.GetChar(c); else return tmp;

switch (c) { case ';': tmp.token_type = SEMICOLON; return tmp; case '^': tmp.token_type = POWER; return tmp; // case '-': tmp.token_type = MINUS; return tmp; case '+': tmp.token_type = PLUS; return tmp; case '=': tmp.token_type = EQUAL; return tmp; // case '(': tmp.token_type = LPAREN; return tmp; // case ')': tmp.token_type = RPAREN; return tmp; // case ',': tmp.token_type = COMMA; return tmp; case '*': tmp.token_type = MULT; return tmp; default: if (isdigit(c)) { input.UngetChar(c); return ScanNumber(); } else if (isalpha(c)) { input.UngetChar(c); return ScanIdOrKeyword(); } else if (input.EndOfInput()) tmp.token_type = END_OF_FILE; else tmp.token_type = ERROR;

return tmp; } }

bool InputBuffer::EndOfInput() { if (!input_buffer.empty()) return false; else return cin.eof(); }

char InputBuffer::UngetChar(char c) { if (c != EOF) input_buffer.push_back(c);; return c; }

void InputBuffer::GetChar(char& c) { if (!input_buffer.empty()) { c = input_buffer.back(); input_buffer.pop_back(); } else { cin.get(c); } }

string InputBuffer::UngetString(string s) { for (size_t i = 0; i

#endif

lexer.h

#ifndef __LEXER__H__ #define __LEXER__H__

#include #include

#include "inputbuf.h"

// ------- token types -------------------

typedef enum { END_OF_FILE = 0, POLY, EVAL, INPUT, EQUAL, ID, POWER, NUM, PLUS, SEMICOLON, ERROR, MULT } TokenType;

class Token { public: void Print();

std::string lexeme; TokenType token_type; int line_no; };

class LexicalAnalyzer { public: Token GetToken(); Token peek(int); LexicalAnalyzer();

private: std::vector tokenList; Token GetTokenMain(); int line_no; int index; Token tmp; InputBuffer input;

bool SkipSpace(); bool IsKeyword(std::string); TokenType FindKeywordIndex(std::string); Token ScanNumber(); Token ScanIdOrKeyword(); };

#endif //__LEXER__H__

inputbuf.h

#ifndef __INPUT_BUFFER__H__ #define __INPUT_BUFFER__H__

#include #include

class InputBuffer { public: void GetChar(char&); char UngetChar(char); std::string UngetString(std::string); bool EndOfInput();

private: std::vector input_buffer; };

#endif //__INPUT_BUFFER__H__

Assume you have test cases that includes the following text files such as:

test_01.txt:

POLY X^2 * 2 * X + 10;

EVAL INPUT X = 2;

EVAL INPUT X = 7;

EVAL INPUT Y = 7; INPUT X = 10;

Assuming you use a Git Bash terminal instead of powershell in visual studio code

And execute the following command: ~$ g++ -std=c++17 parser.cc parser.h -o prog.out

Then ~$ ./prog.out myoutput_01.txt

**As of now, if we execute the following command the, output is not correct, but very informative**

**Instead, what we want is to achieve to display the following outputs**

We should get our myoutput_01.txt file to read:

26 696 2010

When dealing with syntax, ensure the POLY line is written correctly (must end with ; no operation signs next to each other, the names such as EVAL, INPUT, POLY following test_01 are named and placed correctly in the file, ensure the inputs are placed under EVAL, and that the program defines all possible ID INPUTS under EVAL that are covered by POLY, otherwise they return a SYNTAX ERROR !!&%!!

We should also consider error codes and print them as they should:

image text in transcribed

the following example above would output

ERROR CODE 1: 4 5 6 7 ERROR CODE 1: 10 11

image text in transcribed

the following example above would output

ERROR CODE 2: 3 3 ERROR CODE 2: 11 11

How would you be able to achieve the following output to match the expected output? Can you also include syntax errors using the expected function as well? Please help, I really want to learn and understand this, Thank you (You are OPEN Modify the files to your content)

include "parser.h" #include #include #include #include using namespace std; Evoid Parser::syntax_error() cout ... ERROR CODE 1: where to represents the line numbers of each re-defined variable that occurs in an evaluation section. Additionally, the mimber of error code signatures printed correlate to the number of evaluation blocks where a variable re-declaration error occurs (e.g., if two evaluation blocks contain a variable re-declaration error, then the error code signature will be printed twice). An instance of this error and its output is shown below: POLY Y*4 + Y * X + myz; 1 2 3 4 5 EVAL INPUT X = 1; INPUT X INPUT Y 8; INPUT Y = 10; 23; 6 == 7 8 9 10 EVAL INPUT X = 2; INPUT X = 3; INPUT Y = 4; 11 WN .... 12 4.2.2 Error Code 2 Error Code 2 is risen when an undefined variable occurs within the evaluation. This error occurs when an evaluation block (i.e., a EVAL section) does not properly define all the variables required for a complete evaluation of the polynomial. The structure output of this error should be as follows: ERROR CODE 2: 1> 1> . ERROR CODE 2: where #include #include #include using namespace std; Evoid Parser::syntax_error() cout ... ERROR CODE 1: where to represents the line numbers of each re-defined variable that occurs in an evaluation section. Additionally, the mimber of error code signatures printed correlate to the number of evaluation blocks where a variable re-declaration error occurs (e.g., if two evaluation blocks contain a variable re-declaration error, then the error code signature will be printed twice). An instance of this error and its output is shown below: POLY Y*4 + Y * X + myz; 1 2 3 4 5 EVAL INPUT X = 1; INPUT X INPUT Y 8; INPUT Y = 10; 23; 6 == 7 8 9 10 EVAL INPUT X = 2; INPUT X = 3; INPUT Y = 4; 11 WN .... 12 4.2.2 Error Code 2 Error Code 2 is risen when an undefined variable occurs within the evaluation. This error occurs when an evaluation block (i.e., a EVAL section) does not properly define all the variables required for a complete evaluation of the polynomial. The structure output of this error should be as follows: ERROR CODE 2: 1> 1> . ERROR CODE 2: where

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

Students also viewed these Databases questions