Search code examples
pascalbnf

Where in this BNF grammar do match ; after 'end'


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.


Solution

  • 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.