[antlr-interest] Beginner needing help with tree grammar

Jim Idle jimi at temporal-wave.com
Fri Oct 21 10:02:03 PDT 2011


A node with no children is specified as simply:

space: WS

Not

space: ^(WS WS)


Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Yves Weißig
> Sent: Friday, October 21, 2011 5:00 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Beginner needing help with tree grammar
>
> Hi list,
>
> I'm stucked with a problem in a tree grammar. I'm trying to extent my
> grammar from my previous post by adding an output module using
> TreeGrammar and String Template. My TreeGrammar looks like this:
>
> prog      : (space | group)+ ;
>
> plaintext : TEXT | AZ | HYPHEN | UNDERSCORE | TILDE | APOSTROPHE | INT
> ;
>
> command   : ^(COMMAND n=AZ v=INT?) -> command(n={$n.text},v={$v.text})
> ;
>
> escape    :
>             ^(ESCAPE v=OPEN) -> escape(v={$v.text}) |
>             ^(ESCAPE v=CLOSE) -> escape(v={$v.text}) |
>             ^(ESCAPE v=BACKSLASH) -> escape(v={$v.text}) |
>             ^(ESCAPE v=ASTERISK) -> escape(v={$v.text}) |
>             ^(ESCAPE v=HYPHEN) -> escape(v={$v.text}) |
>             ^(ESCAPE v=UNDERSCORE) -> escape(v={$v.text}) |
>             ^(ESCAPE v=TILDE) -> escape(v={$v.text}) |
>             ^(ESCAPE v=HEXESCAPE) -> hexescape(v={$v.text}) ;
>
> space     :
>             ^(WS v=WS) -> ws(v={$v.text}) |
>             ^(TAB v=TAB) -> tab(v={$v.text}) |
>             ^(NEWLINE v=NEWLINE) -> newline(v={$v.text}) ;
>
> delimiter :
>             ^(DELIMITER v=WS) -> ws(v={$v.text}) |
>             ^(DELIMITER v=TAB) -> tab(v={$v.text}) |
>             ^(DELIMITER v=NEWLINE) -> newline(v={$v.text}) ;
>
> group     :
>             ^(GROUP v+=space*
>               (v+=group v+=space* |
>               v+=plaintext v+=space* |
>               (v+=command | v+=escape)
>               (v+=delimiter v+=space+ | v+=delimiter | ))+) ->
> group(v={$v}) ;
>
> Now given the previous example RTF text my parser works fine and gives
> me:
>
> (WS   ) (GROUP (COMMAND pard) (COMMAND fs 32) (COMMAND b) (DELIMITER  )
> NOTES (COMMAND par)) (NEWLINE
> ) (WS   ) (GROUP (COMMAND pard) (COMMAND fs 26) (DELIMITER  ) Recently
> (WS  ) I (WS  ) skimmed (WS  ) (GROUP (COMMAND i) (DELIMITER  )
> Structure (WS  ) and (WS  ) Interpretation (WS  ) of (NEWLINE
> ) (WS    ) Computer (WS  ) Programs) , (WS  ) by (WS  ) Sussman (WS  )
> and (WS  ) Abelson , (WS  ) and (WS  ) I (WS  ) think (WS  ) there (WS
> ) should (NEWLINE
> ) (WS    ) have (WS  ) been (WS  ) more (WS      ) (TAB 	) (WS  )
> pictures . (NEWLINE
> ) (WS   ) (COMMAND line) (DELIMITER  ) I (WS  ) like (WS  ) pictures .
> (WS   ) Is (WS  ) that (WS  ) so (WS  ) na (ESCAPE \'ef) ve ? (NEWLINE
> ) (WS   ) (COMMAND par))
>
> the tree walker now fails:
>
> node from line 1:0 required (...)+ loop did not match anything at input
> 'WS'
>
> but there is a WS node at the beginning, what am I 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


More information about the antlr-interest mailing list