fbpx
维基百科

越位规则

越位規則Off-side rule)是指程式語言中,用縮排來表示块结构的範圍。這名詞是來自Peter J. Landin,是足球越位(offside)的雙關語。

定义

越位规则,定义于Peter J. Landin在1966年叫做《The Next 700 Programming Languages》的文章中[1]

这个规则被解读为是一种词法约定,通过缩进来表达程序中的声明的作用域:任何非空白记号,当出现在上一行这种记号左侧之时,被接受为一个新声明的开始[2]。实例为Haskell语言的黄金规则:作为某个表达式一部份的代码应该比这个表达式的开始处要缩排进去,即使这个表达式不是此行的最左元素。所有组合起来的表达式必须精确的对齐,在表达式的左侧的所有东西都被当作缩排,即使不是空白。当一个表达式的开始处不是一行的开始的时候,表达式的后续部分可以只比包含这个表达式开始的那一行要缩排进去。由花括号{...}和分号;组织起来的代码块可不采用越位规则。下面以将适用越位规则的do控制结构嵌入if...then...else为例:

错误 正确
if foo then do first thing  second thing  third thing else do something_else 
if foo then do first thing  second thing  third thing else do something_else 
if foo then do  first thing  second thing  third thing else do  something_else 
if foo then do  first thing  second thing  third thing else do   something_else 
垂直对齐[注 1] 悬挂缩进[注 2] 悬挂缩进[注 3]
注释
  1. ^ 组合起来的同类短语必须精确的对齐。
  2. ^ 从在行首的符号then所属的短语的角度看,和从不在行首的do所属的短语的角度看,都是悬挂缩进。
  3. ^ 从在行首的符号then所属的短语的角度看是悬挂缩进;从不在行首的do所属的短语的角度看,缩进区块相当于用括号包围起来的除外情况。

程式範例

以下是一個Python語言程式的例子,其中用縮排表示其程式區塊[3]

def is_even(a: int) -> bool: """确定数a是否是偶数.""" if a % 2 == 0: print('偶数!') return True print('奇数!') return False 

Python中括号内多行代码会隐式的接合在一起,也有着相应的缩排规则:

# 参数比后续部份多一层缩进 def long_function_name( var_one, var_two, var_three, var_four): # 可选的圆括号内后续行多一层缩进,注意这里关键字在行首 if (this_is_first_thing and that_is_second_thing): do_something() # 可选的圆括号内后续行不额外缩进,同类语言元素垂直对齐 elif (this_is_third_thing and that_is_fourth_thing): do_something_different() # 悬挂缩进,参数比行首缩进一层 spam = long_function_name( arg_one, arg_two, arg_three, arg_four) # 按开定界符垂直对齐 eggs = long_function_name(arg_one, arg_two, arg_three, arg_four) #可选的闭括号位置 my_list = [ 1, 2, 3, 4, 5, 6, ] # 可选的闭括号位置 my_set = { 1, 2, 3, 4, 5, 6, } 

实现

越位规则可以在词法分析阶段实现,就像Python那样,在这里增加缩进导致词法器输出一个INDENT记号,而减少缩进导致词法器输出一个DEDENT记号[4]。这些记号分别对应于使用花括号表示块结构的语言中的开花括号{和闭花括号},并且意味着短语语法不依赖于使用的是缩进还是花括号。这要求词法器保持状态,也就是当前的缩进层级,因而在缩进变更的时候可以检测到,因此这种词法文法不是上下文无关的,INDENT/DEDENT依赖于以前缩进层级的上下文信息。

遵循越位規則的語言

