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

Consider the following grammar rule in the picture below. Write the Java method

ID: 3808302 • Letter: C

Question

Consider the following grammar rule in the picture below. Write the Java method to parse these Simple Expressions using the code below as a reference.

/**

* Use as a reference

*/

S -> ‘if’ e ‘then’ block ‘else’ block

--------------------------------------------

public AST rExpr() throws SyntaxError {

AST t;

if (isNextTok(Tokens.If))

{

scan();

t = new IfTree();

t.addKid(rExpr());

expect(Tokens.Then);

t.addKid(rBlock());

expect(Tokens.Else);

t.addKid(rBlock());

return t;

}

Use this grammar rule please.

Production TYPE "float" TYPE "void S 'return' NAME (E list E SE SE Description FLOAT token VOID token Return statement for void functions Calling void functions GREAT token

Explanation / Answer

Answer: See below:

1. For 'TYPE':

---------------------------------------------------------

public AST rType() throws SyntaxError
   {
       AST t;
       if(isNextTok(Tokens.FLOAT))
       {
           t=new FloatTree();
           scan();
       }
       else if(isNextTok(Tokens.VOID))
       {
           t=new VOIDTree();
           scan();
       }
       return t;
   }

------------------------------------------

2. Return statement of void function:

-------------------------------

public AST rS() throws SyntaxError
   {
       AST t;
       if(isNextTok(Tokens.Return))
       {
           scan();
           t=new ReturnTree();
           t.addKid(rExpr());
           expect(Tokens.SemiColon);
       }
       return t;
   }

--------------------------------

3. Calling of void functions:

-------------------------------------

public AST rName() throws SyntaxError {
       AST t;
       scan();
       if (isNextTok(Tokens.LeftParenthesis)) {
           t = (new CallTree()).addKid(t);
           if (!isNextTok(Tokens.RightParenthesis)) {
               do {
                   t.addKid(rExpr());
                   if (isNextTok(Tokens.Comma)) {
                       scan();
                   } else {
                       break;
                   }
               } while (true);
           }
           expect(Tokens.RightParenthesis);
       }
       return t;
   }

----------------------------------------

4. GREAT token:

---------------------------------------

public AST rE() throws SyntaxError
   {
       AST t, kid=rSE();
       t=new GreatTree();
       if(t==null)
       {
           return kid;
       }
       t.addKid(kid);
       t.addKid(rSE());
       return t;
   }

-----------------------------------------------------

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