Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用侦听器的ANTLR 4解释器_Java_If Statement_Listener_Interpreter_Antlr4 - Fatal编程技术网

Java 使用侦听器的ANTLR 4解释器

Java 使用侦听器的ANTLR 4解释器,java,if-statement,listener,interpreter,antlr4,Java,If Statement,Listener,Interpreter,Antlr4,在java中,是否可以使用侦听器而不是访问者来评估antlr4中的IF语句?如果是,你能建议怎么做吗 我想为我的语言做一个翻译。我使用了antlr生成的侦听器来实现它。我已经完成了一些表达式的语义分析和评估,现在我不知道如何评估IF,WHILE,FOR等函数调用 我的antlr4语法不包括词汇规则: grammar CodeCraftGrammar; @header{ package gen; } program : constantStatement* mainFunction

在java中,是否可以使用侦听器而不是访问者来评估antlr4中的
IF
语句?如果是,你能建议怎么做吗

我想为我的语言做一个翻译。我使用了antlr生成的侦听器来实现它。我已经完成了一些表达式的语义分析和评估,现在我不知道如何评估
IF
WHILE
FOR
等函数调用

我的antlr4语法不包括词汇规则:

grammar CodeCraftGrammar;

@header{
package gen;
}

program
    :  constantStatement* mainFunction functionDeclaration* EOF 
    ;
constantStatement 
    : ABSOLUTE dataType ID ASSIGN expression SEMI     
    ;
variableDeclaration 
    : dataType ID 
    ;
dataType
    : INT       
    | FLOAT     
    | CHAR     
    | STRING    
    | BOOLEAN   
    ;
functionDeclaration
    : returnType ID parameterList block
    ;
returnType
    : dataType #returnDataType
    | DARKNESS  #returnDarkness
    ;
parameterList
    : LPAREN parameter? (COMMA parameter)* RPAREN
    ;
parameter
    : dataType ID
    ;
block
    : LBRACE statement* RBRACE
    ;
mainFunction
    : DARKNESS MINE parameterList block
    ;

statement
    : block #blockStatement
    | variableDeclaration SEMI  #varDec
    | assignmentStatement SEMI  #assign
    | functionCallStatement SEMI #funcCall
    | ifStatement   #if
    | whileStatement    #while
    | doWhileStatement  #dowhile
    | forStatement  #for
    | returnStatement   #return
    | CHOKE SEMI    #choke
    ;
assignmentStatement
    : ID ASSIGN expression
    ;
functionCallStatement
    : ID actualParameters  #funcCallID
    | PRINT actualParameters   #funcCallPrint
    | PRINTLN actualParameters #funcCallPrintln
    ;
actualParameters
    : LPAREN expression? (COMMA expression)* RPAREN
    ;
ifStatement
    : WETHER condition block OTHERWISE block  #ifelse
    | WETHER condition block #ifonly
    ;
condition
    : LPAREN expression RPAREN  
    ;
whileStatement
    : UNTIL condition block
    ;
doWhileStatement
    : EXECUTE block UNTIL condition SEMI
    ;
forStatement
    : AS LPAREN assignmentStatement SEMI expression SEMI assignmentStatement RPAREN block
    ;
returnStatement
    : REPLY expression SEMI
    ;
expression
    : '!' expression #bangExpr
    | '(' expression ')'  #parensExpr
    | expression ('*'|'/'|'%') expression #multdivmodExpr
    | expression ('+'|'-') expression   #addminusExpr
    | expression ('>='|'<='|'<'|'>') expression  #relationalExpr
    | expression ('=='|'!=') expression               #equalityExpr
    | expression '&&' expression               #andExpr
    | expression '||' expression                #orExpr
    | IntegerLiteral #intExpr
    | BooleanLiteral #boolExpr
    | FloatingPointLiteral #floatExpr
    | StringLiteral #stringExpr
    | CharacterLiteral #charExpr
    | ID actualParameters #funcCallExpr
    | ID         #idExpr
    | NULL  #nullExpr
    ;
