赞
踩
语法和语义之间的差异
Here you will learn about difference between Syntax and Semantics with example.
在这里,您将通过示例了解语法和语义之间的区别。
The both terms might seem the same things but they are not, they are just interrelated concepts, which even exist as individuals.
这两个术语看似相同,但事实并非如此,它们只是相互关联的概念,甚至作为个体存在。
What is Syntax?
什么是语法?
When we talk about a language (not just a programming language), let’s say English (most common one), there are certain rules of grammar we have to follow if we want to practice this language in the real world. These rules actually define the structure of our sentence.
当我们谈论一种语言(不仅是一种编程语言)时,例如说英语(最常见的一种语言),如果要在现实世界中练习这种语言,必须遵循某些语法规则。 这些规则实际上定义了我们句子的结构。
A Simple rule: Noun + Verb + object
一个简单的规则: 名词+动词+宾语
To make sense and let others understand what we want to convey in certain language, we need these rules. They are the grammar of a language that the user must follow.
为了使人们理解并让其他人理解我们想要用某种语言传达的内容,我们需要这些规则。 它们是用户必须遵循的一种语言的语法。
Same is in programming, if we talk about any language like C, C++, Java, etc there are certain rules that we follow when coding to make the compiler understand what we want to convey.
在编程中也是如此,如果我们谈论诸如C,C ++,Java等任何语言,那么在进行编码时都会遵循某些规则,以使编译器理解我们想要传达的内容。
Some of these rules are:
其中一些规则是:
So these actually form a structure to write something that is correct and agrees with the grammar of a language.
因此,这些实际上形成了一种结构,可以编写正确的东西并符合某种语言的语法。
This grammar of a language is called Syntax of that language.
一种语言的语法称为该语言的语法。
What is Semantics?
什么是语义学?
Semantics, on the other hand, deals with the logical part of a sentence/statement. Let’s say the following English sentence:
另一方面,语义学处理句子/陈述的逻辑部分。 让我们说以下英语句子:
Dog fly aeroplane.
狗飞飞机。
This sentence is correct on the part of the syntax of the English Grammar
这句话在英语语法的语法上是正确的
A Simple rule: Noun + Verb + object
一个简单的规则: 名词+动词+宾语
But it’s not logically correct (if are not being sarcastic).
但这在逻辑上是不正确的(如果不是讽刺的话)。
This logical meaning of a sentence that defines its validity is what we call Semantics of that sentence.
定义其有效性的句子的逻辑含义就是我们所说的句子的语义。
Semantics deals with the meaning of a sentence. In programming, if call a function that can only work on two values of a ‘char’ variable.
语义处理句子的含义。 在编程中,如果调用只能在“ char”变量的两个值上工作的函数。
- func(char c){
- if(c == 'y'){
- //DO SOMETHING
- }
- else if(c == 'n'){
- //DO ANOTHERTHING
- }
- }
And we call this function with some other value:
我们用其他一些值调用此函数:
func(‘a’);
func('a');
This statement is perfect with the syntax aspect but is logically wrong i.e., semantically wrong.
该语句在语法方面是完美的,但在逻辑上是错误的,即在语义上是错误的。
Lets know about syntax vs semantics in tabular form.
让我们以表格形式了解语法与语义。
Syntax | Semantics |
The grammar of a language is called Syntax. | Semantics deals with the meaning of a sentence. |
Syntax errors can be caught at compilation time and are easy to track. | Semantics errors are hard to find. They are mostly encountered at runtime, or when the results do not match with the expectations. |
句法 | 语义学 |
语言的语法称为语法。 | 语义处理句子的含义。 |
语法错误可以在编译时捕获,并且易于跟踪。 | 语义错误很难发现。 它们通常在运行时或结果与预期不符时遇到。 |
So this was the major difference between semantics and syntax of a language, or even in general to anything.
因此,这是语言的语义和语法之间的主要差异,甚至对任何事物而言都是如此。
翻译自: https://www.thecrazyprogrammer.com/2018/05/difference-between-syntax-and-semantics.html
语法和语义之间的差异
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。