Misleading Compile time Error in Free-form Programming Language

In this post let us discuss a scenario where a missing semicolon error is interpreted as a different error by the compiler in a free-form programming language like C.

What is a Free-form Programming Language?

Free-form language is a programming language in which the positioning of characters on the page in program text is insignificant.

Program text does not need to be placed in specific columns as on old punched card systems, and frequently ends of lines are insignificant. Whitespace characters are used only to delimit tokens, and have no other significance.

In free-form languages the end of a statement is detected by the compiler based on the “statement separator”.

Most popular programming languages C and Java are free-from languages, because whitespace is used only to denote tokens, and otherwise ignored by the compiler.

Semicolon(“;”) is used as the statement separator in both languages to notify the compiler the end of statement.

Misleading Compiler Error for Missing Semicolon

Consider the following classic bubble sort implementation in C.

If you closely inspect the above bubble sort code you would be able to notice that there is a missing semicolon in the line number 5.

So, generally we would expect the compiler to throw a compile time error saying missing semicolon in line number 5.

But following is the compile time error thrown,

As you can see the error is not thrown from line number 5 and it is not for missing semicolon.

Weird!!! Ah?

The error is thrown from line number 6 and it is saying invalid operands to binary expression.

If we look at line number 6 alone you might wonder there is nothing wrong with code and be mad at the compiler.

But, actually the compiler is giving the correct error. Bingo!!!

So what is going on here?

Since there is a missing semicolon (statement terminator) in line number 5, compiler doesn’t interpret line number 5 as a single statement as it is a free-form language. Compiler continues until it finds the semicolon in line number 6.

So, for the compiler the statement is as following,

Since C is a free-from language for the compiler it doesn’t matter whether you write the above statement in a single line or break it into multiple lines. We generally group related statements into a single line for human readability.

The above statement is not a valid expression, so compiler will complain about that instead of the missing semicolon.

The reason it’s not valid is because,

xp is a pointer to an integer, so *xp * xp is trying to multiply an integer (*xp) with a pointer to an integer (xp). It is not valid, so the compiler throws an error indicating it.

The point to note here is, the compiler cannot always detect missing semicolons.

In times when it doesn’t detect missing semicolon it might throw a totally unrelated error in a different line.

Sometimes problems like this will need you to look at previous lines to see if they are okay and without errors. Because, no matter how much you look at the line where the error is reported, there is no error there.

Hope you enjoyed the blog and got something to take away.

Thank you for Reading!

Originally published at https://serantechexplore.wixsite.com on April 12, 2021.

Senior Software Engineer @WSO2, B.Sc.(Hons).Computer Engineering

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store