Question

I'm writing my grammar but I have problem to allowing blank spaces. My grammar work if I write 'hello' + 'world' but if I try to compile operations like the following I have problems:

'hello' + ' world'

This are my grammar files:

grammar Common;

identifierNum: hostId DOT observableId DOT method ':Num';
identifierString: hostId DOT observableId DOT method  ':String';


hostId: ID;
observableId: ID;
method: ID'('')';

MUL: '*';
DIV: '/'; 
ADD: '+';
SUB: '-';
DOT: '.';
MIN: '<';
MAX: '>';


ID: [a-zA-Z] [a-zA-Z0-9]*;
STRING: '\''[a-zA-Z0-9]+ '\'';
DIGIT: [0-9]+ ;
DOUBLE: [0-9]* DOT [0-9]+ ;


WS: [ \n\t]+ -> skip;

and this

grammar Expression;
import Common;

expression:     stringExpr                      # StringExpression
    |           numExpr                         # NumExpression
    ;     

stringExpr:     stringExpr ADD stringExpr   # Concat
    |           STRING                      # BaseStr 
    |           identifierString            # IdString     
    |           '(' stringExpr ')'          # ParensString
    ;

numExpr:    numExpr op=(MUL|DIV) numExpr                # MulDiv
    |       numExpr op=(ADD|SUB) numExpr                # AddSub
    |       DIGIT                                       # Int
    |       DOUBLE                                      # Double
    |       identifierNum                               # IdNum      
    |       '(' numExpr ')'                             # ParensNum 
    ;

Thanks for your help!

Was it helpful?

Solution

Your have not allowed a blank being part of the STRING token, so it will be filtered out as WS before this token is resolved. You can alter it like that:

STRING: '\''[ a-zA-Z0-9]+ '\'';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top