英文原文:http://www.freepascal.org/docs-html/ref/refch1.html#x8-70001
第1章 Pascal语言符号 (Pascal Tokens) 语言符号(Tokens)是源代码的基本构成块:它们是组成语言的"字":字符根据程序语言的规则组合成语言符号.有5个系列的语言符号: 保留字(reserved words) 这些是在语言中具有固定意思的字.它们不能被改变或重定义. 标识符(identifier) 这些是程序员定义的标志的名字.它们可以被改变或者重用.它们是语法规则中的主体. 操作符(operators) 它们经常是数学符号或者其他操作符: +, -, *等. 分隔符(separators) 这经常是泛空格符. 常量(constants) 数字或者字符常量是用来定义源代码中的确定值,比如1(整数常量)或者2.3(浮点常量)或者'String constant'(字符串:一片文本). 在这一章,我们会说明所有的Pascal保留字,以及各种用来定义字符串,数字,标识符等的方法. 1.1 符号(Symbols) Free Pascal允许所有的字符,数字,和一些特殊符号字符出现在Pascal源文件.

_________________________________________________________________________________________________________Recognised symbols
--letter --A...Z ---------------------------------------------------
--a...z -|

-- - ----------------------------------------------------
digit 0...9

--hex digit-|0...9------------------------------------------------
-A...F--|
a...f
___________________________________________________________________

如下的字符具有特殊意思:
        + - * / = < > [ ] . , ( ) : ^ @ { } $ # 
    以及如下的字符组合: 
         <= >= := += -= *= /= (* *) (. .) //
    
    当使用范围指示符时,字符组合'(.'等价于左方括号'[',同样的,字符组合'.)'等价于右方括号']'.当使用注释分隔符时,字符组合'(*'等价于左大括号'{'而'*)'等价于右大括号'}'.这些字符组合若出现在字符串中则保留原意.
    
  1.2 注释(Comments)
    注释是源代码中被编译器完全忽略的一片代码.它们的存在只是有益于程序员来理解整片代码.对于编译器来说,好像注释根本就不存在.
    
    如下的代码片展示了一个注释:
        (* My beautiful function returns an interesting result *)  
        Function Beautiful : Integer;  
    使用(*和*)作为注释分隔符是来自Pascal语言刚开始时的历史.现在一般都换成用{和}来当作注释分隔符,比如如下的例子:
        { My beautiful function returns an interesting result }  
        Function Beautiful : Integer;  

    注释也可以跨越多行:
        {  
           My beautiful function returns an interesting result,  
           but only if the argument A is less than B.  
        }  
        Function Beautiful (A,B : Integer): Integer; 

    单行注释也可以使用 // 分隔符: 
        // My beautiful function returns an interesting result  
        Function Beautiful : Integer;  

    该注释从//字符开始直到该行末尾.这种类型的注释是由Borland在Delphi的Pascal编译器中引用的. 

    Free Pascal支持注释的混合使用.如下的结构是正确的注释: 
        (* This is an old style comment *)  
        {  This is a Turbo Pascal comment }  
        // This is a Delphi comment. All is ignored till the end of the line.
    以下是正确的嵌套注释: 
        { Comment 1 (* comment 2 *) }  
        (* Comment 1 { comment 2 } *)  
        { comment 1 // Comment 2 }  
        (* comment 1 // Comment 2 *)  
        // comment 1 (* comment 2 *)  
        // comment 1 { comment 2 }
    以上最后两个注释只能用在单行.如下两个例子将会报错:
        // Valid comment { No longer valid comment !!  
            } 
    和
        // Valid comment (* No longer valid comment !!  
            *)
    编译器在发现这样的结构时将会回应'错误的字符(invlid character)'错误,而不管-Mturbo开关.
    
    备注:在TP或者Delphi模式下,不允许嵌套的注释,原因是为了最大地兼容原有为这些编译器写的代码.
    
  1.3 保留字(Reserved words)
    保留字是Pascal语言的一部分,因而不能被程序员重新定义.在语法图中它们是用黑体字形定义的.Pascal不是大小写敏感的,所以编译器接受任何大小写字母组合的保留字.
    Turbo Pascal和Delphi的保留字有一些不同.在TP模式下,只有Turbo Pascal的保留字能被认出,而Delphi的可以被它重新定义.默认情况下,Free Pascal认可Delphi的保留字.
    
    1.3.1Turbo Pascal保留字(Turbo Pascal reserved words)
      在Turbo Pascal模式下存在如下关键词
        absolute  
        and  
        array  
        asm  
        begin  
        case  
        const  
        constructor  
        destructor  
        div  
        do  
        downto  
        else  
        end  
        file  
        for  
        function  
        goto  
        if  
        implementation  
        in  
        inherited  
        inline  
        interface  
        label  
        mod  
        nil  
        not  
        object  
        of  
        on  
        operator  
        or  
        packed  
        procedure  
        program  
        record  
        reintroduce  
        repeat  
        self  
        set  
        shl  
        shr  
        string  
        then  
        to  
        type  
        unit  
        until  
        uses  
        var  
        while  
        with  
        xor
    
    1.3.2 Free Pascal保留字(Free Pascal reserved words)
      除了上述Turbo Pascal的保留字,Free Pascal也认为如下为保留字:
        dispose  
        exit  
        false  
        new  
        true  
        
    1.3.3 Object Pascal保留字(Object Pascal reserved words)
      Object Pascal(在Delphi或者Objfpc模式下)的保留字和Turbo Pascal一样,再加上如下的关键字:
        as  
        class  
        dispinterface  
        except  
        exports  
        finalization  
        finally  
        initialization  
        inline  
        is  
        library  
        on  
        out  
        packed  
        property  
        raise  
        resourcestring  
        threadvar  
        try
        
    1.3.4 修饰字(Modifiers)
      如下是所有修饰字的列表.就意义而言它们不是完全正确的保留字因为它们能被当成标识符使用,但是在一些特定的位置,它们对于编译器有特定的意思,即此时编译器认为它们是Pascal语言的一部分.
        absolute  
        abstract  
        alias  
        assembler  
        cdecl  
        cppdecl  
        default  
        export  
        external  
        far  
        far16  
        forward  
        index  
        local  
        name  
        near  
        nostackframe  
        oldfpccall  
        override  
        pascal  
        private  
        protected  
        public  
        published  
        read  
        register  
        reintroduce  
        safecall  
        softfloat  
        stdcall  
        virtual  
        write
        
      备注:预定义类型比如Byte, Boolean和常量比如maxint之类并不是保留字.它们是标识符,是在system单元中声明的.这意味着这些类型可以在其他单元里被重新定义.尽管如此,不鼓励程序员这么做,因为这会引起很多混乱.
      
  1.4 标识符(Identifiers)
    标识符是程序员用来给特定的常量(contants),类型(types),变量(variables),过程(procedures)与函数(functions),单元(units),和程序(programs)定义名字的.所有的在源代码中由程序员定义的名字(除了保留字)被认为是标识符.
    标识符由1到127个有效字符(字母,数字和下划线字符)组成,但必须由一个字母表中的字符开头,或者一个下划线(_).如下的语法图给出了基本的标识符语法.

