[antlr-interest] how to skip/read next n Characters (n is read form input)

Thomas Ruschival thomas at ruschival.de
Thu Nov 15 12:17:14 PST 2012


Hi Claus-Dieter,
I am running antlr v3 with anltworks 1.4.3 and java 1.7.0_1.

It seems the problem has 2 aspects.
1st) no list object is created, only declared (List b_list = null;)
2nd) Nothing matches, not (b+=CHAR)+, not (b+=CHAR|DIGIT|'E')+ not even
not (b+=.)+

What really surprised me (remember I am still newbie) is that ANTLR
generates Lexer tokens for characters form parser rules and that these
tokens do no longer match the generic CHAR lexer rule.
For instance the 'C' or 'E' from the grammar below do not match if I
replace (val=.) by (val=CHAR) in the rule print.

Best Regards
Thomas


I stripped down my grammar to share:

grammar JPosEscape;
@members{
    private boolean escBool = false;
}

DIGIT :
    ('0'..'9');

CHAR :
    ('\u0000'..'\u7fff');

escapecmd :
    ( bold
     | underline
     | readNchars
    );


bold :
    '!'?
         {
          escBool = false;
         }
    ('b' 'C')
              {
               System.out.println("Selecting Bold:" + escBool);
              };

underline :
    ullines = (DIGIT)*
    '!'?
         {
          escBool = false;
         }
    ('u' 'C')
              {
               System.out.println("Selecting UNDERLINE:" + escBool +
"using # "+ $ullines.text + "lines");
              };

readNchars :
    NUMBER = DIGIT+ 'E'
    (
        b +=  ( CHAR|DIGIT|'E' )
    )+
    {$b.size() <= Integer.parseInt($NUMBER.text)}?
                                                   {
                                                    System.out.println("
readNChars #" + $b.size());
                                                   };

expr :
    print*;

print :
    ('@' '|') => ('@' '|')
     escapecmd
    | (val = . )   
     {
      System.out.print($val.text);
     };


 


Am 15.11.2012 04:46, schrieb cd.barth at t-online.de:
> Hi Thomas, 
> the sample from ANTLR v3 reference is not (yet?) running with ANTLR v4.0b3.
> (b+=CHAR)+ {$b.size()<=Integer.parseInt($NUM.text)}?
>
> I see that {System.out.println(((StartContext)_localctx).b.size());}
> is a workaround for the counter but synpred is not working for me too.
>
> It would be nice getting therefor a solution from Ter in the final Version.
>
> Regards Claus-Dieter


More information about the antlr-interest mailing list