[antlr-interest] Tree grammar, can't find the error

Yves Weißig weissig at rbg.informatik.tu-darmstadt.de
Tue Oct 25 15:11:16 PDT 2011


Hi,

thanks for your hints, I now produce the dot spec with:
DOTTreeGenerator gen = new DOTTreeGenerator();
StringTemplate st = gen.toDOT(t);
System.out.println(st);
The tree pretty much assembles what I thought when I run it through dot.
Small examples work for me, I worked through them.
A rule like this:
(1) prog      : ^('PROG' v='WSN') -> emit(v={$v}) ;
would match a node WSN being a child of PROG, or?
(2) prog      : ^('PROG' v='WSN' x+=.*) -> emit(v={$v}, x={$x}) ;
then this would match a node WSN and other nodes being children of PROG?
(3) prog      : ^('PROG' x+=.*) -> emit(x={$x}) ;
and this would match all children of PROG?
Only (3) works for me, although WSN is a child of PROG, the output of
(3), something like 'WSN GROUP WSN GROUP ...' and the dot spec assure me...

Yves

Am 25.10.2011 20:54, schrieb Jim Idle:
> You are just typing the tree syntax like a parser syntax and that will not
> work. Walk the tree and produce the dot spec, produce the .png
> representation and you will see the point. This is all recursive...
> 
> Think of how an expression tree is written:
> 
> expr : ^(PLUS expr expr)
> 	| ^(MINUS expr expr)
> 	| NUMBER
> 	;
> 
> You might need to start with the simple examples before trying to detangle
> the parser that you have, but definitely start looking at the tree
> graphically.
> 
> Jim
> 
>> -----Original Message-----
>> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
>> bounces at antlr.org] On Behalf Of Yves Weißig
>> Sent: Tuesday, October 25, 2011 9:37 AM
>> To: antlr-interest at antlr.org
>> Subject: [antlr-interest] Tree grammar, can't find the error
>>
>> Hi list,
>>
>> I trying to write a tree grammar for this tree:
>>
>> (PROG (WSN   ) (GROUP (COMMAND pard) ) ... )
>>
>> So, a PROG node is parent of multiple WSN, GROUP, etc. nodes.
>>
>> My rules look like:
>> prog      : ^('PROG' (v+=space | v+=group)+) -> emit(v={$v}) ;
>> space     : ^('WSN' v=.*) -> emit(v={$v}) |
>>             ^('TABN' v=.*) -> emit(v={$v}) |
>>             ^('NEWLINEN' v=.*) -> emit(v={$v}) ;
>> group     : ^('GROUP' v+=space*
>>               (v+=group v+=space* |
>>               v+=plaintext v+=space* |
>>               (v+=command | v+=escape)
>>               (v+=delimiter v+=space+ | v+=delimiter | ))+) ->
>> grp(v={$v}) ; ...
>>
>> This throws "can't look backwards more than one token in this stream",
>> so I assume I have an error in my tree grammar... I just managed to
>> write:
>>
>> prog      : ^('PROG' v+=.*) -> emit(v={$v}) ;
>>
>> which gives me:
>>
>> WSNGROUP
>>
>> But I want to output the subtrees, so I need the first approach. Can
>> anybody tell me what I'm doing wrong?
>>
>> Best regards, Yves
>>
>> 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