[antlr-interest] A newbie having problems creating his first grammar...

Niklas Söderberg niklas.soderberg at gmail.com
Tue Sep 25 02:22:36 PDT 2007


Hi all! A newbie here, just discovered Antlr and it seems like an awesome
tool! I'm trying to create a grammar for a querylanguage I need to parse in
c# and I'm totally stuck, perhaps some kind soul out there could point me in
the right direction... feel free to comment my grammar as well, perhaps I'm
going at this all wrong?

My problem is that when I run my grammar in the AntlrWorks interpreter, the
last parenthesis in my input is "lost", the interpreter runs without error
and displays the grammar treegraph, but the tree is "unbalanced" with the
last parenthesis missing, and I can't understand what in my rules are
causing this behaviour?

It's probably a silly mistake on my part, but obviously I can't see it.

I figured it should be easier to start small, so I picked a piece of the
input and started working on that, but I can't even get this to work as
expected:-/ The small sample input I'm trying to parse is this:

<OR>(<WILDCARD>(`abc*`),<WILDCARD>(`qwe*`))

using this grammar:

grammar Query;

options
{
language = 'CSharp';
output=AST;
}

fragment KEYWORD_BEGIN : '<';
fragment KEYWORD_END : '>';
fragment KEYWORD_LIST
 : ('WILDCARD' | 'OR' | 'AND' | 'WORD' | 'CASE' )
 ;

KEYWORD
 : (KEYWORD_BEGIN KEYWORD_LIST KEYWORD_END)
 ;

opExpr  : operation (',' operation)*
 | KEYWORD '(' opExpr ')'
 ;

operation
 : (KEYWORD '(' STRING_LITERAL ')')
 ;

INTLIT  : ('0'..'9')+;
STRING_LITERAL : ('`'! (~('`'|'\n'|'\r'))+ '`'!);
WS : (' ' | '\t' | '\f' | '\r\n' | '\r' | '\n') { channel = HIDDEN; };

Thanks in advance for any help,
Niklas

If anyone is interested, here is a sample of a complete query that I want to
parse:

<AND>(((<OR>(<WILDCARD>(`string1*`),<NEAR/5>(<OR>(<WILDCARD>(`string2*`)),<OR>(<WILDCARD>(`string3*`))),<NEAR/5>(<OR>(<CASE><WORD>(`string4`),<CASE><WORD>(`string5`)),<OR>(<WILDCARD>(`string6*`),<WILDCARD>(`string7*`),<WILDCARD>(`string8*`),<WILDCARD>(`string9*`),<WILDCARD>(`string10*`),<WILDCARD>(`string11*`)))))<IN>(MAINTITLE,SUBTITLE,INGRESS,ARTICLETEXT)),(<OR>(<WORD>4617,<WORD>4619,<WORD>4620)<IN>(SOURCE_ID)))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070925/003e837f/attachment.html 


More information about the antlr-interest mailing list