Syntax errors in the compiler front-end?

32 views
Skip to first unread message

Ron Burkey

unread,
Dec 13, 2022, 5:25:28 PM12/13/22
to BNFC Developers and Users
For a language I'm trying to implement, I've made an LBNF grammar (HAL_S.cf), and have used BNFC to create a compiler front-end for it in one of the ways described in the docs:

    bnfc --c -l -m HAL_S.cf
    make

The front-end this builds (TestHAL_S) works fine for me at this my current development stage.  It returns reasonable abstract syntaxes that make sense to me.

Except for one thing.  If the there are any errors in the source code I feed into the compiler front end, the front end seems to stop as soon as it reports finding a single syntax error.

Is that what it's supposed to do?  I'd certainly like to learn about more than one error per compiler run, if I could.

Ron Burkey

unread,
Dec 14, 2022, 10:01:27 AM12/14/22
to BNFC Developers and Users
In Linux (which I'm using), BNFC generates the Parser.c using bison.   This explanation in the bison docs appears to be saying that the problem of stopping after a single syntax error is encountered is the expectation in bison, but it can be worked around.  The explanation given, if I understand it correctly, is that for each rule in the bison file generated by BNFC (in my case, HAL_S.y), it's necessary to modify the rule with some kind of an error-recovery action.  Not much advice on that error-recovery action is given, except that duplicating the rule and then adding the token error (predefined in bison) to the duplicate rule allows compilation to continue after an error in that rule, because it makes the syntax error non-fatal.  There's a question about how much of the parsing context needs to be discarded at that point, but just ignoring the entire line apparently works well in most cases.

But what is a practical way to implement this when (for example, in my case), there are hundreds of rules in the grammar?  There doesn't seem to be any way to add all of these error tokens directly in the LBNF grammar, since the token error is predefined in bison but not in BNFC.  And fixing up the rules in the generated bison .y file means that all of my fixups are going to be overwritten the next time the grammer is modified and the bison .y file is updated.

This seems to me to be so fundamental that I can't have been the first one to encounter the problem.  Any advice on the workflow for this would be appreciated.
Reply all
Reply to author
Forward
0 new messages