编程语言

  • ABC
  • BOO
  • BuddyScript英语BuddyScript
  • Cobra英语Cobra (programming language)
  • CoffeeScript
  • Curry
  • F♯(如果未指定#light "off"
  • Genie
  • Haskell[5](只用于whereletdoof子句在花括号省略的时候)
  • ISWIM,越位規則就是設計此抽象語言時介入的
  • Miranda
  • Nemerle
  • Occam
  • PROMAL英语PROMAL
  • Python
  • Spin英语Spin (programming language)
  • XL英语XL (programming language)
  • Nim

其他语言

參考資料

  1. ^ Landin, P. J. (PDF). Comm. ACM. March 1966, 9 (3): 157–166 [2020-10-11]. doi:10.1145/365230.365257. (原始内容 (PDF)存档于2010-06-20). Indentation, used to indicate program structure. A physical ISWIM can be defined in terms of an unspecified parameter: a subset of phrase categories, instances of which are restricted in layout by the following rule called "the offside rule." The southeast quadrant that just contains the phrase's first symbol must contain the entire phrase, except possibly for bracketed subsegments. This rule has three important features. It is based on vertical alignment, not character width, and hence is equally appropriate in handwritten, typeset or typed texts. Its use is not obligatory, and use of it can be mixed freely with more conventional alternatives like punctuation. Also, it is incorporated in ISWIM in a systematic way that admits of alternatives without changing other features of ISWIM and that can be applied to other languages. 
  2. ^ off-side rule for FOLDOC. [2020-10-11]. (原始内容于2021-01-20). 
  3. ^ . [2012-04-30]. (原始内容存档于2012-02-07). 
  4. ^ Python Documentation (页面存档备份,存于互联网档案馆), 2. Lexical analysis (页面存档备份,存于互联网档案馆): 2.1.8. Indentation (页面存档备份,存于互联网档案馆
  5. ^ . [2012-04-30]. (原始内容存档于2012-03-31). 

越位规则, 越位規則, side, rule, 是指程式語言中, 用縮排來表示块结构的範圍, 這名詞是來自peter, landin, 是足球中越位, offside, 的雙關語, 目录, 定义, 程式範例, 实现, 遵循越位規則的語言, 编程语言, 其他语言, 參考資料定义, 编辑, 定义于peter, landin在1966年叫做, next, programming, languages, 的文章中, 缩排被用来指示程序结构, 物理的iswim可以依据一个未指明的参数即短语范畴的一个子集来定义, 它的实例受到. 越位規則 Off side rule 是指程式語言中 用縮排來表示块结构的範圍 這名詞是來自Peter J Landin 是足球中越位 offside 的雙關語 目录 1 定义 2 程式範例 3 实现 4 遵循越位規則的語言 4 1 编程语言 4 2 其他语言 5 參考資料定义 编辑越位规则 定义于Peter J Landin在1966年叫做 The Next 700 Programming Languages 的文章中 1 缩排被用来指示程序结构 物理的ISWIM可以依据一个未指明的参数即短语范畴的一个子集来定义 它的实例受到遵从叫做 越位规则 的规则的布局的限制 包含一个短语的第一个符号作为原点的右下象限必定包含整个短语 除了可能的被括号包围的子段落之外 这个规则有三个重要特征 它基于了对齐 而非字符宽度 因而它同等的适合于手写 排字和打字的文本 它的使用不是强制的 它可以自由的同更常规的替代者比如标点符号一起混合使用 还有它是以一种系统性方式结合进入ISWIM中的 这容许了替代者而不改变ISWIM的其他特征 它可以应用于其他语言 这个规则被解读为是一种词法约定 通过缩进来表达程序中的声明的作用域 任何非空白记号 当出现在上一行这种记号左侧之时 被接受为一个新声明的开始 2 实例为Haskell语言的黄金规则 作为某个表达式一部份的代码应该比这个表达式的开始处要缩排进去 即使这个表达式不是此行的最左元素 所有组合起来的表达式必须精确的对齐 在表达式的左侧的所有东西都被当作缩排 即使不是空白 当一个表达式的开始处不是一行的开始的时候 表达式的后续部分可以只比包含这个表达式开始的那一行要缩排进去 由花括号 和分号 组织起来的代码块可不采用越位规则 下面以将适用越位规则的do控制结构嵌入if then else为例 错误 正确if foo then do first thing second thing third thing else do something else if foo then do first thing second thing third thing else do something else if foo then do first thing second thing third thing else do something else if foo then do first thing second thing third thing else do something else垂直对齐 注 1 悬挂缩进 注 2 悬挂缩进 注 3 注释 组合起来的同类短语必须精确的对齐 从在行首的符号then所属的短语的角度看 和从不在行首的do所属的短语的角度看 都是悬挂缩进 从在行首的符号then所属的短语的角度看是悬挂缩进 从不在行首的do所属的短语的角度看 缩进区块相当于用括号包围起来的除外情况 程式範例 编辑以下是一個Python語言程式的例子 其中用縮排表示其程式區塊 3 def is even a int gt bool 确定数a是否是偶数 if a 2 0 print 偶数 return True print 奇数 return False Python中括号内多行代码会隐式的接合在一起 也有着相应的缩排规则 参数比后续部份多一层缩进 def long function name var one var two var three var four 可选的圆括号内后续行多一层缩进 注意这里关键字在行首 if this is first thing and that is second thing do something 可选的圆括号内后续行不额外缩进 同类语言元素垂直对齐 elif this is third thing and that is fourth thing do something different 悬挂缩进 参数比行首缩进一层 spam long function name arg one arg two arg three arg four 按开定界符垂直对齐 eggs long function name arg one arg two arg three arg four 可选的闭括号位置 my list 1 2 3 4 5 6 可选的闭括号位置 my set 1 2 3 4 5 6 实现 编辑越位规则可以在词法分析阶段实现 就像Python那样 在这里增加缩进导致词法器输出一个INDENT记号 而减少缩进导致词法器输出一个DEDENT记号 4 这些记号分别对应于使用花括号表示块结构的语言中的开花括号 和闭花括号 并且意味着短语语法不依赖于使用的是缩进还是花括号 这要求词法器保持状态 也就是当前的缩进层级 因而在缩进变更的时候可以检测到 因此这种词法文法不是上下文无关的 INDENT DEDENT依赖于以前缩进层级的上下文信息 遵循越位規則的語言 编辑编程语言 编辑 ABC BOO BuddyScript 英语 BuddyScript Cobra 英语 Cobra programming language CoffeeScript Curry F 如果未指定 light off Genie Haskell 5 只用于where let do或of子句在花括号省略的时候 ISWIM 越位規則就是設計此抽象語言時介入的 Miranda Nemerle Occam PROMAL 英语 PROMAL Python Spin 英语 Spin programming language XL 英语 XL programming language Nim 其他语言 编辑 Haml reStructuredText Sass YAML參考資料 编辑 Landin P J The next 700 programming languages PDF Comm ACM March 1966 9 3 157 166 2020 10 11 doi 10 1145 365230 365257 原始内容 PDF 存档于2010 06 20 Indentation used to indicate program structure A physical ISWIM can be defined in terms of an unspecified parameter a subset of phrase categories instances of which are restricted in layout by the following rule called the offside rule The southeast quadrant that just contains the phrase s first symbol must contain the entire phrase except possibly for bracketed subsegments This rule has three important features It is based on vertical alignment not character width and hence is equally appropriate in handwritten typeset or typed texts Its use is not obligatory and use of it can be mixed freely with more conventional alternatives like punctuation Also it is incorporated in ISWIM in a systematic way that admits of alternatives without changing other features of ISWIM and that can be applied to other languages off side rule for FOLDOC 2020 10 11 原始内容存档于2021 01 20 Python FAQ on colons 2012 04 30 原始内容存档于2012 02 07 Python Documentation 页面存档备份 存于互联网档案馆 2 Lexical analysis 页面存档备份 存于互联网档案馆 2 1 8 Indentation 页面存档备份 存于互联网档案馆 The Haskell Report Layout 2012 04 30 原始内容存档于2012 03 31 取自 https zh wikipedia org w index php title 越位规则 amp oldid 74995511, 维基百科,wiki,书籍,书籍,图书馆,

文章

,阅读,下载,免费,免费下载,mp3,视频,mp4,3gp, jpg,jpeg,gif,png,图片,音乐,歌曲,电影,书籍,游戏,游戏。