Wednesday, February 13, 2013

Syntax vs Semantics


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).

Actually there are not two levels but three:
  • lexical level: how characters are combined to produce language elements ( i and f produces if)
  • syntactical level: how language elements are combined to produce language expressions ( if(42,==answer and ) produces a conditional statement)
  • semantic level: how language expressions are converted to CPU instructions in order to form a meaning (a conditional statement allows to execute one branch or the other depending on the result of the boolean expression)

Syntax refers to formal rules governing the construction of valid statements in a language. 
Semantics refers to the set of rules which give the meaning of a statement.

Sources: http://programmers.stackexchange.com/questions/113800/whats-the-difference-between-syntax-and-semantics


No comments:

Post a Comment