Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you please implement a top-down recursive descent parser and build a simple symbol table to properly resolve references within polynomial expressions, in addition to

Can you please implement a top-down recursive descent parser and build a simple symbol table to properly resolve references within polynomial expressions, in addition to represent the polynomial expression as an Abstract Syntax Tree (AST) that will later be evaluated to the result of the polynomial expression in C or C++?

The input to the program is divided into two distinct parts: 1. The first part defines a polynomial to be represented as an abstract syntax tree. 2. The second part evaluates the polynomial with the corresponding set of inputs.

Your polynomial evaluator is responsible for reporting syntax errors, semantic errors, and evaluating the resulting AST from the supplied inputs. Overview of the program is shown below:

image text in transcribed

Example Inputs to your parser with the corresponding expected output:

POLY X^3 + X + 2; EVAL INPUT X = 4;

The resulting output of this input is as follows below: 70

If no errors exist within the input, then you should evaluate the polynomials and print the results of each evaluation on a separate line. For example, if the resulting input is: POLY X * Y;

EVAL INPUT X = 3; INPUT Y = 2; EVAL INPUT X = 5; INPUT Y = 7; INPUT M = 12;

The resulting output of the polynomial evaluations would then be: 6 35

Here are more test cases that are place in a folder to be used as input, the expected file includes the expected output:

test_01.txt:

POLY X^2 + 7;

EVAL INPUT X = 1;

test_01.txt.expected:

8

test_02.txt:

POLY X^2 + 3 * Y * 7;

EVAL INPUT X = 1; INPUT Y = 2;

EVAL INPUT X = 12; INPUT Y = 7;

EVAL INPUT X = 5; INPUT Y = 11;

test_02.txt.expected:

43 291 256

We also have to keep in mind of Error Codes 1 and 2,

Error Code 1 is risen when a variable re-declaration occurs within the input. This occurs when an input variable with the same identifier is defined more than one time within a single EVAL section. The structure of the error reported should be as follows:

ERROR CODE 1: ...

where to represents the line numbers in which a re-defined variable occurs in the evaluation section. Additionally, the number of error codes correlate to the number of evaluation blocks where a variable re-declaration error occurs.

An instance of this error and its output is shown below: POLY Y^4 + Y * X + myZ; EVAL INPUT X = 1; INPUT X = 23; INPUT Y = 8; INPUT Y = 10; EVAL INPUT X = 2; INPUT X = 3; INPUT Y = 4;

For this input, the resulting error code printout should be as follows: ERROR CODE 1: 4 5 6 7 ERROR CODE 1: 10 11

Error Code 2 is risen when an undefined variable occurs within the evaluation. This error occurs when an evaluation section (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: ...

where to represents the line number of the corresponding evaluation section where a variable of the polynomial is not defined.

An instance of this error is demonstrated below: POLY X^2 + 2 * Y + 3 + Z; EVAL INPUT X = 1; EVAL INPUT Y = 1; INPUT Z = 5; INPUT X = 3;

EVAL INPUT Y = 3;

For this input, the resulting error code printout should be: ERROR CODE 2: 3 3 ERROR CODE 2: 11 11

PARTI { POLY X^3 + x + 2; EVAL INPUT X = 2; AST 12 1012 740 I am OUTPUT EVAL INPUT X = 10; INPUT Y = 11; INPUT PART 11 Symbol Table EVAL INPUT X = 9; Symbol Table Symbol Table

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

Professional Microsoft SQL Server 2014 Integration Services

Authors: Brian Knight, Devin Knight

1st Edition

1118850904, 9781118850909

More Books

Students also viewed these Databases questions