C语言表达式与语另类解释

来源:互联网 发布:云尚数据怎么样 编辑:程序博客网 时间:2024/05/08 01:05

C语言表达式与语另类解释


这段日子断断续续的研究C语言,前一时间批判了传统的C语言运算符分类法,接下来很自然的对象是C语言表达式和语句,因为一直对C语言表达式和语句有相当多的“牢骚”,这些牢骚多是些对C语言表达式和语句的含混解析的不满,例如,声明语句是语句,赋值语句是语句,无条件跳转语句(如break)亦是语句!据我的理解,这是三类完全不同性质的“计算操作”,归类到一起,那[语句]的本质是什么?答曰:以分号结尾的XX。所以,[100;] 是语句,[1>2;] 是语句,["fuck you";] 也就是语句……只是它们没什么用……显然“以分号结尾”只是从形式上对C语言程序的构造元素进行粗糙的归类,没有揭示语句的实质。

表达式呢?表达式稍微好一点,表达式是由变量或常量与运算符的任意组合序列,用以表达一个计算值。但这个定义也避免不了像[6+(c = 3 + 8)]这样的合法却让人迷惑的表达式。很多书已经指出表达式的本质,那就是[表达式蕴涵着值],但这个蕴涵着值意味什么,在计算理论的深层本质是什么呢?

可以看出,我的疑问都是从计算理论的角度发出的,希望从计算理论的角度对C语言的这些设计进行解释,包括前一文的对C语言运算符的分类。

表达式与语句<-->估值、操值、保值和go where
表达式被估值或操值产生临时值,语句通过保值保存临时值或go somewhere改变机器的计算格局

指令(运算符)、表达式和语句
在组成结构上,语句由指令和表达式组成;在单独的意义上,表达式表达着值,指令则指明操值方式,语句利用表达式的产生临时值改变机器的计算格局

机器的计算格局
机器状态格局,具体表现为机器所有变量的值的组合,变更变量的值或多添一个变量和去一个变量均改变机器格局
机器当前指令,具体处理器的PC值

表达式与语句
我们先看看C99官方解释:
An expression is a sequence of operators and operands that specifies computation of a
value, or that designates an object or a function, or that generates side effects, or that
performs a combination thereof.
表达式是由操作数和操作符组合成的序列,它要么指定(specifies)值计算,要么指明(designates)某个对象或函数,
要么产生一些副作用(side effects),要么同时具有以上多种作用。

Statements are executed for their effect, and do not have values.
语句的执行有一定的结果(effect),但没有值(values)。

我得说,这个官方解释我依然不满意。例如在表达式的定义里,操作数和操作符已被当作直观概念,不作解释了;语句执行的结果也没有进一步的分析和分类,官方里对语句的分类依然很混蛋,神马标号语句都有。

表达式蕴涵着值,在执行时,被机器估值(求值),被估出的值是临时的,它何去何从取决表达式所在语句的操值语义。

为什么需要表达式?
其实”表达式“一语出现在各种逻辑推演的情景,例如数学,例如数理逻辑。表达式就是由符号组成的式子(式子就是一组符号),用于构造推理论证所必须的命题,而这些符号会因不同的逻辑推演而被赋于不意义和角色。一般的情况,符号被赋于三种角色,第一,有确定意思的常项,第二,未确定意思的变项;第三,构造复合意思的联项。这里的意思就是上面的”值“。表达式也相当于思维逻辑学里的概念,但是概念不足以完全表达的[一步逻辑推演],有所断定的命题才行,语句就是命题。但是在编程的情景,把程序语句说成命题,说成[一步逻辑推演]不太好理解。编程里的语句是对表达式的”值“作进一步的处理。

语句

[逻辑推演步]与改变机器格局如何关联

表达式种类
纯一表达式
    常量 | 变量
混合表达式
    
语句种类
    数据流语句
    控制流语句

【formula】∶表示普遍事实、规律、法则或原理的一组符号,如代数式,方程式等

