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

SyntaxProgramming LanguagesSemantics

Syntax Problem Overview


What is the difference between syntax and semantics in programming languages (like C, C++)?

Syntax Solutions


Solution 1 - Syntax

TL; DR

In summary, syntax is the concept that concerns itself only whether or not the sentence is valid for the grammar of the language. Semantics is about whether or not the sentence has a valid meaning.

Long answer:

Syntax is about the structure or the grammar of the language. It answers the question: how do I construct a valid sentence? All languages, even English and other human (aka "natural") languages have grammars, that is, rules that define whether or not the sentence is properly constructed.

Here are some C language syntax rules:

  • separate statements with a semi-colon
  • enclose the conditional expression of an IF statement inside parentheses
  • group multiple statements into a single statement by enclosing in curly braces
  • data types and variables must be declared before the first executable statement (this feature has been dropped in C99. C99 and latter allow mixed type declarations.)

Semantics is about the meaning of the sentence. It answers the questions: is this sentence valid? If so, what does the sentence mean? For example:

x++;                  // increment
foo(xyz, --b, &qrs);  // call foo

are syntactically valid C statements. But what do they mean? Is it even valid to attempt to transform these statements into an executable sequence of instructions? These questions are at the heart of semantics.

Consider the ++ operator in the first statement. First of all, is it even valid to attempt this?

  • If x is a float data type, this statement has no meaning (according to the C language rules) and thus it is an error even though the statement is syntactically correct.
  • If x is a pointer to some data type, the meaning of the statement is to "add sizeof(some data type) to the value at address x and store the result into the location at address x".
  • If x is a scalar, the meaning of the statement is "add one to the value at address x and store the result into the location at address x".

Finally, note that some semantics can not be determined at compile-time and therefore must be evaluated at run-time. In the ++ operator example, if x is already at the maximum value for its data type, what happens when you try to add 1 to it? Another example: what happens if your program attempts to dereference a pointer whose value is NULL?

Solution 2 - Syntax

Syntax refers to the structure of a language, tracing its etymology to how things are put together.
For example you might require the code to be put together by declaring a type then a name and then a semicolon, to be syntactically correct.

Type token;

On the other hand, the semantics is about meaning. A compiler or interpreter could complain about syntax errors. Your co-workers will complain about semantics.

Solution 3 - Syntax

Semantics is what your code means--what you might describe in pseudo-code. Syntax is the actual structure--everything from variable names to semi-colons.

Solution 4 - Syntax

Wikipedia has the answer. Read syntax (programming languages) & semantics (computer science) wikipages.

Or think about the work of any compiler or interpreter. The first step is lexical analysis where tokens are generated by dividing string into lexemes then parsing, which build some abstract syntax tree (which is a representation of syntax). The next steps involves transforming or evaluating these AST (semantics).

Also, observe that if you defined a variant of C where every keyword was transformed into its French equivalent (so if becoming si, do becoming faire, else becoming sinon etc etc...) you would definitely change the syntax of your language, but you won't change much the semantics: programming in that French-C won't be easier!

Solution 5 - Syntax

  • You need correct syntax to compile.
  • You need correct semantics to make it work.

Solution 6 - Syntax

Late to the party - but to me, the answers here seem correct but incomplete.

Pragmatically, I would distinguish between three levels:

  1. Syntax
  2. Low level semantics
  3. High level semantics

1. SYNTAX

Syntax is the formal grammar of the language, which specifies a well-formed statement the compiler will recognise.

So in C, the syntax of variable initialisation is:

data_type variable_name = value_expression;

Example:

int volume = 66 * 22 * 55;

While in Go, which offers type inference, one form of initialisation is:

variable_name := value_expression

Example:

volume := 66 * 22 * 55

Clearly, a Go compiler won't recognise the C syntax, and vice versa.

2. LOW LEVEL SEMANTICS

Where syntax is concerned with form, semantics is concerned with meaning.

In natural languages, a sentence can be syntactically correct but semantically meaningless. For example:

The man bought the infinity from the store.

The sentence is grammatically correct but doesn't make real-world sense.

At the low level, programming semantics is concerned with whether a statement with correct syntax is also consistent with the semantic rules as expressed by the developer using the type system of the language.

For example, this is a syntactically correct assignment statement in Java, but semantically it's an error as it tries to assign an int to a String

String firstName = 23;

So type systems are intended to protect the developer from unintended slips of meaning at the low level.

Loosely typed languages like JavaScript or Python provide very little semantic protection, while languages like Haskell or F# with expressive type systems provide the skilled developer with a much higher level of protection.

For example, in F# your ShoppingCart type can specify that the cart must be in one of three states:

    type ShoppingCart =
        | EmptyCart  // no data
        | ActiveCart of ActiveCartData
        | PaidCart of PaidCartData

