Sven Van Echelpoel wrote:
This looks like the same tree to me?[...] LT just returns the next token in the input stream and has no regard for rule boundaries etc.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? 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
