语法和语义之间的差异_语法和语义之间的区别

语法和语义之间的差异

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:

其中一些规则是:

  1. Command terminates with a semi colon.

    命令以半冒号终止。
  2. Code block starts with a opening curly brace “{“ and ends with a closing curly brace “}”.

    代码块以大括号“ {”开头,以大括号“}”结尾。
  3. for loop has three statements inside round brackets“()”, first one is initialise, second one is condition and third is variable update.

    for循环在圆括号“()”中包含三个语句,第一个是初始化,第二个是条件,第三个是变量更新。

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.

让我们以表格形式了解语法与语义。

语法和语义之间的区别 (Difference between Syntax and Semantics)

SyntaxSemantics
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

语法和语义之间的差异

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值