Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot create implicit token for string literal in non-combined grammar

so found a nice grammar for a calculator and copied it with some lil changes from here: https://dexvis.wordpress.com/2012/11/22/a-tale-of-two-grammars/

I have two Files: Parser and Lexer. Looks like this:

    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;    

and the Lexer:

    lexer grammar WRBLexer;

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

Basically just splitted Lexer and Parser into two files. But when i try to save i get some Errors:

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)

Hope someone can help me with this.

Best regards

like image 614
FelRPI Avatar asked Oct 30 '14 09:10

FelRPI


1 Answers

All literal tokens inside your parser grammar: '*', '/', etc. need to be defined in your lexer grammar:

lexer grammar WRBLexer;

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

And then in your parser grammar, you'd do:

expression
    : ...
    | expression (MUL|DIV) expression   # multOrDiv
    | expression (ADD|SUB) expression   # addOrSubtract
    | ...
    ;
like image 190
Bart Kiers Avatar answered Oct 04 '22 16:10

Bart Kiers