ANTLR:没有可行的替代错误' {" type"'

时间:2016-05-23 09:57:22

标签: javascript java antlr antlr4

我知道有很多这样的问题,我们已经完成了所有这些问题,但我们似乎无法找到符合我们需求的解决方案。

我们为javascript to Java转换器,词法分析器和解析器编写了一个简单的语法,我们无法正确使用输入文件的第一个令牌。

这是语法:

lexer grammar JS2JAVALexer;

STRING
   : '"' (ESC | ~ ["\\])* '"'
   ;
fragment ESC
   : '\\' (["\\/bfnrt] | UNICODE)
   ;
fragment UNICODE
   : 'u' HEX HEX HEX HEX
   ;
fragment HEX
   : [0-9a-fA-F]
   ;
NUMBER
   : '-'? INT '.' [0-9] + EXP? | '-'? INT EXP | '-'? INT
   ;
fragment INT
   : '0' | [1-9] [0-9]*
   ;
// no leading zeros
fragment EXP
   : [Ee] [+\-]? INT
   ;
// \- since - means "range" inside [...]
WS
   : [ \t\n\r] + -> skip
   ;

OPENPAR : '(' ;
CLOSEPAR : ')' ;
OPENBRACES : '{' ;
CLOSEBRACES : '}' ;
OPENBRACKETS : '[' ;
CLOSEBRACKETS : ']' ;
TWOPOINTS : ':' ;
QUOTATION_MARK : '"';
COMMA : ',' ;
TRUE : 'true';

FALSE : 'false';
NULL : 'null' ;

TYPE : 'type';
SOURCETYPE : '"sourceType"';
BODY : '"body"';

运行后我们得到错误"第2行:4在输入处没有可行的选择' {"类型"'

这是我们的输入文件:

{
    "type": "Program",
    "body": [
        {
            "type": "FunctionDeclaration",
            "id": {
                "type": "Identifier",
                "name": "name"
            },
            "params": [
                {
                    "type": "Identifier",
                    "name": "arg1"
                },
                {
                    "type": "Identifier",
                    "name": "arg2"
                }
            ],
            "defaults": [],
            "body": {
                "type": "BlockStatement",
                "body": [
                    {
                        "type": "VariableDeclaration",
                        "declarations": [
                            {
                                "type": "VariableDeclarator",
                                "id": {
                                    "type": "Identifier",
                                    "name": "x"
                                },
                                "init": {
                                    "type": "Literal",
                                    "value": 1,
                                    "raw": "1"
                                }
                            }
                        ],
                        "kind": "var"
                    },
                    {
                        "type": "VariableDeclaration",
                        "declarations": [
                            {
                                "type": "VariableDeclarator",
                                "id": {
                                    "type": "Identifier",
                                    "name": "y"
                                },
                                "init": {
                                    "type": "Literal",
                                    "value": 2,
                                    "raw": "2"
                                }
                            }
                        ],
                        "kind": "var"
                    },
                    {
                        "type": "ReturnStatement",
                        "argument": {
                            "type": "BinaryExpression",
                            "operator": "+",
                            "left": {
                                "type": "Identifier",
                                "name": "x"
                            },
                            "right": {
                                "type": "Identifier",
                                "name": "y"
                            }
                        }
                    }
                ]
            },
            "generator": false,
            "expression": false
        }
    ],
    "sourceType": "script"
}

Parser的代码:

parser grammar JS2JAVAParser;

options {
    tokenVocab = JS2JAVALexer;
}

json
   : object
   | array
   ;

object
   : OPENBRACES pair (COMMA pair)* CLOSEBRACES
   | OPENBRACES CLOSEBRACES
   ;

left_operand 
    : QUOTATION_MARK left_name QUOTATION_MARK
    ;

left_name 
    : TYPE 
    | BODY 
    | SOURCETYPE
    | DECLARATIONS
    | ID
    | INIT
    | OPERATOR
    | LEFT
    | RIGHT
    | VALUE
    | RAW
    | KIND
    ;

pair
   : left_operand TWOPOINTS value
   ;

array
   : OPENBRACKETS value (COMMA value)* CLOSEBRACKETS
   | OPENBRACKETS CLOSEBRACKETS
   ;

value
   : QUOTATION_MARK value_name QUOTATION_MARK
   | object
   | array
   | TRUE
   | FALSE
   | NULL
   | STRING
   | IDENTIFIER
   | LITERAL
   | VAR
   | STRING
   ;

value_name 
    : SCRIPT
    | PROGRAM
    ;

很抱歉这么久又重复的问题,但我们的想法已经不多了。提前感谢您的耐心等待。

1 个答案:

答案 0 :(得分:0)

问题在于,在您的顶级解析器规则中,“type”被识别为STRING令牌。实际上,似乎所有可能是STRING或其他东西都将被识别为STRING。所以基本上你需要解决词法分析器歧义。通过重写词法分析器规则,或者可能使用词法分析器模式。

至少作为参考,ANTLR的github repo上有this JSON grammar