Question

Reading this Pascal BNF grammar I can't understand why is a ; required to appear after end in a function definition. After a function-heading is seen, a function-block that's block may appear:

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

When a begin appear, that's part of a statement-par, that's part of a block, it's processed by statement-part, right?

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

Note statement-part. There's no ; here after end keyword and this is not part of a statement-sequence. So, I don't get how the compiler claims about lack of ; after end keyword, like in this example:

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

What am I missing? am I reading wrong the grammar? all Pascal compilers I've tried give an error if I omit this.

Was it helpful?

Solution 2

You don't have to have a semi-colon after an end. Simple as that.

Semi-colon is used to separate statements. So you only need to have a semi-colon after an end if it is not the last statement. If it is the last statement you should instead have a full stop.

Now, there could also be some error in the BNF that means that according to the BNF you don't have to have a semi-colon where you actually need it, but the only way to figure that out is to analyze the whole BFN in detail, which I don't feel is constructive. :-)

But in this case I think what you have missed is that a procedure or function declaration must end with a semi-colon.

OTHER TIPS

ANTLRWorks is your best friend here.

If you try some pascal grammar such as http://www.monperrus.net/martin/pascal-antlr3 using antlrworks (http://www.antlr3.org/works/) you'll see that a program like

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

will be parsed like this

enter image description here

so you can see exactly what's happening.

ps. the pascal grammar link I've provided to you has a problem with one specific token, but I bet you can workaround this ;-)

ps2. update - antlrworks screenshot to help @Jack

enter image description here

Procedure and functions do not need to be terminated with a semi-colon, but they must be separated by one:

From the Pascal BNF

proc-and-func-declaration:  
   proc-or-func  
   proc-and-func-declaration ; proc-or-func 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top