Now the compiler can check that your code hasn't tried to put the cart into an illegal state.

In Python, you would have to write your own code to check for valid state.

3. HIGH LEVEL SEMANTICS

Finally, at a higher level, semantics is concerned with what the code is intended to achieve - the reason that the program is being written.

This can be expressed as pseudo-code which could be implemented in any complete language. For example:

    // Check for an open trade for EURUSD
    // For any open trade, close if the profit target is reached
    // If there is no open trade for EURUSD, check for an entry signal
    // For an entry signal, use risk settings to calculate trade size
    // Submit the order.

In this (heroically simplified) scenario, you are making a high-level semantic error if your system enters two trades at once for EURUSD, enters a trade in the wrong direction, miscalculates the trade size, and so on.

TL; DR

If you screw up your syntax or low-level semantics, your compiler will complain.

If you screw up your high-level semantics, your program isn't fit for purpose and your customer will complain.

Solution 7 - Syntax

Syntax is the structure or form of expressions, statements, and program units but Semantics is the meaning of those expressions, statements, and program units. Semantics follow directly from syntax. Syntax refers to the structure/form of the code that a specific programming language specifies but Semantics deal with the meaning assigned to the symbols, characters and words.

Solution 8 - Syntax

Understanding how the compiler sees the code

Usually, syntax and semantics analysis of the code is done in the 'frontend' part of the compiler.

  • Syntax: Compiler generates tokens for each keyword and symbols: the token contains the information- type of keyword and its location in the code. Using these tokens, an AST(short for Abstract Syntax Tree) is created and analysed. What compiler actually checks here is whether the code is lexically meaningful i.e. does the 'sequence of keywords' comply with the language rules? As suggested in previous answers, you can see it as the grammar of the language(not the sense/meaning of the code). Side note: Syntax errors are reported in this phase.(returns tokens with the error type to the system)

  • Semantics: Now, the compiler will check whether your code operations 'makes sense'. e.g. If the language supports Type Inference, sematic error will be reported if you're trying to assign a string to a float. OR declaring the same variable twice. These are errors that are 'grammatically'/ syntaxially correct, but makes no sense during the operation. Side note: For checking whether the same variable is declared twice, compiler manages a symbol table

So, the output of these 2 frontend phases is an annotated AST(with data types) and symbol table.

Understanding it in a less technical way

Considering the normal language we use; here, English:

e.g. He go to the school. - Incorrect grammar/syntax, though he wanted to convey a correct sense/semantic.

e.g. He goes to the cold. - cold is an adjective. In English, we might say this doesn't comply with grammar, but it actually is the closest example to incorrect semantic with correct syntax I could think of.

Solution 9 - Syntax

He drinks rice (wrong semantic- meaningless, right syntax- grammar)

Hi drink water (right semantic- has meaning, wrong syntax- grammar)

Solution 10 - Syntax

Syntax: It is referring to grammatically structure of the language.. If you are writing the c language . You have to very care to use of data types, tokens [ it can be literal or symbol like "printf()". It has 3 tokes, "printf, (, )" ]. In the same way, you have to very careful, how you use function, function syntax, function declaration, definition, initialization and calling of it.

While semantics, It concern to logic or concept of sentence or statements. If you saying or writing something out of concept or logic, then you are semantically wrong.

Solution 11 - Syntax

The syntax of a programming language is the form of its expressions, statements, and program units. Its semantics is the meaning of those expressions, statements, and program units. For example, the syntax of a Java while statement is

while (boolean_expr) statement

The semantics of this statement form is that when the current value of the Boolean expression is true, the embedded statement is executed. Then control implicitly returns to the Boolean expression to repeat the process. If the Boolean expression is false, control transfers to the statement following the while construct.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhaccksView Question on Stackoverflow
Solution 1 - SyntaxJeff NView Answer on Stackoverflow
Solution 2 - SyntaxdoctorloveView Answer on Stackoverflow
Solution 3 - SyntaxthumbtackthiefView Answer on Stackoverflow
Solution 4 - SyntaxBasile StarynkevitchView Answer on Stackoverflow
Solution 5 - Syntaxmeaning-mattersView Answer on Stackoverflow
Solution 6 - SyntaxTullochgorumView Answer on Stackoverflow
Solution 7 - SyntaxKobina EboView Answer on Stackoverflow
Solution 8 - SyntaxVedant PanchalView Answer on Stackoverflow
Solution 9 - SyntaxHari ThapliyalView Answer on Stackoverflow
Solution 10 - SyntaxHafiz Shehbaz AliView Answer on Stackoverflow
Solution 11 - SyntaxBoraKurucuView Answer on Stackoverflow