Semantics and syntax difference
Syntax is defined by the grammar of the language. Semantics is definied by the implentation.
For example,
int a;
a = 123;
here is a=123; is both syntactically and semantically correct
but consider this
int a;
int *b;
a = b;
a = b is syntactically correct (grammar allows this, lvalue is a variable that can be written, rvalue is a variable or a constant or an expression) but semantically the statement is wrong.
Some compilers may catch this and some may not, even if they catch it is just a warning. Also some semantic errors like above can be avoided by typecasting, but a syntax error cannot be avoided.
One more example suppose you have a class called String and you declare
String s1, s2;
s1="abc";
s2=s1;
Now syntax tells whether this operation has to be permitted as per the grammar. How it is implemented internally is left to semantics.
You can either copy the value of s1 to s2, which is called value semantics or you can just copy the reference, which is called pointer semantics. In case of value semantics you can change s1 and s2 independently but in case of pointer semantics if you change s1 it changs s2 as well.
Sammons Preston TOLD-P:4 Scoring Software and Reporting System Version 1.0 Health and Beauty (Sammons Preston)
|