[antlr-interest] Different result using Java and Python runtime

Tom Klonikowski klonikowski at drheydenreich.de
Wed Sep 12 15:17:18 PDT 2007


Hello list,

my name is Tom and i'm just creating my first parser using ANTLR for a
Python-based project. Its fun so far :)

I encountered a different behaviour of the Java and Python runtime for a
certain grammar (appended below).

Using the test input ": '$$$/context/property=value'" with the Java
runtime i get the expected result:
'$$$/context/property=value'.

With Python i get:
line 1:0 mismatched input ": '$$$/context/property=value'" expecting
STRINGDECL

I'm using V3.0.1

Am i missing something?

Thanks a lot for any hints and recommendations.

Tom

My grammar looks like: <text.g>

grammar test;

// Python
//options {
//    language=Python;
//}
//zstring returns [value] : STRINGDECL { $value=$STRINGDECL.text };

// Java
zstring returns [String value] : STRINGDECL { $value=$STRINGDECL.text; };

STRINGDECL : COLON WSCHAR+ QUOTE STRING QUOTE ;
   
QUOTE    : '\'';
COLON    : ':';

WSCHAR    : ( ' ' );
   
ASCII7BASE : ( 'A'..'Z' | 'a'..'z' | '_' );
   
ASCII7EXT : ( '$' | '/' | '=' );

STRING : ( ASCII7BASE | ASCII7EXT | WSCHAR )+;

// end grammar

Java main: <antlrtest.java>

import org.antlr.runtime.*;
public class antlrtest {
    public static void main(String[] args) throws java.lang.Exception {
        ANTLRStringStream stream =
            new ANTLRStringStream(": '$$$/context/property=value'");
        testLexer lexer = new testLexer(stream);
        testParser parser = new testParser(new CommonTokenStream(lexer));
        System.out.println(parser.zstring());
    }
}

Python main: <antrltest.py>

from testLexer import testLexer as Lexer
from testParser import testParser as Parser
from antlr3 import CommonTokenStream, ANTLRStringStream
stream = ANTLRStringStream(": '$$$/context/property=value'")
lexer = Lexer(stream)
parser = Parser(CommonTokenStream(lexer))
print parser.zstring()


More information about the antlr-interest mailing list