在程序设计中经常会使用到Syntax和Semantics,那么这两者分别是什么意思,又有什么区别呢?
Syntax是语法,Semantics是语义;从字面的意思来看,前者注重的是“法则”,而后者则偏重于“涵义”。以SQL语句为例,在Oracle中,解析Sql语句要先经过语法检查,然后再进行语义检查。语法检查时,会判断你写的SQL语句中是否有错别字,如把select写成了selct等;而语义则是判断你写这个SQL的目的是什么——是要查询,还是个性数据等。
Code
What is the difference between syntax and semantics.
This question could be answered merely by taking a definition out of
any dictionary or cognitive psychology text book. John R. Anderson
defines the two in his book " Cognitive psychology and its
implications"(1995) as,
Semantics- Grammatical rules for assigning meaning to a sentence.
Syntax- Grammatical rules for specifying correct word order and
inflectional structure in a sentence.
Doing this does not really give a clear explanation of the
differences between the two. It is beter explained if the features of
the two are explored more thoroughly.
Word order is the basic principal of syntax, those trying to
understand what is written use the syntactic cues of word order to
help give the sentence structure and meaning. Word order gives a
sentence the correct intonnation especially with the use of funtion
words such as,a and who, due to this its meaning becomes much
clearer. If the words of a sentence were not in the correct order
there would be no syntactic cues as to the meaning of the sentence.
This point leads on to the difference between syntax and semantics.
Semantics are an individuals own interpretation of the meaning of a
"sentence" based on their prior knowledge. Therefore a sentence that
seemingly makes no syntactic sense, can have meaning when using
semantic cues. The sentence "Baby milk drinks" does not have a
syntactic meaning, but through semantics most people would interpret
it as meaning " Baby drinks milk " as our prior knowledge tells us
that a baby drinks milk, and therefore we can find a meaning from the
key words.
The difference between syntax and semantics is that syntax is only
concerned with what is linguistically and grammatically correct,
semantics requires all ones prior knowledge which, as Anderson
states, " goes far beyond anything which is language specific".