[antlr-interest] specialStateTransition() in Java-generated lexer

Kevin J. Cummings cummings at kjchome.homeip.net
Thu Jul 23 14:22:58 PDT 2009


On 07/23/2009 04:53 PM, Bart J. Geraci wrote:
> When ANTLR generates my lexer, I get this method:
>
> ***********
>
> @Override
> public int specialStateTransition(int s, IntStream _input)
>     throws NoViableAltException {
>     IntStream input = _input;
>     int _s = s;
>     switch (s) {
>       case 0:
>       int LA26_16 = input.LA(1);
>       s = -1;
>       if ((LA26_16 == '\"')) {
>             s = 77;
>       } else if (((LA26_16>= '\u0000'&&  LA26_16<= '\t')
>           || (LA26_16>= '\u000B'&&  LA26_16<= '\f')
>           || (LA26_16>= '\u000E'&&  LA26_16<= '!')
>           || (LA26_16>= '#'&&  LA26_16<= '\u2027')
>           || (LA26_16>= '\u202A'&&  LA26_16<= '\uFFFF'))) {
>         s = 36;
>       }
>
>       if (s>= 0)
>         return s;
>       break;
>     }
>     NoViableAltException nvae =
>       new NoViableAltException(getDescription(), 26, _s, input);
>       error(nvae);
>       throw nvae;
> }
>
> ***********
> What strikes me is that the method argument "s" is modified and
> returned. There is a similarly named local variable "_s" that is
> assigned, but is not modified.
>
> Is this correct or should some of these "s" be "_s" ?

Bart,
	The only usage of "s" after the "_s = s" assignment, are assignments to 
"s" itself, which reflect a value depending on the if statements inside 
the switch.  If you assigned a positive value to s, return it. 
Otherwise (I assume s is still -1) you break out of the switch 
statement.  In which case (pun intended), since you are about to throw 
an exception, it contains the saved input value _s.  So, the code looks 
good to me.....

-- 
Kevin J. Cummings
kjchome at rcn.com
cummings at kjchome.homeip.net
cummings at kjc386.framingham.ma.us
Registered Linux User #1232 (http://counter.li.org)


More information about the antlr-interest mailing list