使用antlr4和java解析器解析objective-c代码时出错,但与grun一起使用

时间:2018-04-09 20:52:34

标签: antlr4

我正在使用一步语法here当我运行grun时效果很好:

grun ObjectiveC translationUnit ~/Desktop/test.h -gui

但是,使用生成的java解析器运行以下代码时:

        try {
            ANTLRInputStream inputStream = new ANTLRInputStream(
                    "@interface TestInterface \r @property(nonatomic, assign) BOOL testProp; \r @end");

            ObjectiveCLexer lexer = new ObjectiveCLexer(inputStream);
            CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
            ObjectiveCParser parser = new ObjectiveCParser(commonTokenStream);
            parser.setBuildParseTree(true);
            ParseTree tree = parser.expression();

            ObjectiveCParserBaseVisitor visitor = new ObjectiveCParserBaseVisitor();
            visitor.visit(tree);
        }
        catch(Exception ex){
            System.out.print(ex);
        }

我收到此错误:

line 1:0 extraneous input '@interface' expecting {'sizeof', 'true', 'false', 'BOOL', 'Class', 'bycopy', 'byref', 'id', 'IMP', 'in', 'inout', 'nil', 'NO', 'NULL', 'oneway', 'out', 'Protocol', 'SEL', 'self', 'super', 'YES', '@encode', '@protocol', '@selector', 'atomic', 'nonatomic', 'retain', '__autoreleasing', '__block', '__bridge_retained', '__bridge_transfer', '__covariant', '__contravariant', '__deprecated', '__kindof', '__unused', NULL_UNSPECIFIED, NULLABLE, NONNULL, 'null_resettable', 'NS_INLINE', 'NS_ENUM', 'NS_OPTIONS', 'assign', 'copy', 'getter', 'setter', 'strong', 'readonly', 'readwrite', 'weak', 'unsafe_unretained', 'IBOutlet', 'IBOutletCollection', 'IBInspectable', 'IB_DESIGNABLE', IDENTIFIER, LP, '[', '@', BANG, '~', '++', '--', '+', '-', '*', '&', '^', CHARACTER_LITERAL, STRING_START, HEX_LITERAL, OCTAL_LITERAL, BINARY_LITERAL, DECIMAL_LITERAL, FLOATING_POINT_LITERAL}

1 个答案:

答案 0 :(得分:1)

您正在使用解析器规则translationUnit运行grun,但在您的代码中使用expression。这样做:

ParseTree tree = parser.translationUnit();