What is the difference between syntax and semantics in programming languages?

What is the difference between syntax and semantics in programming languages ​​(e.g. C, C ++)?

+82
syntax programming-languages semantics
Jul 29 '13 at 17:22
source share
6 answers

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.

+158
Jul 29 '13 at 18:10
source share

Syntax refers to the structure of a language, tracing its etymology before everything comes together.
For example, you might want code to be compiled by declaring a type, then a name and a semicolon, so that they are syntactically correct.

 Type token; 

On the other hand, semantics are about meaning. The compiler or interpreter may complain about syntax errors. Your colleagues will complain about semantics.

+14
Jul 29 '13 at 17:32
source share

Wikipedia has an answer. Reading syntax (programming languages) and semantics (informatics) wikipages.

Or think about the work of any compiler or interpreter . The first step is lexical analysis, in which tokens are generated by dividing the string into tokens, and then parsing them , which build some abstract syntax tree (which is a representation of the syntax). The next steps include the transformation or evaluation of these AST (semantics).

Also, note that if you defined option C, where each keyword was converted to its French equivalent (so if becomes si , do becomes faire , else becomes sinon , etc., etc ...) you would definitely change the syntax of your language, but you don’t change the semantics much: programming in this French C will not be easier!

+8
Oct 17 '14 at 11:21
source share

Semantics is what your code means - which you can describe in pseudo-code. Syntax is an actual structure - everything from variable names to half-columns.

+6
Jul 29 '13 at 17:27
source share

Syntax is the structure or form of expressions, operators, and program units, but Semantics is the meaning of these expressions, operators, and program units. Semantics are executed directly from the syntax . Syntax refers to the structure / form of the code defined by a particular programming language, but Semantics refers to the value assigned to characters, symbols, and words.

+2
May 17 '15 at 2:50
source share

Syntax: this refers to the grammatical structure of the language. If you are writing c. You must take great care to use data types, tokens [it can be a literal or a character like "printf ()". It has 3 outputs, "printf, (,)"]. Likewise, you must be very careful how you use a function, the syntax of a function, declaring a function, defining, initializing, and calling it.

While semantics, it refers to the logic or concept of a sentence or statements. If you say or write something from a concept or logic. Then you are semantically mistaken.

+1
Apr 02 '15 at 16:45
source share



All Articles