博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
语法和语义之间的差异_语法和语义之间的区别
阅读量:2510 次
发布时间:2019-05-11

本文共 3538 字,大约阅读时间需要 11 分钟。

语法和语义之间的差异

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)

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.

因此,这是语言的语义和语法之间的主要差异,甚至对任何事物而言都是如此。

翻译自:

语法和语义之间的差异

转载地址:http://taggb.baihongyu.com/

你可能感兴趣的文章
Java学习之i/o系统
查看>>
laravel 视图组件
查看>>
[转发]Oauth 1.0 1.0a 和 2.0 的之间的区别有哪些?
查看>>
animation
查看>>
mysqldump的几个主要选项探究
查看>>
程序员修炼之路
查看>>
动态规划的基本模型
查看>>
php生成xml文件方法
查看>>
.Net邮箱的使用
查看>>
Go语言引用类型
查看>>
nodejs学习
查看>>
顺序表的实现
查看>>
青蛙跳圆周
查看>>
SGU 102
查看>>
创建http服务器
查看>>
memcache服务端与客户端的安装部署
查看>>
坐标间获取两点距离
查看>>
递归方法-库存填充
查看>>
第五节20181117
查看>>
flex布局
查看>>