Syntax vs. Semantics – What’s the Difference?

semanticssyntax

I've always thought that referring to the syntax of a language was the same as referring to the semantics of a language. But I've been informed that apparently that's not the case. What's the difference?

Best Answer

Semantics ~ Meaning

Syntax ~ Symbolic representation

So two programs written in different languages could do the same thing (semantics) but the symbols used to write the program would be different (syntax).

A compiler will check your syntax for you (compile-time errors), and derive the semantics from the language rules (mapping the syntax to machine instructions say), but won't find all the semantic errors (run-time errors, e.g. calculating the wrong result because the code says add 1 instead of add 2).