2014-01-26 31 views
1

阅读this帕斯卡BNF语法我不明白为什么需要出现在函数定义中的end之后。功能镦看到后,function-block多数民众赞成block可能会出现:在这个BNF语法中匹配的地方; '结束'后

function-declaration = 
    function-heading ";" function-body | 
    function-heading ";" directive | 
    function-identification ";" function-body . 
function-body = 
    block . 

begin出现,这是一个statement-par的一部分,这是一个块的一部分,它是由statement-part处理,对不对?

block = 
    declaration-part statement-part . 
statement-part = 
    begin statement-sequence end . 

注意statement-partend关键字后不存在;,这不是statement-sequence的一部分。所以,我不明白编译器有缺乏;如何索赔后end关键字,就像这个例子:

function myabs(i : integer) : integer; 
begin 
    if i < 0 then begin i := -i; end; < -- it's process by statement-sequence, so, ';' may appear 
    myabs := i; 
end; <-- it is the semicolon what about I'm speaking 

我缺少什么?我读错了语法吗?我试过的所有Pascal编译器都会给出一个错误,如果我省略这个。

+0

就在这个相同的链接,它是汇集了'功能declaration'和'功能body',即部分:'程序 - 和 - function-declaration-part = {(procedure-declaration | function-declaration)“;” }这就是分号的地方。 – lurker

+0

为什么-1? donwvoter请解释一下? –

回答

1

结束后您不必有分号。就那么简单。

分号用于分隔语句。所以,如果不是最后一条语句,你只需要在分号结尾后加分号。如果这是最后一个陈述,你应该完全停下来。

现在,在BNF中也可能存在一些错误,这意味着根据BNF,您不必在实际需要的地方使用分号,但要弄清楚的唯一方法是分析整个BFN详细,我不觉得是建设性的。 :-)

但是在这种情况下,我认为你错过的是一个procedure or function declaration must end with a semi-colon

+0

但是为什么任何Pascal编译器(我尝试过的)都会说“预计”,“但是其他的东西”等等。 –

+0

@TheMask因为它预期在该位置有一个分号,但它没有找到。请不要误解,在您询问的位置应该有一个分号。 –

+0

我以前没见过你编辑过。非常感谢。 :)真的,我之前读过所有这个程序员,并没有注意到这一点。 –

1

程序和功能并不需要用分号来终止,但必须通过一个分开:

the Pascal BNF

proc-and-func-declaration: 
    proc-or-func 
    proc-and-func-declaration ; proc-or-func 
2

ANTLRWorks是你最好的朋友在这里。

如果你尝试一些帕斯卡语法如http://www.monperrus.net/martin/pascal-antlr3使用antlrworks(http://www.antlr3.org/works/),你会看到一个像程序

program first; 
function myabs(i : integer) : integer; 
begin 
end; 
begin 
end. 

会这样

enter image description here

这样你就可以被解析确切地看到发生了什么。

ps。我提供给你的pascal语法链接有一个特定的令牌有问题,但我敢打赌你可以解决这个问题;-)

ps2。更新 - antlrworks截图,以帮助@Jack

enter image description here

+0

你是如何从整个语法创建这个图的?我只能从所选语法(鼠标所在的位置)创建。 – Jack

+0

@Jack,这样做:打开antlrworks 1.5.2,打开语法(ctrl + O),将pascal片段粘贴到左侧文本区域(选项卡“解释器”)。在左侧文本区域上方的选择框中默认选择“program”,然后点击“play”图标。我在答案中添加了屏幕截图。 – Leo