[antlr-interest] grammar works in antlrworks debug mode but not eclipse
D. Frej
dieter_frej at gmx.net
Fri Nov 11 00:00:18 PST 2011
The grammar is rather stupid and simple
grammar Expression;
// memoize=true;
options {output=AST;backtrack=true;}
tokens {
PLUS='+';
MINUS='-';
MULT='*';
DIV='/';
}
content
: atom ((PLUS|MINUS|MULT|DIV) atom)*
;
atom
: INT
| FLT
;
INT : '0'..'9'+
;
FLT
: ('0'..'9')+ '.' ('0'..'9')* EXPONENT?
| '.' ('0'..'9')+ EXPONENT?
| ('0'..'9')+ EXPONENT
;
WS : ( ' '
| '\t'
| '\r'
| '\n'
) {$channel=HIDDEN;}
;
fragment
EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;
It is executed with the following Java code
ANTLRStringStream input = new ANTLRStringStream("15 / 7.25 +4");
ExpressionLexer lex = new ExpressionLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lex);
ExpressionParser g = new ExpressionParser(tokens); //, 49100, null);
try {
ExpressionParser.content_return r = g.content();
CommonTree ctree = (CommonTree) r.getTree();
System.out.println("ctree.type|" + ctree.getType() + "|" +
ExpressionParser.tokenNames[ctree.getType()] + "|");
} catch (RecognitionException e) {
e.printStackTrace();
}
Yet here the input of "15 / 7.25 +4" does not work. Eclipse outputs
"ctree.type|0|<invalid>|". When I use simply "15" as input, I get the -
correct - output "ctree.type|7|INT|".
Thanks,
Didi
Am 10.11.2011 16:46, schrieb Jim Idle:
> There is not enough information here for anyone to help. What do you mean
> by " only some rules works but not all"? They give syntax errors? Lexer?
> Parser? Exception? etc.
>
> Jim
>
>> -----Original Message-----
>> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
>> bounces at antlr.org] On Behalf Of D. Frej
>> Sent: Thursday, November 10, 2011 2:05 AM
>> To: antlr-interest at antlr.org
>> Subject: [antlr-interest] grammar works in antlrworks debug mode but
>> not eclipse
>>
>> Hi,
>>
>> I created a grammar with the latest antlrworks. With antlrworks' debug
>> functionality I "tested" the grammar. For proper throughout testing I
>> copied the generated Lexer and Parser code to my eclipse project. The
>> only change I made to the code was to add a package declaration.
>>
>> However, when I ran the code in eclipse only some rules works but not
>> all. An input that works perfectly in antlrworks debug functionality is
>> not working in eclipse. Any suggestions? I tried it with antlr-3.4 as
>> well as an older version (3.1.1).
>>
>> This behaviour happens to me for the first time, even though I am using
>> antlr for quite some time and in different versions.
>>
>> Thanks,
>>
>> - Didi
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
>> email-address
> 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