Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

HOW CAN I FIX THIS CODE TO MATCH THE GRAMMER PROVIDED /* * ParseNode.cpp * */ #include ParseNode.h // We want to use our getToken

HOW CAN I FIX THIS CODE TO MATCH THE GRAMMER PROVIDED

/* * ParseNode.cpp * */

#include "ParseNode.h"

// We want to use our getToken routine unchanged... BUT we want to have the ability // to push back a token if we read one too many; this is our "lookahead" // so we implement a "wrapper" around getToken

static bool pushedBack = false; static Token pushedToken;

Token GetToken(istream& in) { if( pushedBack ) { pushedBack = false; return pushedToken; }

return getToken(in); }

void PutBackToken(Token& t) { if( pushedBack ) { cout

pushedBack = true; pushedToken = t; }

// handy function to print out errors void error(string s) { cout

// Prog := Stmt | Stmt Prog ParseNode *Prog(istream& in) { ParseNode *stmt = Stmt(in);

if( stmt != 0 ) return new StatementList(stmt, Prog(in)); return 0; }

// Stmt := Set ID Expr SC | PRINT Expr SC ParseNode *Stmt(istream& in) { Token cmd = GetToken(in);

if( cmd == SET ) { Token idTok = GetToken(in); if( idTok != ID ) { error("Identifier required after set"); return 0; } ParseNode *exp = Expr(in); if( exp == 0 ) { error("expression required after id in set"); return 0; } if( GetToken(in) != SC ) { error("semicolon required"); return 0; }

return new SetStatement(idTok.getLexeme(), exp); } else if( cmd == PRINT ) { ParseNode *exp = Expr(in); if( exp == 0 ) { error("expression required after id in print"); return 0; } if( GetToken(in) != SC ) { error("semicolon required"); return 0; }

return new PrintStatement(exp); } else PutBackToken(cmd); return 0; }

ParseNode *Expr(istream& in) { return 0; }

ParseNode *Term(istream& in) { return 0; }

// Primary := ICONST | FCONST | STRING | ( Expr ) | Poly ParseNode *Primary(istream& in) { // check tokens... or call Poly return 0; }

// Poly := LCURLY Coeffs RCURLY { EvalAt } | ID { EvalAt } ParseNode *Poly(istream& in) { // note EvalAt is optional return 0; }

// notice we don't need a separate rule for ICONST | FCONST // this rule checks for a list of length at least one ParseNode *Coeffs(istream& in) { return 0; }

ParseNode *EvalAt(istream& in) { return 0; }

image text in transcribed

You will be given code segments in class that you can incorporate into your programs. The grammar for our language is as follows: Prog Stmt I Stmt Prog Stmt Set ID Expr SC I PRINT Expr SC Expr Term (+l-) Expr Term Primary Primary Primary CONST I FCONSTISTRINGI(Expr)IPoly Poly LCURLY Coeffs RCURLY EvalAt IID EvalAt Coeffs R Coeff Coeff Coeff ICONSTIFCONST EvalAt LSQ Expr RSQ Note that there are 9 rules so there may be 9 functions, one per rule; you may decide that you want to combine some rules together for ease of implementation. Some examples: set f 1, 2, 4); #fis x 2 2x 4 print should print 4 set 2,0,0,1 2xn3 1 set g (1,0, 0);#xn 2 set x 2 set gofx glxl; should be 4 (2 2) set fofgofx fI gofx 1; should be 129 You will be given code segments in class that you can incorporate into your programs. The grammar for our language is as follows: Prog Stmt I Stmt Prog Stmt Set ID Expr SC I PRINT Expr SC Expr Term (+l-) Expr Term Primary Primary Primary CONST I FCONSTISTRINGI(Expr)IPoly Poly LCURLY Coeffs RCURLY EvalAt IID EvalAt Coeffs R Coeff Coeff Coeff ICONSTIFCONST EvalAt LSQ Expr RSQ Note that there are 9 rules so there may be 9 functions, one per rule; you may decide that you want to combine some rules together for ease of implementation. Some examples: set f 1, 2, 4); #fis x 2 2x 4 print should print 4 set 2,0,0,1 2xn3 1 set g (1,0, 0);#xn 2 set x 2 set gofx glxl; should be 4 (2 2) set fofgofx fI gofx 1; should be 129

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

Filing And Computer Database Projects

Authors: Jeffrey Stewart

2nd Edition

007822781X, 9780078227813

More Books

Students also viewed these Databases questions