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...
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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...
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.
Great! Thanks a lot Alec.