token - 无法为非组合语法中的字符串文字创建隐式标记

标签 token grammar antlr4 implicit

因此找到了一个很好的计算器语法,并从此处复制了一些小变化:
https://dexvis.wordpress.com/2012/11/22/a-tale-of-two-grammars/

我有两个文件:Parser和Lexer。看起来像这样:

    parser grammar Parser;

options{
    language = Java;
    tokenVocab = Lexer;
}

// PARSER
program : ((assignment|expression) ';')+;

assignment : ID '=' expression;

expression
    : '(' expression ')'                # parenExpression
    | expression ('*'|'/') expression   # multOrDiv
    | expression ('+'|'-') expression   # addOrSubtract
    | 'print' arg (',' arg)*            # print
    | STRING                            # string
    | ID                                # identifier
    | INT                               # integer;

arg : ID|STRING;    

和Lexer:
    lexer grammar WRBLexer;

STRING : '"' (' '..'~')* '"';
ID     : ('a'..'z'|'A'..'Z')+;
INT    : '0'..'9'+;
WS     : [ \t\n\r]+ -> skip ;

基本上只是将Lexer和Parser分为两个文件。
但是,当我尝试保存时,出现一些错误:
error(126): Parser.g4:9:35: cannot create implicit token for string literal in non-combined grammar: ';'
error(126): Parser.g4:11:16: cannot create implicit token for string literal in non-combined grammar: '='
error(126): Parser.g4:2:13: cannot create implicit token for string literal in non-combined grammar: '('
error(126): Parser.g4:2:28: cannot create implicit token for string literal in non-combined grammar: ')'
error(126): Parser.g4:3:10: cannot create implicit token for string literal in non-combined grammar: 'print'
error(126): Parser.g4:3:23: cannot create implicit token for string literal in non-combined grammar: ','
error(126): Parser.g4:9:37: cannot create implicit token for string literal in non-combined grammar: '*'
error(126): Parser.g4:9:41: cannot create implicit token for string literal in non-combined grammar: '/'
error(126): Parser.g4:10:47: cannot create implicit token for string literal in non-combined grammar: '+'
error(126): Parser.g4:10:51: cannot create implicit token for string literal in non-combined grammar: '-'
10 error(s)

希望有人可以帮助我。

此致

最佳答案

解析器语法中的所有文字标记:'*''/'等都需要在词法分析器语法中定义:

lexer grammar WRBLexer;

ADD : '+';
MUL : '*';
...

然后在解析器语法中,您将执行以下操作:
expression
    : ...
    | expression (MUL|DIV) expression   # multOrDiv
    | expression (ADD|SUB) expression   # addOrSubtract
    | ...
    ;

关于token - 无法为非组合语法中的字符串文字创建隐式标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26649363/

相关文章:

php - Drupal token 使用示例

c# - JwtSecurityToken 与 SecurityTokenDescriptor 中的日期不同

c - 构建解释器 : designing an AST

python - 准确解析antlr4中的n个参数

java - 如何检测用户是否输入了不符合我的ANTLR语法规则的字符串?

java - 运行 AntLR 4 语法生成的方法

java - HttpClient - 如何设置指纹和_csrf?

android - 使用 Volley 进行异步帐户身份验证

grammar - 这种语言的正确语法是什么?

java - 我应该如何限制 ANTLR 中 ID token 的长度?