ANTLR访问方法未被调用

时间:2016-04-14 21:58:56

标签: java antlr antlr4

我正在使用Java中的antlr生成代码开发一个项目。当我创建一个解析树时,它会生成正确的树。但是当我将该树传递给visit方法时,visit方法返回null。

语法文件:

grammar Formula;

formula : expression+ EOF;

expression
    : ID '(' term (',' term)* ')'                               # doPredicate
    | '(' expression ')'                                        # doParenthesis
    | '~' expression                                            # doNot
    | expression '=>' expression                                # doThen
    | expression '<=>' expression                               # doIFF
    | expression '<=' expression                                # doIF
    | expression '?' expression ':' expression                  # doITE
    | expression '&' expression                                 # doAnd
    | expression '|' expression                                 # doOr
    | expression '^' expression                                 # doXor
    | 'forall' VAR 'in' (range_expr | set_expr)  expression     # doExpandAnd
    | 'exists' VAR 'in' (range_expr | set_expr)  expression     # doExpandOr
    | TRUE                                                      # newTrueBoolean
    | FALSE                                                     # newFalseBoolean
    | VAR                                                       # newVariable
    | ID                                                        # newIdentifier
    ;

term
    : ID                                              # newTermID
    | VAR                                             # newTermVar
    | int_expr                                        # doIntExpr
    ;

int_expr
    : '(' int_expr ')'                                # parenthesizedIntExpression
    | '|' int_expr '|'                                # absValueExpression
    | '-' int_expr                                    # unaryMinusExpression
    | int_expr op=( '*' | '/' | '%' ) int_expr        # multiplicativeExpression
    | int_expr op=( '+' | '-' ) int_expr              # additiveExpression
    | VAR                                             # newIntVariable
    | NUMBER                                          # newInteger
    ;

range_expr 
    : '{' (int_expr '..' int_expr) '}' ;

set_expr 
    : '{' term (',' term)*  '}' ;

// Tokens
TRUE            : 'True';
FALSE           : 'False';
ID              : LETTER ALPHANUM*;
VAR             : '_' LETTER ALPHANUM*;
NUMBER          : DIGIT+;

// Drops whitespaces and comments
WS              : [ \t\n\r]+ -> skip ;
COMMENTS        : ('/*' .*? '*/' | '//' ~'\n'* '\n' ) -> skip;

// Fragments
fragment DIGIT    : '0' .. '9';
fragment UPPER    : 'A' .. 'Z';
fragment LOWER    : 'a' .. 'z';
fragment LETTER   : LOWER | UPPER;
fragment WORD     : LETTER | '_' | '$' | '#' | '.';
fragment ALPHANUM : WORD | DIGIT;

FormulaTreeVisitor.java

公共类FormulaTreeVisitor扩展了FormulaBaseVisitor {

@Override
public String visitFormula(FormulaParser.FormulaContext ctx) {
    // TODO Auto-generated method stub
    System.out.println("Hi");
    super.visitFormula(ctx);
    System.out.println("Hi");
    return "Hi";
}   

}

Test.java

公共课测试{

/**
 * @param args
 */
public static void main(String[] args) {
    ANTLRInputStream in = new ANTLRInputStream("a|b");
    FormulaLexer lexer = new FormulaLexer(in);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    FormulaParser parser = new FormulaParser(tokens);

    ParseTree tree = parser.formula(); 
    System.out.println(tree.toStringTree(parser));
    FormulaTreeVisitor eval = new FormulaTreeVisitor();
    String f = eval.visit(tree);
    System.out.println(f);

    //show AST in GUI
    JFrame frame = new JFrame("Antlr AST");
    JPanel panel = new JPanel();
    TreeViewer viewr = new TreeViewer(Arrays.asList(
            parser.getRuleNames()),tree);
    viewr.setScale(1.5);//scale a little
    panel.add(viewr);
    frame.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200,200);
    frame.setVisible(true);
}

}

在控制台中输出

(公式(表达式(表达式a)|(表达式b))) 空

从不调用FormulaTreeVisitor类中的visitFormula方法。此外,eval.visit(树)返回null。有人可以帮助我如何调用visitFormula方法,以便我可以实现我的逻辑。

0 个答案:

没有答案