[antlr-interest] Cannot find symbol?

Bart Kiers bkiers at gmail.com
Fri Mar 9 13:33:22 PST 2012


Hi Keith,

A couple of remarks:
- variables you define yourself in the `@init` block, shouldn't have a `$`
in front of them when you refer to them;
- `c.text();` should be `$c.text;`. A `$` should be there, and `text` is an
attribute, not a method;
- `names.add($col);` is wrong: `$col` refers to a token, while you need to
add a string to the list. It should be `names.add($col.text);`

Try something like this:


min returns [String result]
@init {
  java.util.List<String> names = new java.util.ArrayList<String>();
}
 : col1=COLUMN {names.add($col1.text);} (',' col2=COLUMN
{names.add($col2.text);})+
   {
     $result = names.get(0);
     for (int i = 0; i < names.size(); i++) {
       if ($result.compareToIgnoreCase(names.get(i)) < 0) $result =
names.get(i);
     }
   }
 ;


COLUMN returns [String name]
 : '(' c=COLUMNNAME ')' {$name = $c.text;}
 ;

COLUMNNAME
 : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
 ;


Regards,

Bart.


On Fri, Mar 9, 2012 at 10:10 PM, Jones, Keith <kpj1 at wustl.edu> wrote:

> I'm testing a simple grammar I've written, but for some reason when I try
> to run it through the debugger it fails to compile, giving me multiple
> "Cannot find symbol" errors. Is it a problem with my syntax or what? Here
> are the relevant portions:
>
> min    returns [String result]
>
> @init {
>    List<String> names = new ArrayList<String>();
>    result = "";
> }:
>
>    col=COLUMN { $names.add($col); }
>        (COMMA col=COLUMN {$names.add($col);} )+
>        {$result = $names.get(0);
>        for (int i = 0; i < names.size; i++) {
>            if ($result.compareToIgnoreCase(names.get(i)) < 0)
>                $result = names.get(i);
>        }};
>
>
> COLUMN returns [String name]
>
> @init {
>    name = "";
> }:
>    OPENPAREN (c=COLUMNNAME {$name = c.text();}) CLOSEPAREN;
>
>
> COLUMNNAME    :    ('a'..'z'|'A'..'Z'|'_')
> ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
>    ;
>
>
> These give me the following errors when I attempt to run the debugger:
>
> [15:05:56] cannot find symbol
> [15:05:56] symbol  : variable $names
> [15:05:56] location: class autocompleteParser
> [15:05:56]              $names.add(col);
> [15:05:56]              ^
> [15:05:56] cannot find symbol
> [15:05:56] symbol  : variable $names
> [15:05:56] location: class autocompleteParser
> [15:05:56]                     $names.add(col);
> [15:05:56]                     ^
> [15:05:56] cannot find symbol
> [15:05:56] symbol  : variable $names
> [15:05:56] location: class autocompleteParser
> [15:05:56]             result = $names.get(0);
> [15:05:56]                      ^
> [15:05:56] cannot find symbol
> [15:05:56] symbol  : method text()
> [15:05:56] location: class org.antlr.runtime.CommonToken
> [15:05:56]             name = c.text();
> [15:05:56]                     ^
>
> Why is this happening? I've stuck as closely to the provided examples as I
> could.
>
> Any help would be greatly appreciated!
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>


More information about the antlr-interest mailing list