view raw
user181415 user181415 - 8 months ago 37
Java Question

Distinguishing between lexical error and semantic error

What is the difference between these two errors, lexical and semantic?

int d = "orange";
inw d = 4;

Would the first one be a semantic error? Since you can't assign a literal to an int? As for the second one the individual tokens are messed up so it would be lexical? That is my thought process, I could be wrong but I'd like to understand this a little more.


There are really three commonly recognized levels of interpretation: lexical, syntactic and semantic. Lexical analysis turns a string of characters into tokens, syntactic builds the tokens into valid statements in the language and semantic interprets those statements correctly to perform some algorithm.

Your first error is semantic: while all the tokens are legal it's not legal in Java to assign a string constant to a integer variable.

Your second error could be classified as lexical (as the string "inw" is not a valid keyword) or as syntactic ("inw" could be the name of a variable but it's not legal syntax to have a variable name in that context).

A semantic error can also be something that is legal in the language but does not represent the intended algorithm. For example: "1" + n is perfectly valid code but if it is intending to do an arithmetic addition then it has a semantic error. Some semantic errors can be picked up by modern compilers but ones such as these depend on the intention of the programmer.

See the answers to whats-the-difference-between-syntax-and-semantics for more details.