[antlr-interest] [C target] [3.1.1] Deriving imaginary nodes from real tokens

Gavin Lambert antlr at mirality.co.nz
Fri Dec 12 03:09:30 PST 2008


At 23:49 12/12/2008, Sven Van Echelpoel wrote:
 >Indeed it does, but the single argument case can never be a
 >text, it is always a token, at least if I understand this
 >correctly.

No, I believe you are mistaken there.

   -> FOO["bar"]
   -> FOO[$bar]
   -> FOO[$bar, "baz"]
   -> FOO[FigureSomethingOut($bar, $baz)]
   -> FOO

All of these are perfectly valid ANTLR syntax, and only the first, 
third, and last cases are obvious to ANTLR.  (The second could 
possibly be guessed if a label is being referenced, but if it's a 
parameter then it gets complicated again.  The fourth is just 
impossible.)

 >There are in fact two templates in AST.stg. There is one that
 >does the right thing when a single argument is passed, while
 >the other (which is unfortunately the one that gets invoked
 >in my case) does not.

I haven't actually looked at the templates myself, so I'm just 
going by what you've posted earlier, but this is how it looks to 
me:

Template "createImaginaryNode":
   - if two parameters, call createTypeTokenText, passing in a 
type, a token, and some text.
   - if one parameter, call createTypeText, passing in a type and 
some text.
   - if no parameters, call createTypeText, passing in a type and 
the stringified type.

This template ought to (always!) be called by rewrites of the 
forms shown above.

Template "createRewriteNodeFromElement":
   - if two parameters, call createTypeTokenText, passing in a 
type, a token, and some text.
   - if one parameter, call createTypeToken, passing in a type and 
a token.
   - if no parameters, insert it directly.

This template ought to be called by rewrites of the following 
forms:
   -> FOO
   -> $b
   -> $b["foo"]

(The first one I'm not entirely sure about; if it can produce this 
then it'll only be if there was a FOO matched on the left side of 
the rewrite.  If there couldn't be one then it'll definitely have 
to use the first template instead.)



More information about the antlr-interest mailing list