Java Program to Add variable to current scope

How to write a C Program to Add variable to current scope in Java Programming ?


Solution:

  1.  @Override
  2.   public Void visitInitialisation(InitialisationContext ctx) {
  3.     // TODO Add variable to current scope
  4.     // TODO Check RHS type is equal to variable type
  5.     // System.out.println("Visiting initialisation context");
  6.     String type = ctx.type().getText();
  7.     String ident = ctx.ident().getText();
  8.     try {
  9.       scopeHandler.add(ident, type);
  10.     } catch (Exception e) {
  11.       System.out.println(e.getMessage());
  12.     }
  13.     visit(ctx.assign_rhs());
  14.     if (!nodeType.equals(type)){
  15.       if(type.contains("[]") && type.substring(0, type.length() -2).equals(nodeType)){
  16.         return super.visitInitialisation(ctx);
  17.       }
  18.       System.err.println("Error: Incompatible type at ' "
  19.           + ctx.assign_rhs().getText() + " ' (Expected: " + type + ", Actual: "
  20.           + nodeType + ")");
  21.     }
  22.  
  23.     return super.visitInitialisation(ctx);
  24.   }


Learn More :

Add
    Scope
      Variable

        Learn More Multiple Choice Question :