编程语言的语法与语义

编程语言的语法与语义

摘自《 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.

你可能感兴趣的:(编程语言)