Hugs error messages



  • So I was happily Haskelling away last night, using Hugs, the Haskell interpreter, and everyone was nice and happy. And yet, it was squawking at me:

    ERROR "parse.hs":129 - Syntax error in expression (unexpected ;', possibly due to bad layout)<br><br>Lines 127-&gt;129 look like<br><br>morphLeaf :: Entry -&gt; Entry<br>morphLeaf (Unparsed a) | (isDigit . head) a = ((Leaf . foldl1 (\d e -&gt; d * 10 + e) . map (\d -&gt; (fromEnum d - fromEnum '0')) a)<br>morphLeaf a = a<br><br>Now, I don't mind Hugs alerting me that something's wrong with my code, but I'm a bit miffed at how it's telling me that. Specifically, I don't have a ;' character there. I don't have a ;' character anywhere, even, and yet it's telling me that there's one too many. It might want -1 ;' characters.

    Apparently, someone made the decision to make ;' the end-of-construct signifier and not tell anyone, so I get this message when I miss a )', because telling me that there's no )' there would just be confusing, right?<br><br>"Unexpected ;'" means mismatched parentheses
    "Unexpected }'" also means mismatched parentheses, or maybe it's missing one<br>"Unexpected |'" means an unexpected pipe character. Though after the last two errors, I'm not so inclined to believe it, and I might have a stray colon in there or something, who knows?



  • No offense, but it's a case of RTFM.

    It's too long since I used Haskell, but I remember this language actually uses curly brackets and semicolons to delimit statements, just like C. It also tries to guess where to insert them, so people can write code that looks like mathematical notation. The guesses are based on indentation - think Python - so that's what usually trips the parser. Just as it's telling you.

    Now, there doesn't seem to be any indentation necessary in the code... WTF?


    P.S. Speaking of manuals, take a look at the language reference. Internally, Haskell's syntax is one big, ugly WTF.



  • I've never had any problems decoding GHC(i)'s error messages. You might want to try that instead.

    Using GHCi to create a similar error I get:

    <font face="Courier New"><font face="Arial">Prelude> let f = (head . map (\x -> xx)
    <interactive>:1:31 parse error (possibly incorrect indentation)
    Prelude> let f = (head . map (\x -> x
    x))
    Prelude></font>
    </font>
    It's not completely obvious from the message that it's a ')' that's missing, but at least it doesn't have you searching away for a semi-colon that isn't even there!

    What I find more annoying is that, because I write my code in literate Haskell, the parser always gives me line numbers that are 1 out from the line it's referring to. You get used to it after a while (especially if you're searching for a problem in the context of a whole function) but it's alarming at first.



  • PHP does similar stuff. For example, if you forget a semicolon on one line, you'll get some completely random and unrelated error on a subsequent line, sometimes several lines down. I've learned that if an error seems bogus, I just look up a few lines for missing semicolons.



  • For those of you who have written compilers, or at least dabbled in them in school or similar, you know that compilers break when the grammar does not flow as expected.



    Generally they traverse some sort of structure and expect certain elements to be next. In this case, expecting some sort of delimiter. (I'm sorry, but my Haskell is non-existent) The error message was chosen to be the most logical thing to say in that situation. If it is the wrong thing, more often than not, it could be a copy-paste error trying to fill in error messages, or a simple mistake on the part of the coder.



    Often, especially in the case of line markers, the compiler continues on a bit before figuring out that anything is wrong. This can be the case when the current grammar object has two children, followed by a marker. The compiler won't register an error until it is on or past the position it expects a marker.



    The examples escape me right now, and Weblogic IDE isn't helping by correcting my errors. Suffice it to say that the compiler programmer(s) was (were) trying to cover the most common situation for that error.



  • @Shen said:


    "Unexpected ;'" means mismatched parentheses<br>"Unexpected }'" also means mismatched parentheses, or maybe it's missing one
    "Unexpected `|'" means an unexpected pipe character. Though after the last two errors, I'm not so inclined to believe it, and I might have a stray colon in there or something, who knows?


    The error messages make total sense, although they could be improved. If I may anthropomorphize a little bit...

    The compiler is happily parsing your code, which contains a missing close paren. It reaches the end of a statement, indicated by semicolon. The parser is not EXPECTING to see a semicolon, because it has still not seen a close paren, and to see a semicolon at this point would be ungrammatical. The compiler, dumb program that it is, reports exactly what has occurred: the semicolon was not expected yet.

    A better compiler would have a special case for this error condition, and check to see the balance state of the parentheses on the parser stack. If the parentheses are unbalanced, it could report that instead of the more cryptic "unexpected ';'" error.

    But this is what I'd consider a "bell and whistle" sort of feature. I wouldn't expect compiler writers to go much farther than this in figuring out what the REAL grammatical problem is, especially in cases where a parser error state could be reached in many different ways (which one do you report? If you're wrong, you're misleading the user).

    It's common for many compilers to report the site of a grammatical error far away from where the actual typo or bug is located. In general figuring out what the real error is falls into the class of "guess what the user wants" sorts of problems, and is more trouble than it's worth.



  • You think those error messages are bad, try almost any flavor of SQL.  Usually the message is stated as "error near XYZ", where XYZ is the last thing you did correctly before the parser got confused.  My favorite variation is "Sytax error near ,".  There's a comma on almost every line in SQL.

    My general rule of thumb is to get the structure right first, then worry about the content.  When I open a "(" or "{", I immediately close it and then back up and start typing in the middle.  I find this especially necessary when writing java in a lame editor like Notepad.  A good IDE will solve 95% of these problems.  I generally pick my current favorite language based on my available IDE choices.  I can learn a language quickly, but I'm not going to be able to write a good IDE in a reasonable amount of time.


Log in to reply