_________________________________________________________________________________________________________Identifiers
--identifier---letter------------------------------------------------
--x ---|---letter---|
|digit--|
--x ---
___________________________________________________________________

和Pascal保留字一样,标识符也是不分大小写的,也就是说
        myprocedure; 
    和
        MyProcedure; 
    指的是同一个过程.
    备注: 在版本 2.5.1 可以在一个保留字前面加一个和号(&)来指定它为标识符.意思是如下是可以的: 
    program testdo;  
      
    procedure &do;  
      
    begin  
    end;  
      
    begin  
      &do;  
    end. 

    保留字do像其他标识符一样地声明,像调用'do'一样的使用.
    
  1.5 提示指令(Hint directives)
    大多数标识符(常量constants, 变量variables, 过程或方法functions or methods, 属性properties)可以有一个提示指令跟在它们的声明后面:

_________________________________________________________________________________________________________Hint directives
--hintdirective -|--------------------------------------------------
|--Deprecated ---|
|--Experimental --|
|---Platform ----|
-Unimplemented -
___________________________________________________________________

每当编译器遇到一个标记有提示指令的标识符时,将会显示一个警告,以及相应的详细提示.
        deprecated 
        该标识符的使用是过时的,请使用替代的选择.
        The use of this identifier is deprecated, use an alternative instead.
        experimental
        该标识符的使用是实验性质的:这可以被用来标记一些需要警示的新特性.
        The use of this identifier is experimental: this can be used to flag new features that should be used with caution. 
        platform 
        该标识符是平台相关的:这可能无法在所有平台上定义.
        This is a platform-dependent identifier: it may not be defined on all platforms. 
        unimplemented 
        这应该只用在函数和过程上.该指令用来标志部分特性尚未实现.
        This should be used on functions and procedures only. It should be used to signal that a particular feature has not yet been implemented.
    
    接下来是例子:
        Const  
          AConst = 12 deprecated;  
          
        var  
          p : integer platform;  
          
        Function Something : Integer; experimental;  
          
        begin  
          Something:=P+AConst;  
        end;  
          
        begin  
          Something;  
        end. 

    这会导致如下的输出: 
        testhd.pp(11,15) Warning: Symbol "p" is not portable  
        testhd.pp(11,22) Warning: Symbol "AConst" is deprecated  
        testhd.pp(15,3) Warning: Symbol "Something" is experimental 

    提示指令可以跟在任何种类的标识符后面:单元units, 常量constants, 类型types, 变量variables, 函数functions, 过程和方法procedures and methods.
    
  1.6 数字(Numbers)
    数字默认是以十进制定义的.实数(或小数)数字是使用使用工程学或者科学计数法(如 0.314E1).
    对于整数类型的常量,Free Pascal支持4种格式:
      1.普通的(decimal)十进制格式.这是标准格式.
      2.(Hexadecimal)十六进制格式,和Turbo Pascal一样.要表示一个十六进制的常量,需要在它前面放一个美元符号($).所以十六进制数$FF等价于十进制数255.注意使用十六进制常量时是不需要区分大小写的.
      3.从版本1.0.7开始,(Octal)八进制格式开始被支持.要表示一个八进制数,需要在它前面放一个和符(&).例如15用八进制表示为&17.
      4.(Binary)二进制记数法,一个二进制数可以在它前面放一个百分号(%)来表示.所以,255可以用二进制数%11111111表示.
    如下是数字的语法图.

