I enjoy writing compilers.

I don’t enjoy writing parsers.

This is why I program in Scheme. As long as you use an s-expression-based syntax, you don’t need a parser. If only the whole world used Scheme…

I have gone through many, many syntax errors made by the students in my study. Along with writing, I’ve been setting up my last experiment; it involves, among other things, convincing BlueJ to respond to some syntax errors differently than it does now. And this means a new ANTLR grammar.

For example, Given code like

if(noteNumber = numberOfNotes()) {
    // Note number not valid, so do nothing.
}

there’s a number of errors you could report. In the case this came from, the parser complains that it found EOF, where it expected ‘}’. This is awful; I don’t want my parser to tell me that I forgot a bracket; instead, I want it to yell at me for misusing the ‘if’ syntax.

So, by changing the ANTLR production from

 // If-else statement
    |   "if"^ LPAREN! expression RPAREN! statement

to

 // If-else statement
    |   "if"^ LPAREN! expression RPAREN! compoundStatement

I force students to ALWAYS place the first branch of an ‘if’ in a set of curly brackets. While this may sound strict, too many of the students in my study make repeated mistakes with simple syntaxes like the ‘if’. I believe some of that problem comes from poor error message design, and some of it comes from a parser that is too permissive. I don’t want “optional” brackets—if they’re optional, it means a student can make mistakes like the one above and not see that they’ve commented out a required bracket… because the parser allowed it as a valid, optional syntax.

What I’d love to do is be able to track how many times in a row a student generates an error on the same production in a grammar, so I can give higher-level error messages over time; however, I suspect that will be beyond the scope of the study, as every modification and experiment takes considerable time.

The moral of the story? This is going to take a long, long time. More hours than I have available to me, right at the moment. Who knows; perhaps I’ll have an ANTLR epiphany. Or someone out there will drop me a note, and say “What do you need, Matt? I’m an ANTLR guru!”

Ah. To dream.

Comments are closed.