Microsoft Word - Exam2Notes.txt Page 1 of 4 More Old Exam Questions 1.[5 pts.] The language TeenyJ is defined like TinyJ except that the syntax of is given by: ::= UNSIGNEDINT | new int '[' ']' { '['...

1 answer below »
I have an exam on Principles of Programming Languages on May 17.


Microsoft Word - Exam2Notes.txt Page 1 of 4 More Old Exam Questions 1.[5 pts.] The language TeenyJ is defined like TinyJ except that the syntax of is given by: ::= UNSIGNEDINT | new int '[' ']' { '[' ']' } Suppose the Parser class you completed for TinyJ Assignment 1 is to be modified so that it will parse TeenyJ programs (instead of TinyJ programs). Show how you would complete the following parsing method for . (No code generation is expected.) private static void expr1() throws SourceFileErrorException { TJ.output.printSymbol(NTexpr1); TJ.output.incTreeDepth(); Solution to Problem 2: 0: PUSHSTATADDR 0 1: PUSHNUM 315 2: HEAPALLOC 3: SAVETOADDR 4: INITSTKFRM 1 5: PUSHLOCADDR 1 6: PUSHNUM 19 7: PASSPARAM 8: CALLSTATMETHOD 26 9: SAVETOADDR 10: PUSHSTATADDR 0 11: LOADFROMADDR 12: PUSHNUM 271 13: ADDTOPTR 14: PUSHLOCADDR 1 15: LOADFROMADDR 16: SAVETOADDR 17: WRITESTRING 1 9 18: PUSHSTATADDR 0 19: LOADFROMADDR 20: PUSHNUM 271 21: ADDTOPTR 22: LOADFROMADDR 23: WRITEINT 24: WRITELNOP 25: STOP 26: INITSTKFRM 0 27: PUSHLOCADDR -2 28: LOADFROMADDR 29: PUSHNUM 3 30: LT 31: JUMPONFALSE 35 32: PUSHNUM 0 33: RETURN 1 34: JUMP 45 35: PUSHLOCADDR -2 36: LOADFROMADDR 37: PUSHLOCADDR -2 38: LOADFROMADDR 39: PUSHNUM 1 40: SUB 41: PASSPARAM 42: CALLSTATMETHOD 26 43: SUB 44: RETURN 1 TJ.output.decTreeDepth(); } Page 2 of 4 2.[10 pts.] Complete the table below the following program to show the TinyJ virtual machine instructions that should be generated by TJasn.TJ (after completion of TinyJ Assignment 2) for this TinyJ program. class ExamQ { static int b[] = new int[315]; public static void main (String args[]) { int i = g(19); b[271] = i; System.out.print("g(19) is "); System.out.println(b[271]); } static int g(int m) { if (m < 3) return 0; else return m-g(m-1); } } 0: pushstataddr 0 1: pushnum 315 2: ___________________ 3: savetoaddr 4: ___________________ 5: ___________________ 6: ___________________ 7: ___________________ 8: ___________________ 9: ___________________ 10:___________________ 11:___________________ 12:___________________ 13:___________________ 14:___________________ 15:___________________ 16:___________________ 17:___________________ 18:___________________ 19:___________________ 20:___________________ 21:___________________ 22:___________________ 23:___________________ 24:___________________ 25:___________________ 26:___________________ 27:___________________ 28:___________________ 29:___________________ 30:___________________ 31:___________________ 32:___________________ 33:___________________ 34: jump 45 35:___________________ 36:___________________ 37:___________________ 38:___________________ 39:___________________ 40:___________________ 41:___________________ 42:___________________ 43:___________________ 44: return 1 hint: among the 40 instructions you are asked to write, there are 7 loadfromaddr instructions, 6 pushnum instructions, 5 pushlocaddr instructions, 2 each of the addtoptr, callstatmethod, initstkfrm, passparam, pushstataddr, savetoaddr, and sub instructions, and 1 each of the heapalloc, jumponfalse, lt, return, stop, writeint, writelnop, and writestring instructions. page 3 of 4 while reading this page and the next, you should refer back when necessary to the pages of https://euclid.cs.qc.cuny.edu/316/memory-allocation-vm-instruction-set-and-hints-for-asn-2.pdf that specify the effects of executing each vm instruction. comments on problem 2 regarding the translation of the statements b[271] = i; and system.out.println(b[271]); note: the exprstack column on the right shows the items on the expression evaluation stack immediately after each vm instruction has been executed. the stack grows downwards––when more than one item is on the stack the first line below the word exprstack refers to the bottom item on the stack. b[271] = i; is translated into the seven vm instructions that are shown on the left below. these instructions are put into code memory at addresses 10 – 16, as indicated on p. 1. pushstataddr 0 pushes pointer to b. exprstack ptr to b loadfromaddr pops pointer to b. exprstack pushes the pointer to b[0] that is stored in b's location. ptr to b[0] pushnum 271 pushes the integer 271. exprstack ptr to b[0] 271 addtoptr pops 271 and pointer to b[0]. exprstack pushes (pointer to b[0]) + 271 (i.e., pointer to b[271]). ptr to b[271] pushlocaddr 1 pushes pointer to i. 3)="" return="" 0;="" else="" return="" m-g(m-1);="" }="" }="" 0:="" pushstataddr="" 0="" 1:="" pushnum="" 315="" 2:="" ___________________="" 3:="" savetoaddr="" 4:="" ___________________="" 5:="" ___________________="" 6:="" ___________________="" 7:="" ___________________="" 8:="" ___________________="" 9:="" ___________________="" 10:___________________="" 11:___________________="" 12:___________________="" 13:___________________="" 14:___________________="" 15:___________________="" 16:___________________="" 17:___________________="" 18:___________________="" 19:___________________="" 20:___________________="" 21:___________________="" 22:___________________="" 23:___________________="" 24:___________________="" 25:___________________="" 26:___________________="" 27:___________________="" 28:___________________="" 29:___________________="" 30:___________________="" 31:___________________="" 32:___________________="" 33:___________________="" 34:="" jump="" 45="" 35:___________________="" 36:___________________="" 37:___________________="" 38:___________________="" 39:___________________="" 40:___________________="" 41:___________________="" 42:___________________="" 43:___________________="" 44:="" return="" 1="" hint:="" among="" the="" 40="" instructions="" you="" are="" asked="" to="" write,="" there="" are="" 7="" loadfromaddr="" instructions,="" 6="" pushnum="" instructions,="" 5="" pushlocaddr="" instructions,="" 2="" each="" of="" the="" addtoptr,="" callstatmethod,="" initstkfrm,="" passparam,="" pushstataddr,="" savetoaddr,="" and="" sub="" instructions,="" and="" 1="" each="" of="" the="" heapalloc,="" jumponfalse,="" lt,="" return,="" stop,="" writeint,="" writelnop,="" and="" writestring="" instructions.="" page="" 3="" of="" 4="" while="" reading="" this="" page="" and="" the="" next,="" you="" should="" refer="" back="" when="" necessary="" to="" the="" pages="" of="" https://euclid.cs.qc.cuny.edu/316/memory-allocation-vm-instruction-set-and-hints-for-asn-2.pdf="" that="" specify="" the="" effects="" of="" executing="" each="" vm="" instruction.="" comments="" on="" problem="" 2="" regarding="" the="" translation="" of="" the="" statements="" b[271]="i;" and="" system.out.println(b[271]);="" note:="" the="" exprstack="" column="" on="" the="" right="" shows="" the="" items="" on="" the="" expression="" evaluation="" stack="" immediately="" after="" each="" vm="" instruction="" has="" been="" executed.="" the="" stack="" grows="" downwards––when="" more="" than="" one="" item="" is="" on="" the="" stack="" the="" first="" line="" below="" the="" word="" exprstack="" refers="" to="" the="" bottom="" item="" on="" the="" stack.="" b[271]="i;" is="" translated="" into="" the="" seven="" vm="" instructions="" that="" are="" shown="" on="" the="" left="" below.="" these="" instructions="" are="" put="" into="" code="" memory="" at="" addresses="" 10="" –="" 16,="" as="" indicated="" on="" p.="" 1.="" pushstataddr="" 0="" pushes="" pointer="" to="" b.="" exprstack="" ptr="" to="" b="" loadfromaddr="" pops="" pointer="" to="" b.="" exprstack="" pushes="" the="" pointer="" to="" b[0]="" that="" is="" stored="" in="" b's="" location.="" ptr="" to="" b[0]="" pushnum="" 271="" pushes="" the="" integer="" 271.="" exprstack="" ptr="" to="" b[0]="" 271="" addtoptr="" pops="" 271="" and="" pointer="" to="" b[0].="" exprstack="" pushes="" (pointer="" to="" b[0])="" +="" 271="" (i.e.,="" pointer="" to="" b[271]).="" ptr="" to="" b[271]="" pushlocaddr="" 1="" pushes="" pointer="" to="">
Answered 10 days AfterMay 07, 2021

Answer To: Microsoft Word - Exam2Notes.txt Page 1 of 4 More Old Exam Questions 1.[5 pts.] The language TeenyJ...

Swapnil answered on May 17 2021
136 Votes
1
    private static void parameterDeclList() throws SourceFileErrorException
{
TJ.output.printS
ymbol(NTparameterDeclList);
TJ.output.incTreeDepth();
if (getCurrentToken() == INT)
{
parameterDecl();
while (getCurrentToken() == COMMA)
{
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here