grammar语法;
@标题{
包装发电机;
}
程序
:constantStatement*main函数声明*EOF
;
不变陈述
:绝对数据类型ID赋值表达式
;
可变声明
:数据类型ID
;
数据库类型
:INT
|浮动
|煤焦
|串
|布尔值
;
功能声明
:returnType ID参数列表块
;
返回型
:数据类型#返回数据类型
|黑暗#返回黑暗
;
参数表
:LPAREN参数?(逗号参数)*RPAREN
;
参数
:数据类型ID
;
块
:LBRACE语句*RBRACE
;
主要功能
:黑暗矿井参数列表块
;
陈述
:block#block语句
|变量声明半#varDec
|赋值语句半赋值
|functionCallStatement半函数调用
|如果声明#如果
|whileStatement#while
|doWhileStatement#dowhile
|声明
|返回语句#返回
|半阻风门
;
转让声明
:ID分配表达式
;
函数调用语句
:ID实际参数#functcallid
|打印实际参数#funcCallPrint
|PRINTLN实际参数#funcCallPrintln
;
实际参数
:LPAREN表达式?(逗号表达式)*RPAREN
;
国际单项体育联合会声明
:条件块是否为其他块#如果为其他块
|是否存在条件块#仅适用于
;
条件
:LPAREN表达式RPAREN
;
whileStatement
:直到条件块
;
家庭财产
:执行块,直到条件满足
;
预告
:AS LPAREN assignmentStatement半表达式半assignmentStatement RPAREN块
;
返回声明
:回复
;
表达
: '!' 表达式#bangExpr
|“(“表达式”)”#parensExpr
|表达式(“*”|“/”|“%”)表达式#multdivmodExpr
|表达式(“+”|“-”)表达式#addminusExpr

