[antlr-interest] "type cannot be resolved or is not a field"
The Researcher
researcher0x00 at gmail.com
Mon Sep 12 16:18:38 PDT 2011
Hi Phil,
Try doing it this way from the command line for the first version of the
grammar, but read the explanations in the 5-minute introduction.
Create these four files and run the highlighted commands. I showed you a
full run show you could see what to expect.
Also, notice that CLASSPAH only references antlr-3.4-complete.jar
SimpleCalc.g
grammar SimpleCalc;
tokens {
PLUS = '+' ;
MINUS = '-' ;
MULT = '*' ;
DIV = '/' ;
}
/*------------------------------------------------------------------
* PARSER RULES
*------------------------------------------------------------------*/
expr : term ( ( PLUS | MINUS ) term )* ;
term : factor ( ( MULT | DIV ) factor )* ;
factor : NUMBER ;
/*------------------------------------------------------------------
* LEXER RULES
*------------------------------------------------------------------*/
NUMBER : (DIGIT)+ ;
WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ { $channel = HIDDEN; }
;
fragment DIGIT : '0'..'9' ;
Main.java
import org.antlr.runtime.*;
public class Main
{
public static void main(String[] args) throws Exception {
SimpleCalcLexer lex = new SimpleCalcLexer(new ANTLRFileStream(args[0]));
CommonTokenStream tokens = new CommonTokenStream(lex);
SimpleCalcParser parser = new SimpleCalcParser(tokens);
try {
parser.expr();
} catch (RecognitionException e) {
e.printStackTrace();
}
}
}
bad.dat
6/2+(3*4)-1
good.dat
6/2+3*4-1
student at antlr:~/projects/antlr/mail/mail002$ echo $CLASSPATH
/home/student/antlr-3.4-complete.jar:
student at antlr:~/projects/antlr/mail/mail002$ ls -l
total 16
-rw-r--r-- 1 student student 12 Sep 12 18:40 bad.dat
-rw-r--r-- 1 student student 10 Sep 12 18:40 good.dat
-rw-r--r-- 1 student student 460 Sep 12 18:38 Main.java
-rw-r--r-- 1 student student 640 Sep 12 18:47 SimpleCalc.g
student at antlr:~/projects/antlr/mail/mail002$ java org.antlr.Tool
SimpleCalc.g
student at antlr:~/projects/antlr/mail/mail002$ ls -l
total 40
-rw-r--r-- 1 student student 12 Sep 12 18:40 bad.dat
-rw-r--r-- 1 student student 10 Sep 12 18:40 good.dat
-rw-r--r-- 1 student student 460 Sep 12 18:38 Main.java
-rw-r--r-- 1 student student 640 Sep 12 18:47 SimpleCalc.g
-rw-r--r-- 1 student student 8962 Sep 12 18:48 SimpleCalcLexer.java
-rw-r--r-- 1 student student 5852 Sep 12 18:48 SimpleCalcParser.java
-rw-r--r-- 1 student student 83 Sep 12 18:48 SimpleCalc.tokens
student at antlr:~/projects/antlr/mail/mail002$ javac *.java
student at antlr:~/projects/antlr/mail/mail002$ ls -l
total 52
-rw-r--r-- 1 student student 12 Sep 12 18:40 bad.dat
-rw-r--r-- 1 student student 10 Sep 12 18:40 good.dat
-rw-r--r-- 1 student student 857 Sep 12 18:49 Main.class
-rw-r--r-- 1 student student 460 Sep 12 18:38 Main.java
-rw-r--r-- 1 student student 640 Sep 12 18:47 SimpleCalc.g
-rw-r--r-- 1 student student 3905 Sep 12 18:49 SimpleCalcLexer.class
-rw-r--r-- 1 student student 8962 Sep 12 18:48 SimpleCalcLexer.java
-rw-r--r-- 1 student student 3609 Sep 12 18:49 SimpleCalcParser.class
-rw-r--r-- 1 student student 5852 Sep 12 18:48 SimpleCalcParser.java
-rw-r--r-- 1 student student 83 Sep 12 18:48 SimpleCalc.tokens
student at antlr:~/projects/antlr/mail/mail002$ java Main bad.dat
bad.dat line 1:4 no viable alternative at character '('
bad.dat line 1:8 no viable alternative at character ')'
student at antlr:~/projects/antlr/mail/mail002$ java Main good.dat
student at antlr:~/projects/antlr/mail/mail002$
Notice that using good.dat is valid data so no errors will be presented.
Hope that helps,
Eric
On Mon, Sep 12, 2011 at 6:28 PM, phil jones <interstar at gmail.com> wrote:
>
> Hi everyone,
>
> I don't know if this is a support list, so forgive me if I shouldn't
> be asking this here. (If not here, where?)
>
> I just downloaded and am trying to learn Antlr, starting with the
> calculator example in the five minute guide. I've generated the Java
> lexer / parser, but when I try to compile these I'm getting the error
> :
>
> "type cannot be resolved or is not a field"
>
> The error occurs in the line " this.type = _type;"
>
> in a generated block :
>
> // $ANTLR start PLUS
> public final void mPLUS() throws RecognitionException {
> try {
> int _type = PLUS;
> // SimpleCalc.g:3:6: ( '+' )
> // SimpleCalc.g:3:8: '+'
> {
> match('+');
>
> }
>
> this.type = _type;
> }
> finally {
> }
> }
> // $ANTLR end PLUS
>
> That's in the class SimpleCalcLexer which extends Lexer. So can it be
> that Lexer doesn't have a "type" attribute? Should it have one?
>
> I generated the lexer code from the command line, but am now trying to
> compile it in Processing. (I put the antlr3.3 jar in the Processing
> libraries directory.)
>
> cheers
>
> phil jones
>
> 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