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

Re: [antlr-interest] Error messages at tree parsers

Expand Messages
  • Ric Klaren
    Hi, ... You have to use a custom ast that also stores the line number information. There s been quite some discussion on the list about these things. There s a
    Message 1 of 4 , Jul 6, 2001
      Hi,

      On Thu, Jul 05, 2001 at 10:10:57PM +0200, Roberto Turrado Camblor wrote:
      > [1] I'd like to know how can I build an error message at tree parsers, such
      > as
      >
      > an_string + filename + line_where_error_was_detected

      > At the parser, you can call filename() and getLine() functions, the latter
      > also associated to a token. But tree parsers don't have got none of this
      > methods.

      You have to use a custom ast that also stores the line number information.
      There's been quite some discussion on the list about these things. There's
      a FAQ entry somewhere as well (it might be dated but the general principle
      stands)

      If you want a complete example you might take a look at the SDL or TTCN-3
      parsers from Michael Schmitt.

      http://www.itm.mu-luebeck.de/english/research/specification/sdl2000/
      http://www.itm.mu-luebeck.de/english/research/specification/ttcn3parser/index.html

      They contain all this kindoff stuff if I'm not mistaken. But these are
      C++... hmm you probably using java. Well the idea is still the same.

      > parsers. Do I have to use this approach? Then, how can I fill the line?

      See above :)

      > [2] At parsing, when I get an error, I generate a string and throw a
      > SemanticException from a semantic module. Then, at the grammar, I catch the
      > exception and throw it again. I do this because I don't want to code the
      > exit() all the time. This way the exception is thrown until it reaches the
      > top, the only point where it is printed, and the program exits. Do you think
      > this is a good way?

      I don't quite follow what you are saying here? Parsing in the treewalker?
      Or in the normal parser? exit() you can change by overriding the panic
      methods at places.

      > [3] I'm using Kawa as IDE. I can't set the classpath in order not to get
      > this error:
      >
      > java.lang.NoClassDefFoundError: antlr/ANTLRGrammarParseBehavior
      >
      > When I use jdk from the command line I don't get this error, and the
      > classpath is exactly the same. Is anyone of you using Kawa? Have you got
      > this problem?

      No clue. I'm a C++ guy =)

      Ric
      --
      -----+++++*****************************************************+++++++++-------
      ---- Ric Klaren ----- klaren@... ----- +31 53 4893722 ----
      -----+++++*****************************************************+++++++++-------
      Why don't we just invite them to dinner and massacre them all when they're
      drunk? You heard the man. There's seven hundred thousand of them.
      Ah? ... So it'd have to be something simple with pasta, then.
      --- From: Interesting Times by Terry Pratchet
      -----+++++*****************************************************+++++++++-------
    • Chuck Siska
      roberto -- here s an example of how i handle line numbers for error messages in the treewalker. it s not very clever but it is adequate. For the file, keep
      Message 2 of 4 , Jul 6, 2001
        roberto --

        here's an example of how i handle line numbers for error
        messages in the treewalker. it's not very clever but it
        is adequate. For the file, keep track of it's name each
        time you open your input stream so that it is available
        when you create your error messages. if you nest input
        streams, you might consider a stack of filenames.
        good luck.

        -- chuck

        Naive Line Numbers for Errors

        ------------------------------------------------------------

        I use the following class to capture the line number of a token
        in the parser. The line number is obtained from the lexer token,
        during the initialization of the AST.

        /** A simple AST node with line number */
        public class SASM_Line_AST extends CommonAST
        {
        int iline = 0;

        public SASM_Line_AST( Token tok )
        {
        setText( tok.getText() ); // transfer token text.
        setType( tok.getType() ); // transfer token type.
        iline = tok.getLine(); // transfer token line number.
        }

        public int getLine()
        {
        return iline;
        }

        // To use heterogeneous trees with a tree grammar.
        /** Get the token text for this node */
        public String getText()
        {
        return super.getText();
        }

        /** Get the token type for this node */
        public int getType()
        {
        return super.getType();
        }

        // satisfy abstract methods from BaseAST
        public void initialize(int t, String txt) {
        }
        public void initialize(AST t) {
        }
        public void initialize(Token tok) {
        }
        }
        ------------------------------------------------------------

        This is a sample parser rule. Note the AST node class being
        selected to represent the semicolon token. This is how the line
        number from the lexer's semicolon is incorporated into the AST
        node representing the semicolon. I do this for all occurrences
        of the lexer semicolon token. This is as fine-grained I get in
        reporting line numbers in this grammar.

        decl_const_ident! { Token tok = null; } :
        CONST^ IDENT ax:identifier EQUAL { tok = LT(1); } identifier
        SEMICOLON<AST=SASM_Line_AST>
        {
        String sax = ax_AST.getText();
        m_sasmlexer.m_macrotab.put( sax, tok );
        }
        ;

        ------------------------------------------------------------

        This is an example treewalker rule. Note how the line
        information is extracted from the semicolon's AST node. Error
        messages include the value of m_iline in order to provide line
        number information in the message.

        foo : #(EQUAL ax:ID #(BOR bx:ID)) semx:SEMICOLON
        {
        m_iline = ((SASM_Line_AST)semx).getLine();
        //...
        }
        ;


        Roberto Turrado Camblor wrote:
        >
        > Hello!
        >
        > [1] I'd like to know how can I build an error message at tree parsers, such
        > as
        >
        > an_string + filename + line_where_error_was_detected
        >
        > At the parser, you can call filename() and getLine() functions, the latter
        > also associated to a token. But tree parsers don't have got none of this
        > methods.
        >
        > Since I'm interpreting a file at the tree parser, if I get an error, how can
        > I show the file and the line?
        >
        > NOTE: the errors I'm getting now are such as this one:
        >
        > line 0: Error: existsSymbol: p
        >
        > where "Error: existsSymbol: p" is a string generated by myself. I think
        > "line 0: " is generated by the reportError() method, present at tree
        > parsers. Do I have to use this approach? Then, how can I fill the line?
        >
        > ...
        >
        > Thanks so much and see you!
        --
        |\_/\_.-'""``:-._
        What is life without looking for . . `; -._ )-;-,_`)
        the next cute little bug to play with? v_,- _ ),(,.\ ``-'
        _.- _.,-_/ / ((.'
        -- chuck.siska@... `<}:.. ((,.-' ((,/
      • Roberto Turrado Camblor
        Thanks so much for your answers. I m sure they ll be useful. _________________________________________________________ Do You Yahoo!? Get your free @yahoo.com
        Message 3 of 4 , Jul 6, 2001
          Thanks so much for your answers. I'm sure they'll be useful.


          _________________________________________________________
          Do You Yahoo!?
          Get your free @... address at http://mail.yahoo.com
        Your message has been successfully submitted and would be delivered to recipients shortly.