Syntax refers to the structure or grammar of a language. He answers the question: how can I build the right proposal? All languages, even English and other human (so-called "natural") languages ββhave grammars, that is, rules that determine whether a sentence is correctly constructed.
Here are a few C language syntax rules:
- individual statements with semicolons
- enclose conditional expression of IF statement in parentheses
- group several statements into one statement, enclosed in braces
- data types and variables must be declared before the first executable statement (this function was removed on C99.C99 and the latter allow mixed type declarations.)
Semantics is the meaning of a sentence. He answers the questions: is this proposal really? If so, what does this offer mean? For example:
x++; // increment foo(xyz, --b, &qrs); // call foo
are C. syntactically valid operators. But what do they mean? Is it even right to try to convert these statements into an executable sequence of instructions? These questions are at the heart of semantics.
Consider the ++ operator in the first statement. First, is this really possible?
- If x is a floating-point data type, this operator does not make sense (in accordance with the rules of the C language) and, therefore, is an error , although the operator is syntactically correct.
- If x is a pointer to some data type , the point of the instruction is to add sizeof ( some data type ) to the value at address x and save the result to a location at address x ".
- If x is a scalar, the value of the expression "adds the value to address x and stores the result in a location at address x".
Finally, note that some semantics cannot be determined at compile time and therefore must be evaluated at runtime. In the ++ operator example, if x already has a maximum value for its data type, what happens when you try to add 1 to it? Another example: what happens if your program tries to dereference a pointer whose value is NULL?
Thus, syntax is a concept that deals only with whether a sentence is really applicable to a grammar of a language. Semantics as to whether a sentence has the right meaning.
Jeff N Jul 29 '13 at 18:10 2013-07-29 18:10
source share