Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The erty 3. (30 points) Consider the grammar that represents a stack on integers

ID: 3888267 • Letter: T

Question

The erty 3. (30 points) Consider the grammar that represents a stack on integers: stack-) Empty stack : (num stack);Non-empty stack (a) (10 points) Create an abstract syntax for stack using the define-datatype macro. (b) (10 points) Write the following procedures for stack in Scheme where stack is represented by the abstract syntax of part (a): empty-stack? determines whether a stack is empty ;; returns the val on top of a stack ; report stack underflow error if stack is empty top (stack) pop(stack)returns the stack with the top value popped off ; report stack underflow error if stack is empty push (val stack)i returns the stack vith val pushed on top of stack Here is the error code: (define report-stack-underflow (lambda ) (eopl:error 'empty-stack "Stack underflow")))

Explanation / Answer

int main (int argc, char struct ch *charactop; struct integer *integertop; char rd, op; int i = 0, op1, op2; charactop = cclearstack(); integertop = iclearstack(); while(1) { rd = argv[1][i++]; switch(rd) { case '+': case '-': case '/': case '*': case '(': charactop = cpush(charactop, rd); break; case ')': integertop = ipop (integertop, &op2); integertop = ipop (integertop, &op1); charactop = cpop (charactop, &op); while(op != '(') { integertop = ipush (integertop, eval(op, op1, op2)); charactop = cpop (charactop, &op); if (op != '(') { integertop = ipop(integertop, &op2); integertop = ipop(integertop, &op1); } } break; case '': while (! cemptystack(charactop)) { charactop = cpop(charactop, &op); integertop = ipop(integertop, &op2); integertop = ipop(integertop, &op1); integertop = ipush(integertop, eval(op, op1, op2)); } integertop = ipop(integertop, &op1); printf(" The final solution is: %d ", op1); return 0; default: integertop = ipush(integertop, rd - '0'); } } } int eval(char op, int op1, int op2) { switch (op) { case '+': return op1 + op2; case '-': return op1 - op2; case '/': return op1 / op2; case '*': return op1 * op2; } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote