Sven Van Echelpoel wrote:
[...]
  
    
        
Well, I'm using it now or I wouldn't have noticed it :-) But maybe I
shouldn't and there's a better way of doing it. I'm trying to figure out
that the node returned by LT( 1 ) is a valid node (one of the nodes
created by my grammar). So I check that the type >=
ANTLR3_MIN_TOKEN_TYPE

Would that be correct?
  
      
Probably not. By definition the token types of the nodes can only be
of the types you specify in the lexer or the parser, so unless you
have your own code setting the types then there is no way that you can
get an token type that isn't defined. What is it that you are trying
to do?
    
OK, I'm rewriting an AST and I can't use the plain rewrite rules as I
need some substantial logic to determine which nodes to create. That
decision, for one, depends on the next node. To that end I'm using the
fallback rewrite rule in which I call a function. Something akin to
this:

rewrite_this
   :  ^( node=NEEDS_TO_BE_REWRITTEN b=body )
     -> { createNewNode( $node, $b, LT( 1 ) ) }
   ;


In createNewNode I use $node, $b and the next sibling of $node to
determine what to do. Now I found that when $node has no next sibling,
as in the situation (2) below, LT(1) still returns a node (I would have
expected NULL).

|
+- NEEDS_TO_BE_REWRITTEN   <-- (1)  LT(1) on this one is fine,
|    |                              it returns (2)
|    +- SOME_BODY_NODE
|
+- NEEDS_TO_BE_REWRITTEN   <-- (2) LT(1) on this one also returns a node
     |
     +- SOME_BODY_NODE
  
This looks like the same tree to me?
The type of this node is 3 or ANTLR3_TOKEN_UP, which, from the comments
in antlr3commontoken.h seems to be an imaginary token that signals the
end of the stream.

At first I only checked that LT(1) returned something non-NULL, but
since a node was returned in (2) I ended up creating the wrong node to
return in the rewrite rule. Then I found out that the type returned was
ANTLR3_TOKEN_UP and that I could use ANTLR3_MIN_TOKEN_TYPE to determine
whether I had a 'valid' node or not.

Should LT(1) return a node in (2), or does that signal that something's
amiss? If the behavior of LT(1) is correct, how can I determine that a
node has no next sibling?
  
LT just returns the next token in the input stream and has no regard for rule boundaries etc.

The input stream for an AST consists of a flat stream of tokens with the AST structure represented by the nodes ANTLR_TOKEN_DOWN and ANTLR_TOKEN_UP.

So, if LT(1) is ANTLR_TOKEN_UP, then it means there was no child token. However you can ask the node what the child count is too (see API). More impotantly though, this probably means that your body rule can be empty, in which case your grammar will have written the childless token without a DOWN and UP pair as a rewrite like this:

: X -> ^(X)

is the same as

: X-> X

So, you might find it easier to have:

:  ^( node=NEEDS_TO_BE_REWRITTEN b=body )
     -> { createNewNode( $node, $b, LT( 1 ) ) }
| node=NEEDS_TO_BE_REWRITTEN
;


Also though, if body cannot be empty, then LT(1) in this instance will ALWAYS be ANTLR_TOKEN_UP, matching the closing ) of the rule specification.

You can look at the serialized version of the node stream using the toStringTree() method (see polydif example grammar) and you can also use the makeDot method in the CommonTreeAdaptor to produce a dot (see grahpviz) description of the AST, which the dot program will turn in to a graphic for you.

Basically, I don;t think you need to be test the LT stuff directly, but should specify the grammar structure.

Hope this helps,

Jim

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

Reply via email to