编程语言中的语法和语义有什么区别? [英] What is the difference between syntax and semantics in programming languages?

查看:132
本文介绍了编程语言中的语法和语义有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

语法语义在编程语言(如C、C++)中有什么区别?

What is the difference between syntax and semantics in programming languages (like C, C++)?

推荐答案

语法是关于语言的结构或语法.它回答了这个问题:我如何构造一个有效的句子?所有语言,甚至英语和其他人类(又名自然")语言都有语法,即定义句子是否正确构造的规则.

Syntax is about the structure or the grammar of the language. It answers the question: how do I construct a valid sentence? All languages, even English and other human (aka "natural") languages have grammars, that is, rules that define whether or not the sentence is properly constructed.

以下是一些 C 语言的语法规则:

Here are some C language syntax rules:

  • 用分号分隔语句
  • 将 IF 语句的条件表达式括在括号内
  • 用大括号将多个语句组合成一个语句
  • 必须在第一个可执行语句之前声明数据类型和变量(C99 中已删除此功能.C99 和后者允许混合类型声明.)

语义是关于句子的含义.它回答了以下问题:这句话有效吗?如果有,这句话是什么意思?例如:

Semantics is about the meaning of the sentence. It answers the questions: is this sentence valid? If so, what does the sentence mean? For example:

x++;                  // increment
foo(xyz, --b, &qrs);  // call foo

是语法上有效的 C 语句.但它们是什么意思?尝试将这些语句转换为可执行的指令序列是否有效?这些问题是语义学的核心.

are syntactically valid C statements. But what do they mean? Is it even valid to attempt to transform these statements into an executable sequence of instructions? These questions are at the heart of semantics.

考虑第一条语句中的 ++ 运算符.首先,尝试这样做是否有效?

Consider the ++ operator in the first statement. First of all, is it even valid to attempt this?

  • 如果 x 是浮点数据类型,则该语句没有意义(根据 C 语言规则),因此即使该语句在语法上是正确的,它也是一个错误.强>
  • 如果 x 是指向某种数据类型的指针,则该语句的含义是将 sizeof(某种数据类型) 添加到地址 x 处的值并存储结果放入地址 x 处的位置".
  • 如果 x 是标量,则语句的含义是将地址 x 处的值加 1,并将结果存储到地址 x 处的位置".
  • If x is a float data type, this statement has no meaning (according to the C language rules) and thus it is an error even though the statement is syntactically correct.
  • If x is a pointer to some data type, the meaning of the statement is to "add sizeof(some data type) to the value at address x and store the result into the location at address x".
  • If x is a scalar, the meaning of the statement is "add one to the value at address x and store the result into the location at address x".

最后,请注意某些语义无法在编译时确定,因此必须在运行时进行评估.在 ++ 运算符示例中,如果 x 已经处于其数据类型的最大值,那么当您尝试向其加 1 时会发生什么?另一个例子:如果你的程序试图解引用一个值为 NULL 的指针会发生什么?

Finally, note that some semantics cannot be determined at compile-time and must therefore must be evaluated at run-time. In the ++ operator example, if x is already at the maximum value for its data type, what happens when you try to add 1 to it? Another example: what happens if your program attempts to dereference a pointer whose value is NULL?

总而言之,句法是一个概念,它只关心句子是否对语言的语法有效.语义是关于句子是否具有有效含义.

In summary, syntax is the concept that concerns itself only whether or not the sentence is valid for the grammar of the language . Semantics is about whether or not the sentence has a valid meaning.

这篇关于编程语言中的语法和语义有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