_________________________________________________________________________________________________________Numbers
--hex digit sequence-hex digit--------------------------------------
---------|

-- --- -------------------------------------
octal digit sequence-octal digit|

-- ---- -------------------------------------------
bin digit sequence |-10 -||
------|

-- --- --------------------------------------------
digit sequence -digit-|

--unsigned integer|----digit sequence-------------------------------
|-$ -hex digit sequence--|
|-& -octal digit sequence|
% bin digit sequence

--sign|+ --------------------------------------------------------
-----

--unsigned real digit sequence -|-----------------|-------------------
-.-digit sequence- -scale factor--

--scale factor-|E ---|------digit sequence---------------------------
-e -- -sign--

--unsigned number---unsigned real----------------------------------
-unsigned integer-|

-- --------- ----------------------------
signed number - -| unsigned number
sign
___________________________________________________________________

备注:八进制和二进制记数法在TP或者Delphi兼容模式下是不支持的.
    
  1.7 标签(Labels)
    标签是源码中用来给goto语句跳转定位的标志.标签与标准的标识符不同的是它可以用数字开头.

_________________________________________________________________________________________________________Label
--label--|digit sequence--------------------------------------------
---identifier---
___________________________________________________________________

备注:在使用标签前,必须先指明-Sg 或者 -Mtp开关.默认下,Free Pascal不支持标签以及gote语句.{$GOTO ON}指令也可以用来允许标签和goto语句.
    
  1.8 字符串(Character strings)
    一个字符串(或者短字符串)是一个由0个或更多(根据size)字符组成的序列,用单引号包容,并且在程序源码上只能是单行的:不能存在文字性的回车符或换行符.
    两个单引号之间不含任何字符('')的是空字符串.

_________________________________________________________________________________________________________Character strings
--character string---quoted string ------------------------------------
|-control string-||
---------------|

--quoted string- ’---string character---’-------------------------------
--------------|

-- --- ------------------------
string character --Any-character except ’ or-CR
”

--control string-|# -unsigned integer-------------------------------
-------------------
___________________________________________________________________

   
    字符串由标准的8比特ASCII字符或者Unicode字符(一般是UTF-8编码)组成.控制字符串可以用来表示不能用键盘打出来的字符,比如#27表示逸出(escape)字符.
    单引号字符可以用打两次来嵌入到字符串中.Pascal字符串不支持C风格的转义字符(用反斜杠,比如\n表示换行).
    
    如下是正确的字符常量:
      'This is a pascal string'  
      ''  
      'a'  
      'A tabulator character: '#9' is easy to embed' 

    如下是错误的字符串表示: 
       'the string starts here  
       and continues here' 

    上面的字符串必须像这样打: 
       'the string starts here'#13#10'   and continues here' 
    或者在类Unix上(包括 Mac OS X):
       'the string starts here'#10'   and continues here' 
    而在经典的Mac系列操作系统上:
       'the string starts here'#13'   and continues here' 
    
    可以在字符串中使用其他字符集:这种情况下源文件的字符集(codepage)必须要用{$CODEPAGE XXX}指令或者编译器的控制行命令-Fc来指定.这样字符串中的字符才能用指定的字符集来解读.
posted on 2011-10-24 00:20  小生(骗术师)  阅读(1217)  评论(0编辑  收藏  举报