[antlr-interest] problem with semantic predicates, local declarations, and hoisting

Madden, Warren warren.madden at lmco.com
Wed Aug 8 07:59:11 PDT 2007


Greetings!

 

I'm trying to write a grammar that recognizes a particular data format
that includes some fixed length and some variable length fields.  I'm
running into problems with my semantic predicates being hoisted up, and
moving out of the context of my local variables.  Hoping someone can
point me in the right direction to solve my problem.

 

Below is a sample grammar.  It should recognize a fixed length field of
either 12 or 18 characters, then a space, and then a variable length
number string:

 

 

grammar predtest;

            

parent

            :           mandatory_child SPACE NUMBER+ (CR|LF)?EOF;

 

mandatory_child  @init { int n = 0; }

            :           ( { n <= 17 }?=>

                        ~(CR|LF)

                        { n++; })+

                        {System.out.println("text is " + $text + ", n is
" + n );}

                        {n == 12 || n == 18}?;

 

alphanum

            :           LETTER | NUMBER;

 

LETTER            :           ('a'..'z' | 'A'..'Z');

NUMBER          :           '0'..'9';

SLASH :           '/';

SPACE :           ' ';

TAB      :           '\t';

CR        :           '\r';

LF        :           '\n';

ASCIICHAR 

            :           '\u0003'..'\u0377';

 

 

The technique in mandatory_child is taken from page 286 of the ANTLR
book, with a slight modification to force the string to be either 12 or
18 chars.

 

When I try to compile, I get the following errors:

 

 

$ ant

Buildfile: build.xml

 

compile:

    [javac] Compiling 2 source files

    [javac] d:\java\predtest\predtestParser.java:285: cannot find symbol

    [javac] symbol  : variable n

    [javac] location: class predtestParser.DFA3

    [javac]                         else if (
(LA3_1==SPACE||(LA3_1>=LETTER && L

A3_1<=ASCIICHAR)) && ( n <= 17 )) {s = 2;}

    [javac]

                       ^

    [javac] d:\java\predtest\predtestParser.java:300: cannot find symbol

    [javac] symbol  : variable n

    [javac] location: class predtestParser.DFA3

    [javac]                         else if (
(LA3_0==NUMBER||(LA3_0>=LETTER &&

LA3_0<=ASCIICHAR)) && ( n <= 17 )) {s = 2;}

    [javac]

                        ^

    [javac] d:\java\predtest\predtestParser.java:313: cannot find symbol

    [javac] symbol  : variable n

    [javac] location: class predtestParser.DFA3

    [javac]                         if ( (LA3_3==SPACE||(LA3_3>=LETTER
&& LA3_3<

=ASCIICHAR)) && ( n <= 17 )) {s = 2;}

    [javac]

                  ^

    [javac] 3 errors

 

BUILD FAILED

d:\java\predtest\build.xml:8: Compile failed; see the compiler error
output for

details.

 

Total time: 1 second

 

 

It appears that ANTLR is taking my predicate and moving it into a DFA,
but not carrying the declaration of n along with it.  Any ideas how I
can fix this?  Should I make n a class variable using @members, rather
than using @init as shown in the example?

 

Thanks!

 

Warren Madden

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070808/e186d7e0/attachment-0001.html 


More information about the antlr-interest mailing list