0

I'm trying to parse with ANTLR 4 a line like this:

circle 'my circle' : posx = '800'; posy = '640';

I want to able to parse it without taking care of spaces, tabs and new line in it, eg:

circle'my circle':posx='800';posy='640';

or

circle
  'my circle':
 posx='800';  posy=
'640'

My grammar at the moment is:

grammar Circle;

prog
    : statement*
    ;

statement
    : circle
    ;

circle
    : INDENT? 'circle' '\'' VALUES '\'' ':' params
    ;

params
    : param+
    ;

param
    : ARG '=' '\'' VALUES '\'' ';'
    ;

INDENT : [ \t]+;
VALUES : ARG (ARG)* ;
ARG : [a-zA-Z0-9]+;
WS : [ \t\n\r]+ -> skip;

Anyway trying to parse this:

circle 'my circle' : posx='800'; posy = '640';

I got:

line 1:5 mismatched input ' ' expecting '''

Any idea about how to fix the grammar to parse the line text above skipping spaces, tabs, returns in the middle of the line?

Randomize
  • 8,651
  • 18
  • 78
  • 133

1 Answers1

0

That's because in your parser rules you ask for WS. Like in circle INDENT? 'circle' WS '\'' VALUES '\'' WS ':' params and then in lexer rules you skip all WS [ \t\n\r]+ -> skip;

line 1:5 mismatched input ' ' expecting '''

You get this error because in ARG you can have WS so you need to add ' ' in lexer ARG rule. I don't know ANTLR 4 but in ANTLR 3 it should look like this ARG : ( 'a' .. 'z' | 'A' .. 'Z' | '_' | '0' .. '9'|' ' )+

exemplum
  • 115
  • 9
  • yes that's true. The fact I am trying to get a python-like indentation http://stackoverflow.com/questions/8642154/antlr-what-is-simpliest-way-to-realize-python-like-indent-depending-grammar but without success for the moment. – Randomize Sep 29 '13 at 13:02