I know there are a lot of this questions and we've been going through them all but we can't seem to find a solution that fits our needs.
我知道有很多这样的问题,我们一直在经历这些问题,但我们似乎找不到符合我们需求的解决方案。
We wrote a simple grammar for a javascript to Java converter, the lexer and the parser and we can't get it to consume the first token of our input file correctly.
我们为javascript to Java转换器,词法分析器和解析器编写了一个简单的语法,我们无法正确使用输入文件的第一个令牌。
Here's the grammar:
这是语法:
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"';
After running it we get the error "line 2:4 no viable alternative at input '{"type"'
运行之后,我们得到错误“第2行:4在输入'{”类型“'没有可行的替代方案
This is our input file:
这是我们的输入文件:
{
"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's code:
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
;
Sorry for such a long and repetitive question but we're running out of ideas. Thank you in advance for your patience guys.
很抱歉这么久又重复的问题,但我们的想法已经不多了。提前感谢您的耐心等待。
1 个解决方案
#1
0
The problem is that in your top level parser rule "type" is recognized as STRING token. Actually, seems that everything that could be STRING or something else will be recognized as STRING. So basically you need to resolve the lexer ambiguity. By rewriting lexer rules, or potentially using lexer modes.
问题是在您的*解析器规则中,“type”被识别为STRING令牌。实际上,似乎所有可能是STRING或其他东西都将被识别为STRING。所以基本上你需要解决词法分析器歧义。通过重写词法分析器规则,或者可能使用词法分析器模式。
Also at least as a reference, there is this JSON grammar on ANTLR's github repo.
至少作为参考,在ANTLR的github repo上有这个JSON语法。
#1
0
The problem is that in your top level parser rule "type" is recognized as STRING token. Actually, seems that everything that could be STRING or something else will be recognized as STRING. So basically you need to resolve the lexer ambiguity. By rewriting lexer rules, or potentially using lexer modes.
问题是在您的*解析器规则中,“type”被识别为STRING令牌。实际上,似乎所有可能是STRING或其他东西都将被识别为STRING。所以基本上你需要解决词法分析器歧义。通过重写词法分析器规则,或者可能使用词法分析器模式。
Also at least as a reference, there is this JSON grammar on ANTLR's github repo.
至少作为参考,在ANTLR的github repo上有这个JSON语法。