I've always thought that referring to the syntax of a language was the same as referring to the semantics of a language. But I've been informed that apparently that's not the case. What's the difference?
|
|
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). |
|||||||||||||||||||
|
|
Semantics describe the logical entities of a programming language and their interactions. Syntax defines how these are expressed in characters. For example, the concept of pointer arithmetic is part of C's semantics; the way the Sometimes, two languages share part of their semantics, but the syntax differs wildly (e.g. C# and VB.NET - both use value types and reference types, but the characters you type to define them are different); in other cases, two languages are syntactically similar, but the semantics don't match up (consider Java vs. JavaScript, where the similarities often confuse beginners). |
|||||||
|
|
Actually there are not two levels but three:
|
|||||||||||||||
|
|
You did not specify whether you only refer to programming languages or to general languages used in programming, so my answer is about data languages (such as XML, RDF, data type systems etc.): Brian L. Meek in his seven golden rules for producing language-independent standards (1995) writes that "one language's syntax can be another's semantics". He refers to the words "syntax" and "semantic" used in data description: so if you stumble upon these words in a specification of some data format, you should better replace both words with "Potrzebie" to make clear that you must work out the meaning for yourself. The relation between syntax and semantic, at least in exactly specified data, can better be described by the term "encoding". Semantic is encoded in syntax. As recordings can be nested, one language's syntax is another's semantics. If one goes beyond the realm of data, this nesting can be virtually infinite, as described by Umberto Eco as "unlimited semiosis". To give a an example:
People usually stop at some level and take it as semantic, but in the end there is no final semantic unless some human being interprets the data in his mind. As soon as one tries to express semantic in form of data, it becomes syntax. |
|||
|
|
|
If it can be described in BNF (Backus-Naur Form), it's syntax. If it can't, it's not. Semantics, on the other hand, is about the meaning of a program (or other chunk of source code). And sometimes the line between the two can be blurry. One way to understand the distinction is to look at the kinds of errors you get when your program's syntax or semantics is incorrect. A syntax error is a failure of the source code to match the language grammar, for example, not having a semicolon where one is required. A semantic error is a failure to satisfy other language requirements (what C, for example, calls "constraints"); an example might be writing (Logical errors, such as using 1 where 2 would be correct, are not generally detectable by the compiler -- though in some cases a compiler can warn about questionable code.) |
||||
|
|
|
Syntax is how you arrange a language's tokens. Semantics is what those tokens mean (usually, what a particular arrangement of tokens means). |
|||
|
|
|
Very short example with "plain c":
In this example, the syntax for the "-" token is the same, but, it has a different meaning ("semantic), depending where its used. In the "x" assignament, "-" means the "substraction" operation, In the "y" assignament, "-" means the "negative sign" operation. |
|||||
|
|
Syntax is what the (lexical) symbols say. Semantics is what they mean. C#: C#: |
|||
|
|
|
I will explain it to you with a simple example in the language
Is a syntactically correct statement. It has a noun, a verb, etc. But semantically it is wrong, because this statement has no conceivable or correct meaning. |
||||
|
|
|
||||
|
|
|
Syntax is what the computer understands, semantics is what the human understands. A compiler/interpreter doesn't care a whit about your design, and in any code compiled down to machine level you'd have a hard time deducing the design. Developers care about design because a good design is about reducing complexity by abstracting complex behaviors and interactions, and different kinds of problems lend themselves to different semantics. The choice of language is largely about how easily and efficiently the semantics you want to use can be expressed in its syntax. |
|||||||||||||||||
|
