[antlr-interest] [begginer question] could somebody help me to see this simple grammar?

Bart Kiers bkiers at gmail.com
Wed Feb 16 00:40:01 PST 2011


On Wed, Feb 16, 2011 at 9:18 AM, devdoer bird <devdoer2 at gmail.com> wrote:

> ...
> As you see, the last ')'  is missed.
> So what's wrong with my grammar?
>

Nothing.

Given your grammar (and adding a `parse` rule to it):

grammar T;

parse
  :  logicExp EOF {System.out.println("parsed :: " + $logicExp.text);}
  ;

logicExp
  :  FIELDNAME ( '>' | '<' ) (CONST_INT | CONST_STRING)
  | '(' logicExp')'
  ;

fragment DIGIT : '0'..'9';
fragment LOWER  : 'a'..'z';
fragment UPPER  : 'A'..'Z';
FIELDNAME : LOWER (LOWER|DIGIT)*  ;
CONST_INT : DIGIT+;
CONST_STRING : '"' (LOWER|UPPER|DIGIT)* '"';


and creating the test class:

import org.antlr.runtime.*;

public class Main {
    public static void main(String[] args) throws Exception {
        ANTLRStringStream in = new ANTLRStringStream("(a>3)");
        TLexer lexer = new TLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        TParser parser = new TParser(tokens);
        parser.parse();
    }
}


I generated a lexer & parser, compiled all .java files and ran the main
class:

java -cp antlr-3.2.jar org.antlr.Tool T.g
javac -cp antlr-3.2.jar *.java
java -cp .:antlr-3.2.jar Main


which produced the output:

parsed :: (a>3)


So, if ANTLRWorks produces something different, there's something wrong with
it.

Regards,

Bart.


More information about the antlr-interest mailing list