[antlr-interest] Fundamental tree parsing question

Ted Villalba ted.villalba at gmail.com
Tue Jul 10 12:45:07 PDT 2007


Thanks Benjamin.
Here is the grammar attached below, with your most recent suggestion. I can
walk it without issue, with input like :
query=(city AND mountain dwellers)

until I try to add the use of the imaginary tokens, for example, in the
'field' rule below:
The grammar:

grammar TQL;

options{
    output=AST;
    ASTLabelType=CommonTree;
}

tokens{ TAG; VALUE; TERMS;} //imaginary token types

@header{
import java.util.HashMap;
}

@members {

HashMap fieldMap = new HashMap();

}


start   : ( query  {System.out.println("AST:\n"+$query.tree.toStringTree());}
)+
         ;


query   : field ((WS!)+ BOOL_OP^ (WS!)+ query)*
           | LPAREN! query RPAREN! ((WS!)* BOOL_OP^ (WS!)* query)*
            ;

field    : tag '=' LPAREN value RPAREN -> ^('=' ^(TAG tag) ^(VALUE value))
          | tag '=' terms+ -> ^('=' tag terms)
          | qid
           ;

value   : terms ((WS)+ operator^ (WS)+ value)*
          | LPAREN! value RPAREN! ((WS)+ operator^ (WS!)+ value)*
          ;

tag     : WCHAR -> ^(TAG WCHAR)
         ;

terms : WCHAR (WS WCHAR)*  -> ^(TERMS WCHAR+)
         | QUOTE WCHAR (WS! WCHAR)* QUOTE
         ;

qid     : '#'!DIGIT
         ;

operator: BOOL_OP|WOK_OP;
BOOL_OP    : 'AND'|'and'|'OR'|'or'|'NOT'|'not';
WOK_OP    : 'SAME'|'same'|'NEAR'('/'DIGIT+)*|'near'('/'DIGIT+)*;
DIGIT   : ('0'..'9');
WS      : (' '|'\t'|'\r'|'\n')+ ;
LPAREN    : '(' ;
RPAREN    : ')' ;
QUOTE    : '"';
WCHAR   : ~('='|'('| ')'|'"'|' '|'\t'|'\n'|'\r'|'#')+;


Thanks again for the help.

Ted





On 7/10/07, Benjamin Niemann <pink at odahoda.de> wrote:
>
> Ted Villalba wrote:
>
> > Thanks guys, for the tips. I've been trying to introduce imaginary nodes
> > into my grammar, but ANTLRWorks keeps flipping out on me when I walk
> > through the debugger.
> > I've tried a couple configurations similar to what ya'll sent, but when
> > stepping through, my CPU and RAM get pegged and I start getting a bunch
> of
> > these:
> >
> > java.net.SocketException: Software caused connection abort: recv failed
> > [...]
>
> That look like a bug - either in ANTLRWorks, the runtime or in code
> generation. Could you please post a complete grammar that demonstrates
> this
> bug?
>
> --
> Benjamin Niemann
> Email: pink at odahoda dot de
> WWW: http://pink.odahoda.de/
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070710/61a8586b/attachment.html 


More information about the antlr-interest mailing list