机器的计算格局与语句种类
循环语句是选择语句的变形再复加语义,选择语句是有条件跳转语句。
语句
    保值语句
        赋值语句
        初始化声明语句(声明语句不算语句,但当创建新变量并赋值时,机器计算格局改变)
    跳转语句
        条件跳转语句
            选择语句
            循环语句
        无条件跳语句
            goto identifier ;
                  continue ;
                   break ;
                   return expression ;
        标号语句(标号语句不算语句,它只是协助无默认跳转目标的跳转语句完成语句操作完整语义)
                   identifier : statement
                   case constant-expression : statement
                   default : statement
            


operator specify an operation to be performed (which in turn may yield a value or a function designator, produce a side effect, or some combination thereof)
An operand is an entity on which an operator acts.

《c in a nutshell》
An expression consists of a sequence of constants, identifiers, and operators that the program evaluates by performing the operations indicated. The expression's purpose in the program may be to obtain the resulting value, or to produce side effects of the evaluation, or both (see the section "Side Effects and Sequence Points," later in this chapter).

程序(指令语句)与状态机的转换函数(表)对应问题
状态机内置状态转函数指明,程序是机器的一部分,那程序单条语句改变机器格局与状态转换函数项怎么对应呢?
也就是说,状态转换函数描述了前一状态与后一状态的转换关系,如:
〈 State_current, Symbol, State_next, Action 〉
但程序语句只是条件判断语句才有后一状态由前一状态决定,程序语句有很多无条件的状态转换语句,答案就是无条件也是条件。

图灵机提出的目的是用作算法定义的一个精确模型,非机器设计实现者,不必过多浪费时间在图灵机物理实现上的细节,只需相信图灵机刻画了所有算法。关于图灵机算法描述方式,人们会问:在描述算法时,什么样的详细程度是适当的呢?描述程序有三种:第一种是形式描述,即详尽地写出图灵机的状态、转移函数等,这是描述抽象度最低,最详细的描述;第二种的描述抽象要高一些,叫实现描述,这种描述使用日常语言来描述图灵机的运行,如怎么移动读写头,怎么样在带上存储数据,不给出状态和转移函数的细节;第三种抽象描述,使用日常语言描述算法,忽略实现模型——不提及机器如何管理带子和读写头。

语句

statement:
 labeled-statement
标号语句:控制的跳转目标,有goto和switch两种
labeled-statement:
       identifier : statement
       case constant-expression : statement
       default : statement

 expression-statement
表达语句:
expression-statement:
       expressionopt ;

 compound-statement
语句组:块语句
compound-statement:
        { block-item-listopt }
block-item-list:
        block-item
        block-item-list block-item
block-item:
        declaration
        statement

 selection-statement
选择语句:
selection-statement:
        if ( expression ) statement
        if ( expression ) statement else statement
        switch ( expression ) statement

 iteration-statement
循环语句:
iteration-statement:
        while ( expression ) statement
        do statement while ( expression ) ;
        for ( expressionopt ; expressionopt ; expressionopt ) statement
        for ( declaration expressionopt ; expressionopt ) statement

 jump-statement
跳转语句:
jump-statement:
       goto identifier ;
       continue ;
       break ;
       return expressionopt ;


机器的计算格局

5.1.2. Side Effects and Sequence Points
      
In addition to yielding a value, the evaluation of an expression can result in other changes in the execution environment, called side effects. Examples of such changes include modifications of a variable's value, or of input or output streams.
During the execution of a program, there are determinate points at which all the side effects of a given expression have been completed, and no effects of the next expression have yet occurred. Such points in the program are called sequence points . Between two consecutive sequence points, partial expressions may be evaluated in any order. As a programmer, you must therefore remember not to modify any object more than once between two consecutive sequence points. An example:

--------
《An Introduction to the Theory of Computation》
Configurations of Programs
 An execution of a program on a given input is a discrete process in which the input is consumed, an output is generated, the variables change their values, and the program traverses its instructions. Each stage in the process depends on the outcome of the previous stage, but not on the history of the stages. The outcome of each stage is a configuration of the program that indicates the instruction being reached, the values stored in the variables, the portion of the input left to be read, and the output that has been generated so far.
 Consequently, the process can be described by a sequence of moves between configurations of the
 program.


----《计算理论导引》
图灵机计算时,当前状态、当前带内容和读写头当前位置会发生改变,这三项构成的整体叫做图灵机格局。
0 0
原创粉丝点击