Loading ...
Sorry, an error occurred while loading the content.
 

[antlr-interest] Line number information for AST

Expand Messages
  • hitesh khamesra
    Hi, How to get line number, which says token of AST node belong to that line. with regards, Hitesh. __________________________________ Do you Yahoo!? The New
    Message 1 of 4 , Oct 1 1:53 AM
      Hi,

      How to get line number, which says token of AST
      node belong to that line.

      with regards,
      Hitesh.

      __________________________________
      Do you Yahoo!?
      The New Yahoo! Shopping - with improved product search
      http://shopping.yahoo.com
    • Ric Klaren
      ... In java this should work in C++ I just now see that something wasn t ported over from java mode. If the exception is generated by the match method of the
      Message 2 of 4 , Oct 1 3:09 AM
        On Tue, Sep 30, 2003 at 09:03:41PM -0000, veekook wrote:
        > Is there any way to augment exceptions thrown by the tree parser (eg
        > MismatchedTokenException) to maintain line numbers? I've subclassed
        > CommonAST to include line numbers in my AST, but is there a method
        > for getting at the line numbers for an offending rule from a catch
        > block embedded in a tree grammar rule? The following doesn't work
        > because tree exceptions don't track line numbers.

        In java this should work in C++ I just now see that something wasn't ported
        over from java mode. If the exception is generated by the match method of
        the treeparser then the node attribute in the exception should be set.
        Which means you're one cast away from the line number. Check out
        MismatchedTokenException.java.

        Depending on how you built your trees some nodes may lack line information.

        Cheers,

        Ric
        --
        -----+++++*****************************************************+++++++++-------
        ---- Ric Klaren ----- j.klaren@... ----- +31 53 4893722 ----
        -----+++++*****************************************************+++++++++-------
        Xander: "Buffy, we need to do something *now*"
        Angel: "We need a distraction."
        Buffy: "Right."
        Angel: "What are you going to do?"
        Buffy: "I'm going to kill them all. That oughtta distract 'em..."
      • antlrlist
        ... Hi Hitesh! The key is this: in ANTLR, AST nodes are completely different from tokens. ASTs are (normally) instances of antlr.CommonAST, while tokens are
        Message 3 of 4 , Oct 2 10:05 AM
          <hiteshk25@y...> wrote:
          >
          > Hi,
          >
          > How to get line number, which says token of AST
          > node belong to that line.
          >
          > with regards,
          > Hitesh.

          Hi Hitesh!

          The key is this: in ANTLR, AST nodes are completely different from
          tokens. ASTs are (normally) instances of antlr.CommonAST, while
          tokens are (normally) instances of antlr.CommonToken. These are
          completely different classes (no inheritance neither composition
          between them). They just "look" similar, because both have a "type"
          attribute and a "text" attribute. CommonType can even be initialized
          with a Token, but don't let this dissapoint you: only type and text
          are copied. This is where similarities end: AST nodes don't have
          file line or columns!

          Putting aside the design reasons for this implementation, let me
          reformulate your question:
          How do I *add* line and column to the AST nodes?

          This is easy: you can subclass antlr.CommonAST and add your own line
          and column attributes. Then override the initialize() methods so
          they copy line and column from the tokens, and add get and set
          methods.

          Finally, you'll have to make your parser use your new AST node class
          instead of antlr.CommonAST... but I'll let you guess how to do it :)

          ...

          I was joking :D! Use options ASTLabelType
          (http://www.antlr.org/doc/options.html#_bb13) and
          Parser.setASTNodeType
          (http://www.antlr.org/javadoc/antlr/Parser.html#setASTNodeType
          (java.lang.String))

          However, if you want to make your life easier, you can use my lib,
          antlraux (http://antlraux.sf.net). It has a class, LexInfoAST
          (http://antlraux.sourceforge.net/javadoc/antlraux/util/LexInfoAST.htm
          l) that will match your needs perfectly (it even includes filename
          O_O)

          Cya!
        Your message has been successfully submitted and would be delivered to recipients shortly.