|表情(“>=”|“使用监听器接口评估IF语句是很困难的,就像监听器接口一样,您是由Antlr tree walker驱动的,它以所有者的顺序遍历AST树。但是,为了评估IF语句,您需要根据条件语句的评估结果


我认为访客界面更适合评估IF、for/While和函数调用。

欢迎使用SO。请为您的问题提供上下文或代码示例,如果没有这些示例,您不清楚自己在问什么。
public Main(String code, String[] args,JTextArea ta)throws Exception {
        this.ta =ta;
        String inputFile = "src/sample_code.cc";
        InputStream is=null;
        if ( inputFile!=null ) {
            is = new FileInputStream(inputFile);
        }
        //is = new StringBufferInputStream(code);
        ANTLRInputStream input = new ANTLRInputStream(is);
        CodeCraftGrammarLexer lexer = new CodeCraftGrammarLexer(input);
        CodeCraftGrammarParser parser = new CodeCraftGrammarParser(new CommonTokenStream(lexer));
        parser.setBuildParseTree(true);

        ParseTree tree = parser.program();
        List<String> ruleNames = Arrays.asList(parser.getRuleNames());
        TreeViewer tv = new TreeViewer(ruleNames, tree);

        ParseTreeWalker walker = new ParseTreeWalker();

        FirstPass def = new FirstPass();
        walker.walk(def, tree);
        // create next phase and feed symbol table info from define to reference phase
        SecondPass ref = new SecondPass(def.globals, def.scopes);
        walker.walk(ref, tree);
    }
public class FirstPass extends CodeCraftGrammarBaseListener{
    ParseTreeProperty<Scope> scopes = new ParseTreeProperty<Scope>();
    GlobalScope globals;
    Scope currentScope; // define symbols in this scope
    void saveScope(ParserRuleContext ctx, Scope s) { scopes.put(ctx, s); }

    public FirstPass(){
    }
    public void enterProgram(@NotNull ProgramContext ctx) {
        globals = new GlobalScope(null);
        currentScope = globals;
    }

    public void exitProgram(@NotNull ProgramContext ctx) {
        Main.displayScope(globals);
    }

    public void enterFunctionDeclaration(@NotNull FunctionDeclarationContext ctx) {
        String name = ctx.ID().getText();
        int typeTokenType = ctx.returnType().start.getType(); //UNSURE
        Symbol.Type type = Main.getType(typeTokenType);
        // push new scope by making new one that points to enclosing scope
        FunctionSymbol function = new FunctionSymbol(name, type, currentScope);

        GlobalScope temp = (GlobalScope) currentScope;
        if(temp.symbols.containsKey(function.name)){
            //multiple function declaration in same global scope
            Main.error(ctx.ID().getSymbol(), "Function already declared: "+name);
        }//UNSURE should i let it continue saving in symbol table
        currentScope.define(function); // Define function in current scope
        saveScope(ctx, function); // Push: set function's parent to current
        currentScope = function; // Current scope is now function scope
        if(currentScope.getClass()==FunctionSymbol.class)
            currentScope = (FunctionSymbol)currentScope;
    }

    public void enterMainFunction(@NotNull MainFunctionContext ctx) {
        String name = ctx.MINE().getText();
        int typeTokenType = ctx.DARKNESS().getSymbol().getType(); //UNSURE
        Symbol.Type type = Main.getType(typeTokenType);
        // push new scope by making new one that points to enclosing scope
        FunctionSymbol function = new FunctionSymbol(name, type, currentScope);

        GlobalScope temp = (GlobalScope) currentScope;
        if(temp.symbols.containsKey(function.name)){
            //multiple function declaration in same global scope
            Main.error(ctx.MINE().getSymbol(), "Main already declared: "+name);
        }//UNSURE should i let it continue saving in symbol table
        currentScope.define(function); // Define function in current scope
        saveScope(ctx, function); // Push: set function's parent to current
        currentScope = function; // Current scope is now function scope
        if(currentScope.getClass()==FunctionSymbol.class)
            currentScope = (FunctionSymbol)currentScope;
    }

    public void exitFunctionDeclaration(@NotNull FunctionDeclarationContext ctx) {
        Main.displayScope(currentScope);
        currentScope = currentScope.getEnclosingScope(); // pop scope
        if(currentScope.getClass()==GlobalScope.class)
            currentScope = (GlobalScope)currentScope;
    }

    public void exitMainFunction(@NotNull MainFunctionContext ctx) {
        Main.displayScope(currentScope);
        currentScope = currentScope.getEnclosingScope(); // pop scope
        if(currentScope.getClass()==GlobalScope.class)
            currentScope = (GlobalScope)currentScope;
    }

    public void enterBlock(CodeCraftGrammarParser.BlockContext ctx) {
        // push new local scope
        currentScope = new LocalScope(currentScope);
        currentScope = (LocalScope)currentScope;
        saveScope(ctx, currentScope);
    }

    public void exitBlock(CodeCraftGrammarParser.BlockContext ctx) {
        Main.displayScope(currentScope);
        currentScope = currentScope.getEnclosingScope(); // pop scope
        if(currentScope.getClass()==FunctionSymbol.class)
            currentScope = (FunctionSymbol)currentScope;
    }

    public void exitParameter(@NotNull ParameterContext ctx) {
        FunctionSymbol temp = (FunctionSymbol) currentScope;
        String name = ctx.ID().getText();
        if(temp.arguments.containsKey(name)){
            //multiple function declaration in same global scope
            Main.error(ctx.ID().getSymbol(), "Parameter already declared: "+name);
        }
        defineVar(ctx.dataType(), ctx.ID().getSymbol(),false);
    }

    public void exitVariableDeclaration(@NotNull VariableDeclarationContext ctx) {
        LocalScope temp = (LocalScope) currentScope;
        String name = ctx.ID().getText();
        if(temp.symbols.containsKey(name)){
            //multiple function declaration in same global scope
            Main.error(ctx.ID().getSymbol(), "Variable already declared: "+name);
        }
        defineVar(ctx.dataType(), ctx.ID().getSymbol(),false);
    }

    public void exitConstantStatement(@NotNull ConstantStatementContext ctx) {
        GlobalScope temp = (GlobalScope) currentScope;
        ConstantStatementContext ctx2 = ctx;

        String name = ctx2.ID().getText();
        if(temp.symbols.containsKey(name)){
            //multiple function declaration in same global scope
            Main.error(ctx2.ID().getSymbol(), "Constant already declared: "+name);
        }
        defineVar(ctx2.dataType(), ctx2.ID().getSymbol(),true);
    }

    void defineVar(CodeCraftGrammarParser.DataTypeContext typeCtx, Token nameToken, Boolean isConstant) {
        int typeTokenType = typeCtx.start.getType(); //UNSURE
        Symbol.Type type = Main.getType(typeTokenType);
        VariableSymbol var = new VariableSymbol(nameToken.getText(), type, isConstant);
        currentScope.define(var); // Define symbol in current scope
    }


}
public class SecondPass extends CodeCraftGrammarBaseListener{
    ParseTreeProperty<Scope> scopes;
    GlobalScope globals;
    Scope currentScope; // resolve symbols starting in this scope
    JTextArea ta;
    Stack<Symbol> stack = new Stack<Symbol>();
    Symbol op1,op2,ans;

    public SecondPass(GlobalScope globals, ParseTreeProperty<Scope> scopes) {
        this.scopes = scopes;
        this.globals = globals;
    }
    public void enterProgram(@NotNull ProgramContext ctx) {
        currentScope = globals;
    }

    public void enterFunctionDeclaration(@NotNull FunctionDeclarationContext ctx) {
        currentScope = scopes.get(ctx);
    }
    public void exitFunctionDeclaration(@NotNull FunctionDeclarationContext ctx) {
        currentScope = currentScope.getEnclosingScope();
    }
    public void enterBlock(@NotNull BlockContext ctx) {
        currentScope = scopes.get(ctx);
    }
    public void exitBlock(@NotNull BlockContext ctx) {
        currentScope = currentScope.getEnclosingScope();
    }
    public void exitIf(@NotNull IfContext ctx) {

    }
    public void exitFuncCallID(@NotNull FuncCallIDContext ctx) {
        String funcName = ctx.ID().getText();
        Symbol meth = currentScope.resolve(funcName);
        if ( meth==null ) {
            Main.error(ctx.ID().getSymbol(), "no such function: "+funcName);
        }
        if ( meth instanceof VariableSymbol ) {
            Main.error(ctx.ID().getSymbol(), funcName+" is not a function");
        }
    }

    public void exitAssignmentStatement(AssignmentStatementContext ctx) {
        String name = ctx.ID().getSymbol().getText();
        Symbol var = currentScope.resolve(name);
        if ( var==null ) {
            Main.error(ctx.ID().getSymbol(), "no such variable: "+name);
        }
        if ( var instanceof FunctionSymbol ) {
            Main.error(ctx.ID().getSymbol(), name+" is not a variable");
        }

        Symbol x = stack.pop();
        System.out.println("==\n::assign value"+x.value + " assign type:" + x.type.toString());
        if(var.type == x.type){


            currentScope.resolve(name).value = x.value;
            System.out.println("assignment name:"+name +" value: "+currentScope.resolve(name).value+" type:"+currentScope.resolve(name).type+"===\n");
        }else if (var.type == Type.tFLOAT && x.type== Type.tINT){
            currentScope.resolve(name).value = x.asFloat();
            System.out.println("assignment name:"+name +" value: "+currentScope.resolve(name).value+" type:"+currentScope.resolve(name).type+"===\n");
        }else{
        }
    }
    public void exitFuncCallExpr(@NotNull FuncCallExprContext ctx) {
        String funcName = ctx.ID().getText();
        Symbol meth = currentScope.resolve(funcName);
        if ( meth==null ) {
            Main.error(ctx.ID().getSymbol(), "no such function: "+funcName);
        }
        if ( meth instanceof VariableSymbol ) {
            Main.error(ctx.ID().getSymbol(), funcName+" is not a function");
        }
        //#WHAT THE HECK 
    }
    public void exitBangExpr(@NotNull BangExprContext ctx) {
        Symbol op1 = stack.pop();
        Symbol ans=null;
        String operator = ctx.getChild(0).getText();
        if(op1.isBoolean()){
            if (operator.equals("!")){
                ans = new Symbol(Type.tBOOLEAN, !op1.asBoolean());
            }
            stack.push(ans);
        }else{
            TerminalNode tn = (TerminalNode) ctx.getChild(1);
            String msg = "The operator " + operator +" is undefined for the argument type(s)" + op1;
            Main.semanticError(tn.getSymbol(),msg);
        }
    }
    public void exitOrExpr(@NotNull OrExprContext ctx) {
        Symbol op1 = stack.pop();
        Symbol op2 = stack.pop();
        Symbol ans=null;
        String operator = ctx.getChild(1).getText();
        if(op1.isBoolean()&&op2.isBoolean()){
            if (operator.equals("||")){
                ans = new Symbol(Type.tBOOLEAN, op2.asBoolean() || op1.asBoolean());
            }
            stack.push(ans);
        }else{
            TerminalNode tn = (TerminalNode) ctx.getChild(1);
            String msg = "The operator " + operator +" is undefined for the argument type(s)" + op2 +", "+ op1;
            Main.semanticError(tn.getSymbol(),msg);
        }
    }
    public void exitAndExpr(@NotNull AndExprContext ctx) {
        Symbol op1 = stack.pop();
        Symbol op2 = stack.pop();
        Symbol ans=null;
        String operator = ctx.getChild(1).getText();
        if(op1.isBoolean()&&op2.isBoolean()){
            if (operator.equals("&&")){
                ans = new Symbol(Type.tBOOLEAN, op2.asBoolean() && op1.asBoolean());
            }
            stack.push(ans);
        }else{
            TerminalNode tn = (TerminalNode) ctx.getChild(1);
            String msg = "The operator " + operator +" is undefined for the argument type(s)" + op2 +", "+ op1;
            Main.semanticError(tn.getSymbol(),msg);
        }
    }
    public void exitEqualityExpr(@NotNull EqualityExprContext ctx) {
        Symbol op1 = stack.pop();
        Symbol op2 = stack.pop();
        Symbol ans=null;
        String operator = ctx.getChild(1).getText();
        if((op1.isFloat() || op1.isInt())&&(op2.isFloat() || op2.isInt())){
            if (operator.equals("==")){
                ans = new Symbol(Type.tBOOLEAN, op2.asFloat() == op1.asFloat());
            }else if (operator.equals("!=")){
                ans = new Symbol(Type.tBOOLEAN, op2.asFloat() != op1.asFloat());
            }
            stack.push(ans);
        }else if(op1.isBoolean()&&op2.isBoolean()){
            if (operator.equals("==")){
                ans = new Symbol(Type.tBOOLEAN, op2.asBoolean() == op1.asBoolean());
            }else if (operator.equals("!=")){
                ans = new Symbol(Type.tBOOLEAN, op2.asBoolean() != op1.asBoolean());
            }
            stack.push(ans);
        }else if(op1.isString()&&op2.isString()){
            if (operator.equals("==")){
                ans = new Symbol(Type.tBOOLEAN, op2.asString() == op1.asString());
            }else if (operator.equals("!=")){
                ans = new Symbol(Type.tBOOLEAN, op2.asString() != op1.asString());
            }
            stack.push(ans);
        }else if(op1.isChar()&&op2.isChar()){
            if (operator.equals("==")){
                ans = new Symbol(Type.tBOOLEAN, op2.asChar() == op1.asChar());
            }else if (operator.equals("!=")){
                ans = new Symbol(Type.tBOOLEAN, op2.asChar() != op1.asChar());
            }
            stack.push(ans);
        }else{
            //error expression type mismatch
            //The operator - is undefined for the argument type(s) java.lang.String, java.lang.String
            TerminalNode tn = (TerminalNode) ctx.getChild(1);
            String msg = "The operator " + operator +" is undefined for the argument type(s)" + op2 +", "+ op1;
            Main.semanticError(tn.getSymbol(),msg);
        }
    }
    public void exitRelationalExpr(@NotNull RelationalExprContext ctx) {
        Symbol op1 = stack.pop();
        Symbol op2 = stack.pop();
        Symbol ans=null;
        String operator = ctx.getChild(1).getText();
        if((op1.isFloat() || op1.isInt())&&(op2.isFloat() || op2.isInt())){
            if (operator.equals(">")){
                ans = new Symbol(Type.tBOOLEAN, op2.asFloat() > op1.asFloat());
            }else if (operator.equals("<")){
                ans = new Symbol(Type.tBOOLEAN, op2.asFloat() < op1.asFloat());
            }else if (operator.equals(">=")){
                ans = new Symbol(Type.tBOOLEAN, op2.asFloat() >= op1.asFloat());
            }else if (operator.equals("<=")){
                ans = new Symbol(Type.tBOOLEAN, op2.asFloat() <= op1.asFloat());
            }
            stack.push(ans);
        }else{
            //error expression type mismatch
            //The operator - is undefined for the argument type(s) java.lang.String, java.lang.String
            TerminalNode tn = (TerminalNode) ctx.getChild(1);
            String msg = "The operator " + operator +" is undefined for the argument type(s)" + op2 +", "+ op1;
            Main.semanticError(tn.getSymbol(),msg);
        }
    }
    public void exitMultdivmodExpr(MultdivmodExprContext ctx) {
        Symbol op1 = stack.pop();
        Symbol op2 = stack.pop();
        Symbol ans=null;
        String operator = ctx.getChild(1).getText();
        if((op1.isFloat() || op1.isInt())&&(op2.isFloat() || op2.isInt())){
            if(op1.asFloat()==0 && (operator.equals("/")||operator.equals("%"))){
                //error div/mod by zero
                TerminalNode tn = (TerminalNode) ctx.getChild(1);
                String msg = "Arithmetic Error: " + operator +" by zero";
                Main.semanticError(tn.getSymbol(),msg);
            }else {
                if(op1.isFloat()||op2.isFloat()){ //push as float
                    if (operator.equals("*")){
                        ans = new Symbol(Type.tFLOAT, op2.asFloat() * op1.asFloat());
                    }else if(operator.equals("/")){
                        ans = new Symbol(Type.tFLOAT, op2.asFloat() / op1.asFloat());
                    }else if(operator.equals("%")){
                        ans = new Symbol(Type.tFLOAT, op2.asFloat() % op1.asFloat());
                    }
                }else{ //push as int
                    if (operator.equals("*")){
                        ans = new Symbol(Type.tINT, op2.asInt() * op1.asInt());
                    }else if(operator.equals("/")){
                        ans = new Symbol(Type.tINT, op2.asInt() / op1.asInt());
                    }else if(operator.equals("%")){
                        ans = new Symbol(Type.tINT, op2.asInt() % op1.asInt());
                    }
                }
                stack.push(ans);
            }
        }else{
            //error expression type mismatch
            TerminalNode tn = (TerminalNode) ctx.getChild(1);
            String msg = "The operator " + operator +" is undefined for the argument type(s)" + op2 +", "+ op1;
            Main.semanticError(tn.getSymbol(),msg);
        }
    }

    public void exitAddminusExpr(AddminusExprContext ctx) {
        Symbol op1 = stack.pop();
        Symbol op2 = stack.pop();
        Symbol ans;
        String operator = ctx.getChild(1).getText();
        if((op1.isFloat() || op1.isInt())&&(op2.isFloat() || op2.isInt())){
            if(op1.isFloat()||op2.isFloat()){ //push as float

                if (operator.equals("-")){
                    ans = new Symbol(Type.tFLOAT, op2.asFloat() - op1.asFloat());
                }else{
                    ans = new Symbol(Type.tFLOAT, op2.asFloat() + op1.asFloat());
                }
            }else{ //push as int
                if (operator.equals("-")){  
                    ans = new Symbol(Type.tINT, op2.asInt() -  op1.asInt());
                }
                else{
                    ans = new Symbol(Type.tINT, op2.asInt() + op1.asInt() );
                }
            }
            stack.push(ans);
        }else if(op1.isString() && op2.isString() && operator.equals("+")){
            ans = new Symbol(Type.tSTRING, op2.asString() + op1.asString());
            stack.push(ans);
        }else{
            //error expression type mismatch
            //The operator - is undefined for the argument type(s) java.lang.String, java.lang.String
            TerminalNode tn = (TerminalNode) ctx.getChild(1);
            String msg = "The operator " + operator +" is undefined for the argument type(s)" + op2 +", "+ op1;
            Main.semanticError(tn.getSymbol(),msg);
        }
    }
    public void exitNullExpr(NullExprContext ctx) {
        Symbol s = new Symbol(Type.tNULL,null);
        stack.push(s);
    }
    public void exitIdExpr(@NotNull CodeCraftGrammarParser.IdExprContext ctx) {
        String name = ctx.ID().getSymbol().getText();
        Symbol var = currentScope.resolve(name);
        if ( var==null ) {
            Main.semanticError(ctx.ID().getSymbol(), "no such variable: "+name);
        }else if ( var instanceof FunctionSymbol ) {
            Main.semanticError(ctx.ID().getSymbol(), name+" is not a variable");
        }else{
            stack.push(var);
        }
    }
    public void exitIntExpr(IntExprContext ctx) {
        String valueInString = ctx.IntegerLiteral().getSymbol().getText();
        Integer valueInInteger = Integer.valueOf(valueInString);
        Symbol s = new Symbol(Type.tINT,valueInInteger);
        stack.push(s);
    }
    public void exitFloatExpr(FloatExprContext ctx) {
        String valueInString = ctx.FloatingPointLiteral().getSymbol().getText();
        Float valueInFloat = Float.valueOf(valueInString);
        Symbol s = new Symbol(Type.tFLOAT,valueInFloat);
        stack.push(s);
    }
    public void exitBoolExpr(BoolExprContext ctx) {
        String value = ctx.BooleanLiteral().getSymbol().getText();  
        Symbol s = null;
        if("true".equals(value)){
            s = new Symbol(Type.tBOOLEAN,true);
        }else if("false".equals(value)){
            s = new Symbol(Type.tBOOLEAN,false);
        }
        stack.push(s);
    }
    public void exitStringExpr(StringExprContext ctx) {
        String value = ctx.StringLiteral().getSymbol().getText();
        Symbol s;
        if(value.length()==2){ //String is blank
            s = new Symbol(Type.tSTRING,"");
        }else{
            s = new Symbol(Type.tSTRING,value.substring(1, value.length()-1));
        }
        stack.push(s);
    }
    public void exitCharExpr(CharExprContext ctx) {
        String value = ctx.CharacterLiteral().getSymbol().getText();
        Symbol s = new Symbol(Type.tCHAR,value.charAt(1));
        stack.push(s);
    }   
}