Menu

Error reporting

Help
2003-02-17
2003-02-17
  • Nicolas Capens

    Nicolas Capens - 2003-02-17

    Hi,

    Is there a mechanism to report more accurate lexical or syntax errors? For example, when the parser expects a constant but it finds something else, I would like to throw an exception with as description "Syntax error: expected constant, found 'x'".

    I tried doing this by defining a keyword ANY like this:
    <ANY: (~['x'] | ['x'])*>
    and then in the parser section something like this:
    constant() {/*accept const*/} | <ANY> {throw "expected constant";}

    This works only half. In places where I expect a constant, it returns another error, like "expected constant declaration".

    Is there an explanation for this? Should I use a totally different method? My apologies if this is already covered in the documentation...

     
    • Alec Panovici

      Alec Panovici - 2003-02-17

      Yes (full details in Sec. 3.5.5). In less detail, you can do something like this: get rid of the YACCish "ANY" thing and use exceptions:

      () nice_constant()
      {
        (accept_constant())
        catch (ParseException &pex)
        { // catch the original exception and throw a
          // customized one
          throw ParseException(pex.where(),
               "Constant expected.");
        }
      }

      If you don't like ParseException (say you want to hae just an error code, and keep the strings elsewhere):

      () nice_constant() throw (MyException)
      {
        (accept_constant())
        catch (ParseException &pex)
        { // catch the original exception and throw a
          // customized one
          throw MyException(pex.where(), ERR_42);
        }
      }

      Alec.

       
    • Nicolas Capens

      Nicolas Capens - 2003-02-17

      Great! Thanks a lot Alec.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.