As a computer scientist and a writing hobbyist, I really ought to know these terms' meanings for memory. Can anyone clarify the difference between syntax
and semantics
, and provide some examples? For example, where does punctuation fall? Spelling? What about sentence (or code) structure?
-
2I think you mean "punctuation"– simchonaJul 31, 2011 at 22:04
-
2Semantics and syntax.– simchonaJul 31, 2011 at 22:06
-
3This was already covered here. More at Colorless green ideas sleep furiously.– prashJul 31, 2011 at 22:21
4 Answers
I can only answer the computer question, although it has the exact same meaning for the English language. In short: Syntax is structure, and semantics is meaning.
Programming languages are written based on a grammar (just like English.) Grammars might say something like "If statements always have the form: if (condition) then (statement)." If you write something that follows the grammar perfectly, then it is syntactically correct, but may or may not be semantically correct, or semantically meaningful.
In some arbitrary simple language, the statement:
int i = "hello"
is syntactically correct, but not semantically correct, since it has no meaning even though it correctly follows the structure of the language.
A very common example is Chomsky's statement "Colorless green ideas sleep furiously", which follows the grammar of the English language but is semantically incorrect because it contains several contradictions -- colorless things cannot be green, for instance. You could of course argue this poetically to have some meaning (I would probably hate you), but that's beyond the scope of this discussion.
In English, "I no like!" is grammatically incorrect (syntactically incorrect), but is not semantically incorrect since it does imbue some meaning.
In coding, this is more muddy; it's hard to say whether a statement like "i (int) = 3" is semantically correct even though it's syntactically correct. There's really no meaning in this distinction, either. Generally, a statement has to be syntactically valid before it even has a chance of being semantically valid.
-
-
1But
int i = "hello"
would be bad syntax too in most languages? I don't think there is one where you specify the type but do not have a syntax rule on what can follow?– NanneAug 1, 2011 at 8:54 -
That's why I said arbitrary language. In the compiler I wrote, the grammar for assignment expressions looked like: [Type] [text] = [Value]. So it would be good syntax. I wouldn't be surprised if other, smarter languages had rules like int [text] = [int value], in which case it would be a syntactical issue.– JeremyAug 1, 2011 at 12:26
-
I'm not sure which paradigm you have in mind, but int i = "hello" is both syntactically and semantically valid and is parsed and processed without a whimper of protest, at least in some contexts. Run it and see.– KrisMar 10, 2014 at 8:14
Semantics is:
Semantics ... is the study of meaning. It focuses on the relation between signifiers, such as words, phrases, signs and symbols, and what they stand for, their denotata.
I think your confusion comes from the last phrase, that is, does puncutation come under "signs and symbols"? The answer is no, because, semantics deals with the meaning, so punctuation would not come under this, as punctuation does not have a meaning, and does not denote something.
Punctuation comes under, syntax, which is:
syntax ... is the study of the principles and rules for constructing sentences in natural languages.
Thus, in order to construct a sentences, you will need to follow certain rules, including rules for using punctuation. Punctuation comes under syntax.
What doe spelling come under? I would say, "morphology":
morphology is the identification, analysis and description of the structure of morphemes and other units of meaning in a language like words, affixes, and parts of speech and intonation/stress...
Thus, morphology, deals with how words are formed, and spelling would come under this.
Hope that helps.
All sources are from Wikipedia.
-
1Actually, when talking about human language, there's a case for saying that spelling, and probably also punctuation, are none of these. The Serbian language can be written in Cyrillic or Roman letters, but when you change from one to the other you are not changing either the syntax or the morphology, simply the way that the words are written down on paper. Jul 31, 2011 at 23:20
-
1Punctuation does have meaning. Doesn't it? Punctuation does have meaning, doesn't it.– Kit Z. Fox ♦Aug 1, 2011 at 2:10
Syntax is the symbology and grammar of a language, which tells you the rules for constructing well-formed sentences of the language. Semantics concerns what those statements mean.
# Ruby
if a and b
foo()
end
-- Lua
if a and b then
foo()
end
# C/C++/Java
if (a && b)
foo();
# Python
if a and b:
foo()
These all mean the same thing, which is to say they have the same semantics, but they are described using slightly different syntax.
Some languages offer alternative syntaxes for commonly used language constructs. For instance, in Lua the following are exactly equivalent; i.e. they have identical semantics:
person.favorite = 'donut'
person['favorite'] = "donut"
As do the following:
function add(a,b) return a+b end
add = function(a,b) return a+b end
As far as computer science applies, simple answer is...
Syntax errors are those errors caused because you did not follow the rules of the language:
For C++
for (int i = 0; i < 10; i++)
{ printf("%d\n", i) } // You forgot the semicolon
Semantics errors are those errors caused due to mispelling and such
For C++
for (int i = 0; i < 10; i++)
{ priNtf("%d\n", i); } // You capitalized the "n"