编程语言的语法与语义

编程语言的语法与语义

摘自《Formal Syntax and Semantics of Programming Languages 》by Kenneth Slonneger / Barry L. Kurtz
刘建文略译(http://blog.csdn.net/keminlau

Chapter 1
SPECIFYING SYNTAX

Language provides a means of communication by sound and written symbols. Human beings learn language as a
consequence of their life experiences, but in linguistics-the science
of languages-the forms and meanings of languages are subjected to a more
rigorous examination. This science can also be applied to the subject
of this text, programming languages. In contrast to the natural
languages, with which we communicate our thoughts and feelings,
programming languages can be viewed as artificial languages defined by
men and women initially for the purpose of communicating with computers
but, as importantly, for communicating algorithms among people.

语言以声音和文字符号的形式提供交流的渠道。人类一般通过生活的体验习得语言,但是在作为一门科学的语言学中,语言的形式和及其含义必须被严格考究
(rigorous
examination)。同样的科学对待当然也被应用到本书的主题--编程语言。和自然语言用以交流想法和感觉不同,编程语言可被看成是一个人工语言,
起初定义为为了人机交流,不过现在更重要的是为了在程序员之间交流程序算法。

Many of the methods and much of the terminology of linguistics apply
to programming languages. For example, language definitions consist of
three components:

语言学的很多方法和术语应用于编程语言。比如,语言定义的三个部分:

1. Syntax refers to the ways symbols may be combined to create
well-formed sentences (or programs) in the language. Syntax defines the
formal relations between the constituents要素 of a language, thereby
providing a structural description of the various expressions that make
up legal strings in the language. Syntax deals solely with the form and
structure of symbols in a language without any consideration given to
their meaning.

语法指用字符组合成合法语句的规则集(KEMIN:字符先通过词法
组词或字串,再由代表不同句子成分的词组合成语句)。语法定义语言的各种要素间的形式关系(KEMIN:什么要素,什么关系?),因此给出了语言中各种不
同的合法的语句的结构描述。语法只关注句法结构,不管其含义,那是语义的事。

2. Semantics reveals透露 the meaning of syntactically valid strings in
a language. For natural languages, this means correlating sentences and
phrases with the objects, thoughts, and feelings of our experiences.
For programming languages, semantics describes the behavior that a
computer follows when executing a program in the language. We might
disclose揭露 this behavior by describing the relationship between the
input and output of a program or by a step-by-step explanation of how a
program will execute on a real or an abstract machine.

语义指定一条合法语句的含义。对自然语言来说,短语和语句的含义指相
关的对象、思想和经验感觉;而对编程语言来说,语义描述了计算机执行一个程序时所表现的行为。揭示语义的行为包括描述程序输入输出之间的关系,逐步解说程
序如何在真实或抽象机器的执行过程等。(KEMIN:语义都需要某种实现的。而计算机实际行为,如逻辑运算、给内存保存数据等实现了编程语言的抽象语义)
(KEMIN:在这里,语义的存在及重要性是无可非议的了,问题还不清楚它与算法过程有什么关系。)

3. Pragmatics alludes to those aspects of language that involve the
users of the language, namely psychological and sociological phenomena
such as utility, scope of application, and effects on the users. For
programming languages, pragmatics includes issues such as ease of
implementation, efficiency in application, and programming methodology.

语用涉及了语言使用者的各种方面的内容,包括心理学上、社会学现象,比如语言的实用性、应用范围和对使用者的作用。对编程语言来说,语用问题指实现的简易性、应用的效率和编程方法论。

Syntax must be specified prior to semantics since meaning can be
given only to correctly formed expressions in a language. Similarly,
semantics needs to be formulated before considering the issues of
pragmatics, since interaction with human users can be considered only
for expressions whose meaning is understood. In the current text, we are
primarily concerned with syntax and semantics, leaving the subject of
pragmatics to those who design and implement programming languages,
chiefly compiler writers. Our paramount goal is to explain methods for
furnishing装备 a precise definition of the syntax and semantics of a
programming language.

语法必须先于语义给出,因为只能给正确形式的表达式指定含义;同样,语义也需要明确后才考虑语用问题,因为使用者只有明白表达式的含义后才能使用语言进行
交流。本书主要讲述语法和语义,语用部分留给语言的设计者和实现者,一般是编译器开发人员。我们的第一目标就是解释装备(furnishing)一个编程
语言的语法和语义的精确定义的方法。

We begin by describing a metalanguage for syntax specification called
BNF. We then use it to define the syntax of the main programming
language employed in this text, a small imperative language called Wren.
After a brief look at variants of BNF, the chapter concludes with a
discussion of the abstract syntax of a programming language.

我们先介绍描述语法规范的一种元语言——BNF(巴克斯-诺尔范式)。我们用它来定义本书选用的语言Wren的语法,Wren是种很小的命令式语言。在概览BNF的变种后,我们总结编程语言的抽象语法。

At the simplest level, languages are sets of sentences, each
consisting of a finite sequence of symbols from some finite alphabet.
Any really interesting language has an infinite number of sentences.
This does not mean that it has an infinitely long sentence but that
there is no maximum length for all the finite length sentences. The
initial concern in describing languages is how to specify an infinite
set with notation that is finite. We will see that a BNF grammar is a
finite specification of a language that may be infinite.

时间: 2024-08-01 21:47:40

编程语言的语法与语义的相关文章

编程语言的语法和语义

http://blog.csdn.net/gengzhikui1992/article/details/50762309 为了完整地定义编程语言,我们需要: 语法,描述程序看起来是什么样的: 语义,描述程序的含义. 语法 语法的定义 每一种编程语言都有一系列规则,描述在那种语言中什么样的字符串被认为是有效程序.这些规则定义了这种语言的语法.通过语言的语法规则,我们能把像 y = x + 1 这样可能有效的程序与像 >/;x:[email protected] 这样毫无意义的字符串区分开. 抽象语

C++ 的二进制语法与语义

二进制的语法 C/C++ 默认数字使用十进制,八进制使用前缀 0, 十六进制使用前缀 0x 或 0X,二进制常数的提议被否决(引用 C 语言程序原理国际标准 的 6.4.4.1 章节字段 "A proposal to add binary constants was rejected due to lack of precedent and insufficient utility."),一直没有二进制的表示方法, GCC 使用 0b/0B 前缀作为扩展,其实很多编译器都有这个扩展的,

语法和语义和错误;

每一种编程语言都有自己独特的语法规则. 一种语言的语法,准确,严格地规定了如何组合该语言中的`词汇以形成语句. 必须严格遵守这些规则才能创建程序. 例如,"标识符的名称不能一数字开头" 重点概念:语法规则规定了程序编写的形式,语义则是指每条语句的含义 重点概念:程序员必须对程序的正确性和可靠性负责 在程序开发过程中会遇到三种不同类型的错误: 编译时时错误; 运行时错误; 逻辑错误; 只要存在编译时的错误,就不会得到可执行的程序代码. 在JAVA程序中,许多运行时错误成为异常,异常可被捕

词法、语法与语义相关知识

概念 http://www.juweixin.com/t/detail/148383 语法到底是什么?有人说语法是语言里的法律.法规:有人说语法是框架结构:还有人说语法就是对前人语言习惯的一种总结.以上这些说法都对,但是又不全对. In linguistics, grammar is the set of structural rules governing the composition of clauses, phrases, and words in any given natural l

语法、语义与哲学

语法:语言要素组成语句的规则:是部分如何和成整体的原则: 物质的组成规则. 语义:描述的是信息流通的过程:本质是信息的可理解性. http://www.juweixin.com/t/detail/148383 语法到底是什么?有人说语法是语言里的法律.法规:有人说语法是框架结构:还有人说语法就是对前人语言习惯的一种总结.以上这些说法都对,但是又不全对. In linguistics, grammar is the set of structural rules governing the com

学习编程之Python篇(一)

第一次接触编程,你将面对两大难题: 1.  对所要使用的编程语言的语法和语义不甚了了. 2.  不知道如何通过编程来解决问题. 作为一名新手,你会尝试同时来解决这两个难题:一边熟悉编程语言的语法语义,一边考虑如何靠编程解决问题.这是一个循序渐进的过程,万事开头难,务必保持耐心,切勿操之过急. 学习编程其实没有什么捷径可走,最好的方法就是反复操练,聆听规则,讨论方法,都不如真正做点什么. 在掌握了一些编程语言的语法语义之后,接下来的难题就是怎样才能写出好的程序.那么,我们首先来看看什么是好的程序.

understand软件使用教程(转)

源代码阅读工具(Scientific Toolworks Understand)的特色 1.支持多语言:Ada, C, C++, C#, Java, FORTRAN, Delphi, Jovial, and PL/M ,混合语言的project也支持2.多平台: Windows/Linux/Solaris/HP-UX/IRIX/MAC OS X3.代码语法高亮.代码折迭.交叉跳转.书签等基本阅读功能.4.可以对整个project的architecture.metrics进行分析并输出报表.5.可

编程语言的进化

早上读到了一篇文章<Lisp的永恒之道> 这一篇文章解决了我一直以来对Lisp编程语言的疑惑,加深了对DSL(领域编程语言),同时对编程语言的理解也越来越深入.写一篇文章的目的就是从主要编程语言的进化来看看编程语言发展的过程以及背后的设计哲学,当然,这个只是我自己的认识,如果有不足之处,请博友指教. 机器语言 最早的编程语言是卡带,也就是机器语言,这种语言没有语义和语法,操作符和数据混合在一起,偏向于具体的设备,依赖觉得硬件. 汇编语言 随后,汇编语言就出现了,汇编语言定义了基本的语法结构,定

Atitit.编程语言and&#160;自然语言的比较and&#160;编程语言未来的发展

Atitit.编程语言and 自然语言的比较and 编程语言未来的发展 1. 单词的间隔靠空格,编程的单词的间隔靠分界符..1 2. 语句分界符:自然语言使用逗号,编程语言使用分号1 3. 换行1 4. 段落and  fun method2 5. 上下文相关2 6. 操作泛型化2 7. 动词和名词之间的互换性2 8. 词类的判断3 8.1.1. 词类划分的形式标志3 8.1.2. 自然语言中的指代词(你.我.他.它等等)好比编程语言中的“指针(point)”.4 8.2. 这类疑问句似乎在编程语