space operator (Excel Intersection)

18 views
Skip to first unread message

Владислав Бабин

unread,
May 16, 2014, 7:50:48 AM5/16/14
to antlr-di...@googlegroups.com
Hi all

I am building Excel grammar and encountered the following problem: the grammar below works fine parsing expression "=123 + 456" (note spaces next to "+")

grammar Excel;
formula
 : scalarFormula
 | arrayFormula
 ;
...
additiveExpression
 : multiplicativeExpression
 | additiveExpression ('+'|'-') multiplicativeExpression
 ;
...
intersectRange
 : simpleRange
// | intersectRange ' '+ simpleRange
 ;
...
WHITESPACE : [ \t\n\r]+ -> skip ;

However, if I uncomment the line with intersection operator (which is space in Excel), so that intersectRange definition reads as follows:

intersectRange
 : simpleRange
 | intersectRange ' '+ simpleRange
 ;

I get an error:

line 1:4 extraneous input ' ' expecting {<EOF>, '%', '^', '+', '*', '-', '/', '=', '<', '<=', '>', '>=', NE, '&'}
line 1:6 no viable alternative at input ' '

Changing grammar to

additiveExpression
 : multiplicativeExpression
 | additiveExpression WHITESPACE* ('+'|'-') WHITESPACE* multiplicativeExpression
 ;
...
WHITESPACE : [ \t\n\r] ;

does not help a lot:

line 1:4 extraneous input ' ' expecting {<EOF>, '%', '^', '+', '*', '-', '/', '=', '<', '<=', '>', '>=', NE, '&', WHITESPACE}
line 1:6 extraneous input ' ' expecting {'-', '[', '{', '$', INTEGER, FLOAT, STRING, IDENTIFIER, ERROR, COLUMN, ROW, WHITESPACE, BOOL}

Does anybody know about how to overcome this error? I believe that the problem is that ANTLR starts to recognize space (" ") as a token rather than a white space but how do I override this? Thank you.




Mike Lischke

unread,
May 16, 2014, 8:18:29 AM5/16/14
to antlr-di...@googlegroups.com

Hi Vlad,

intersectRange
 : simpleRange
// | intersectRange ' '+ simpleRange
 ;

Try changing that to :

intersectRange:
simpleRange+
;

which will also resolve the left recursion. I assume here that the number of whitespaces between 2 simple ranges does not matter for the language.


Reply all
Reply to author
Forward
0 new messages