skip parser behavior change

Adding skipto the rule does not do what I expect. Here's the grammar for a pair of tokens, separated by a comma and a space. I made one version where the comma is marked skipand the other where it is missing:

grammar Commas;

COMMA:          ', ';
COMMASKIP:      ', ' -> skip;
DATA:           ~[, \n]+;

withoutSkip:    data COMMA data '\n';
withSkip:       data COMMASKIP data '\n';
data:           DATA;

Testing the rule without skipworks as expected:

$ echo 'a, b' | grun Commas withoutSkip -tree
(withoutSkip (data a) ,  (data b) \n)

With skipan error message appears:

$ echo 'a, b' | grun Commas withSkip -tree
line 1:1 mismatched input ', ' expecting COMMASKIP
(withSkip (data a) ,  b \n)

If I comment on the rules COMMAand withoutSkip, I get the following:

$ echo 'a, b' | grun Commas withSkip -tree
line 1:3 missing ', ' at 'b'
(withSkip (data a) <missing ', '> (data b) \n)

I am trying to get output that has only decimal data tokens, like this:

(withSkip (data a) (data b) \n)

What am I doing wrong?

+3
source share
1 answer

skip . , skip ped lexer .

, , , , "" (), , , , , "". COMMASKIP , COMMA .

- :

;

COMMA : ',' -> skip;
SPACE : (' '|'\n') -> skip;
DATA  : ~[, \n]+;

data  : DATA+;

, , , ? ,, b.

, (.. a,,b) , .

, antlr3 .

ANTLR 4 AST . / . / . , , Q & A: , ANTLR v4?

:

grammar X;

COMMA : ',';
SPACE : (' '|'\n') -> skip;
DATA  : ~[, \n]+;

data  : DATA (COMMA DATA)*;

:

public class MyListener extends XBaseListener {

    @Override
    public void enterData(XParser.DataContext ctx) {

        List dataList = ctx.DATA(); // not sure what type of list it returns...
        // do something with `dataList`
    }
}

, COMMA , enterData(...) DATA.

+4

All Articles