[antlr-interest] Tree grammar for 'zero or more' rewrite

Ted Villalba ted.villalba at gmail.com
Wed Jul 11 16:16:44 PDT 2007


Hi,

Thanks for the response. When you say draw the empty, one term and then two
term, I hope this is what you meant. Let me know if not.

What I have is a query grammar where you can say:
query=(all the kings men)

or add an operator like
query=(all the kings men AND all the kings horses)

and add another operator:
query=(all the kings men AND all the kings horses OR a nail)


Here is the full grammar fwiw:

grammar WQL;

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 (WS)* value (WS)* RPAREN -> ^('=' tag value)
    | tag '=' terms+ -> ^('=' tag terms)
        | qid
        ;

value   : (terms-> ^(TERMS terms)) ((WS)+ operator (WS)+ v=value ->
^(operator $value ^(VALUE $v)))*
    | LPAREN! value RPAREN! ((WS)+ operator^ (WS!)+ value)*
    ;

tag    : WCHAR -> ^(WCHAR)
    ;

terms    : WCHAR (WS WCHAR)*  -> 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,
Ted




On 7/11/07, Terence Parr <parrt at cs.usfca.edu> wrote:
>
>
> On Jul 11, 2007, at 3:44 PM, Ted Villalba wrote:
>
> > Hi,
> >
> > I'm trying to write the tree grammar that will recognize the output
> > from the following rewrite rule:
> >
> > value   : (terms-> ^(TERMS terms)) ((WS)+ operator (WS)+ v=value ->
> > ^(operator $value ^(VALUE $v)))*
>
> Can you draw the empty, one term, then two term case for me?
>
> The tree seems a bit weird to me.  I think i can simplify the grammar
> for me if you show me your intentions.
>
> Ter
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070711/5460897b/attachment.html 


More information about the antlr-interest mailing list