“语义”的简单定义因为它通常与编程语言/API 相关使用?
今天我突然想到,虽然我在提到语言元素和命名约定时已经采用并且不经常使用“语义”一词,但我对正式定义没有任何感觉。
我试图在编程领域找到一个正式的定义,这让我的目光变得呆滞。
我从我遇到的上下文以及它在语言学方面更常见的用法中了解了它的含义,并且我通常使用该术语来指语言元素,或者命名法对其所命名的意图、行为或功能的忠实度。
不过,这个定义是我自己的想法。我主修人类学/英语,从未上过计算机科学课程。这是CS里教的吗?
是否有一个更准确的定义可以归结为一两个陈述,而不是一篇详尽无遗的文章?
It occurred to me today that although I've adopted and don't infrequently use the term "semantics" when referring to language elements and naming conventions, I don't have any sense of a formal definition.
My attempt to find a formal definition in the programming domain made my eyes glaze over.
I have a sense of its meaning from the contexts in which I've encountered it, and from its more common usage with respect to linguistics, and I typically use the term to refer to the meaning or expressiveness of the language element, or the fidelity of nomenclature to the intent, behaviour, or function of that which it names.
This definition, though, is my own idea. I was an Anthropology/English major and have never taken a Computer Science course. Is this taught in CS?
Is there a more accurate definition that can be boiled down to a statement or two, rather than an exhaustive and exhausting article?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
定义摘自此处 是编程语言的语义描述语法和计算模型之间的关系。。这几乎正是您推断的定义。症结所在,也是让你目光呆滞的地方,正是计算模型的精确定义。
有许多不同的形式计算模型,每种模型都会导致不同形式的语义。操作语义可能最接近大多数人非正式地建模语义的方式,让每个代码片段通过解释器运行并更改抽象例程的状态。这种方法在许多并发性或不确定性等模型中都失败了,因此还有其他语义更适合这些情况。
A definition cribbed from here is The semantics of a programming language describe the relationship between the syntax and the model of computation.. This is pretty much exactly what your inferred definition was. The sticking point, and what made your eyes glaze over is precisely defining the model of computation.
There are many different formal models of computation and each leads to a different form of semantics. Operational Semantics is probably closest to the way most people informally model the semantics, having each fragment of code run through an interpreter and changing the state of an abstract routine. This approach breaks down in many models where concurrency or nondeterminism, etc., so there are other sematics better suited to these situations.
这是语言元素在计算方面的正式含义(通常,这是操作语义)。这意味着它表达了您的语言的术语的有效功能,假设一种依赖于我们正在讨论的语义的基础模型。
正如您从维基百科页面中看到的,您主要有 3 种语义:
+
:从堆栈中弹出两个元素并压入总和。这是NOT正式的,也NOT您应该如何真正考虑它只是为了给你一个想法)。这是描述“正常”编程语言语义最常用的一种。例如,对于 Java,对于每个可能的术语,您可以拥有一系列 JVM 指令,旨在执行该序列来对该术语进行建模。 可能当您询问语义的含义时,这就是您正在寻找的。+
关联的函数 f,其中包含术语公理语义 是一种注释语言术语的方法,表达它们如何改变您想要通过程序验证的某些逻辑公式的有效性。您应该考虑阅读此,因为所使用的推理规则和公理与您开发此类的方式类似语义,但它以实用的方式解释从这个描述中,您了解到语义是在上下文中明确定义的东西,并且您需要指定的上下文,否则您无法为您的语言提供其术语的正式定义。
It's the meaning of the language elements in terms of what they formally mean in terms of computation (usually, and this is the operational semantics). This means that it expresses what a term of your language effectively does assuming an underlying kind of model that depends on which semantic we are talking about.
As you can see from wikipedia page you mainly have 3 kinds of semantics:
+
: pops two elements from the stack and push the sum. This is NOT formal and it is NOT how you should really consider it, it's just to give you an idea). This is the most used one to describe semantics of "normal" programming languages. For example for Java you could have, for every possible term, a sequence of JVM instruction meant to be executed to model that term. Probably when you asked for the meaning of semantics this is the one you were looking for.+
that contains what is the semantic (effective meaning) of the termFrom this decription you understand that a semantic is something well defined inside a context, and you need a specified context otherwise you couldn't give you language a formal definition of what its terms do..
从编程语言的理论和实践的角度来看,语言元素具有语义。命名约定则不然。语义与对任何事物的“忠诚度”无关,除非实现是正确的,有时它被称为“忠实于语义”。
除此之外,很难一概而论,因为语义有很多不同的风格。
还有其他类型的语义,称为“操作语义”,其中给定一个程序,语义告诉您该程序将如何在抽象机器上执行(或者在另一种变体中,语义不说明程序将如何执行,而只说明结果会是什么)。
有一个“公理语义”,它大致是关于你可以证明关于单个程序的事实。公理语义是有效证明技术的集合。由实现来确保所有可证明的声明都是真实的。
还有“静态语义”,它广泛地表示在编译时强加的任何要求,以使程序被认为是“良好”或“格式良好”。像“变量必须在使用之前定义”之类的东西是静态语义。但大多数情况下,当人们说静态语义时,他们指的是类型检查。
最后,可以讨论抽象数据类型、类或接口等的“语义”。这种用法比较宽松,但它们归结为允许哪些行为的规范。我建议您在这种情况下避免使用“语义”一词,而使用“合同”或“规范”一词。它将避免混乱的事情。
评论:尝试将一个复杂的主题归结为一两句话并不总是有帮助的。当谈到编程语言时,不要在维基百科上寻找好的信息。维基百科的本意是好的,但它们往往很复杂、令人困惑,或者根本就是错误的。
From a perspective of the theory and practice of programming languages, language elements have semantics. Naming conventions don't. And semantics has nothing to do with 'fidelity' to anything, except perhaps that if an implementation is correct it is sometimes called "faithful to the semantics."
Beyond that it's hard to generalize because there are so many different styles of semantics.
There are other styles of semantics, called "operational semantics", where given a program, the semantics tells you how that program will be executed on an abstract machine (or in another variant, the semantics says not how the program will be executed but only what the result will be).
There is "axiomatic semantics" which is roughly about what facts you can prove about individiual programs. The axiomatic semantics is a collection of valid proof techniques. It's up to the implementation to ensure that all provable claims are true.
There is also "static semantics", by which is meant broadly any requirements imposed at compile time for the program to be considered "good" or "well formed". Stuff like "variables have to be defined before they are used" is static semantics. But mostly when people say static semantics they mean type checking.
Finally, it is possible to speak of the "semantics" of an abstract data type, a class, or an interface, among others. This usage are a good deal looser, but they boil down to a specification of what behaviors are permissible. I advise you to avoid the word "semantics" in this context and to use the word "contract" or "specification" instead. It will avoid confusing things.
Commentary: it's not always helpful to try to boil down a complex subject to one or two sentences. And when it comes to programming languages, don't look for good information on Wikipedia. The Wikipedians mean well, but too often they are complex, confusing, or just plain wrong.