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

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.

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

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.

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

Related

Pascal adding whitespace to file typed

I am trying to write something to a file but pascal adds a bunch of whitespaces between each record's value and also puts 2 records on one line.
The first file is just a list of strings.
The second file (the one I create through the program) should have title and description.
How can I get rid of the whitespaces pascal is adding?
program Wiki;
{$mode objfpc}
TYPE wiki=record
title:string;
description:string;
end;
var
f:text ;
g:file of wiki ;
row:wiki;
fileName: string;
oldFileName:string;
begin
writeln('Old file name:');
readln(oldFileName);
ASSIGN(f,oldFileName);
RESET(f);
writeln('New file name:');
readln(fileName);
ASSIGN(g,fileName);
REWRITE(g);
REPEAT
Readln(f,row.title);
writeln('give a description:');
Writeln(row.title);
Readln(row.description);
Write(g,row)
until EOF(f);
CLOSE(f);
CLOSE(g);
writeln;
writeln('press enter to close.');
readln();
end.
In objfpc mode without {$H+}, I guess that row.description is a fixed size Turbo Pascal style ShortString. It is 255 characters long, and that is probably why you get all that whitespace.
Rather write the output file as a text file:
var
f: Text;
g: Text;
and:
Writeln(g, row.title, ';', row.description);
That should produce text output like:
Finding Nemo;The adventures of two fish trying to find the lost son of one of them
Toy Story;The adventures of a merry bunch of toys
etc.

Compiling Ada with GCC

I'm trying to compile this Calculator.ada file using gcc -c Calculator.ada and receive the error warning: Calculator.ada: linker input file unused because linking not done -- I've tried looking up solutions and downloading other things that may compile this for me but haven't figured it out yet....
Here is Calculator.ada:
--
-- Integer calculator program. Takes lines of input consisting of
-- <operator> <number>, and applies each one to a display value. The
-- display value is printed at each step. The operator is one of =,
-- +, -, *, /, or ^, which correspond to assign, add, subtract, multiply
-- divide, and raise, respectively. The display value is initially zero.
-- The program terminates on a input of q.
--
with Text_IO;
with Gnat.Io; use Gnat.Io;
procedure Calc is
Op: Character; -- Operation to perform.
Disp: Integer := 0; -- Contents of the display.
In_Val: Integer; -- Input value used to update the display.
begin
loop
-- Print the display.
Put(Disp);
New_Line;
-- Promt the user.
Put("> ");
-- Skip leading blanks and read the operation.
loop
Get(Op);
exit when Op /= ' ';
end loop;
-- Stop when we're s'posed to.
exit when Op = 'Q' or Op = 'q';
-- Read the integer value (skips leading blanks) and discard the
-- remainder of the line.
Get(In_Val);
Text_IO.Skip_Line;
-- Apply the correct operation.
case Op is
when '=' => Disp := In_Val;
when '+' => Disp := Disp + In_Val;
when '-' => Disp := Disp - In_Val;
when '*' => Disp := Disp * In_Val;
when '/' => Disp := Disp / In_Val;
when '^' => Disp := Disp ** In_Val;
when '0'..'9' => Put_Line("Please specify an operation.");
when others => Put_Line("What is " & Op & "?");
end case;
end loop;
end Calc;
I would appreciate any assistance as to why I can't compile this. I'm able to compile C files fine with gcc -c and read that I can compile the same way for Ada.
Since gcc only recognizes .ads and .adb as file endings for Ada sources (as explained in this link mentioned by Eugene), you need to tell it explicitly that you want this file to be compiled as Ada source. You can do this via
gcc -x ada -c Calculator.ada
The compiler will then probably give a warning like
Calculator.ada:11:11: warning: file name does not match unit name, should be "calc.adb"
but you can ignore that. However, best practice would be to use the file names expected by gcc.
By default, Ada source file need to end with .ads (for package specs) or .adb (for bodies), and file names need to match the top-level entity they contain. In your case, you should use calc.adb.
If you have more complex source files containing multiple entities, you can use the gnatchop tool to rename source files.
Under File Naming Topics and Utilities, the manual contains much more documentation how source code can be represented in the file system.

Pascal: sentence after an if

Im just starting up with pascal and I'm doing the good old bhaskara solver with the following code:
Program bhaskara;
var
a,b,c: real;
begin
writeln('Ingrese a, b y c');
readln(a,b,c);
if sqr(b) >= 4*a*c then
begin
writeln('tiene raices reales');
end
else
begin
writeln('no tiene raices reales');
end
readln(a);
end.
The last line: readln(a), which is there just to pause the program and see the output is making the program not compile(program works fine without it), it says:
bhaskara.pas(15,2) Fatal: Syntax error, ";" expected but "identifier READLN" found
Im sure it's something simple but i can't find the answer, please help.
Pascal requires a semicolon as a statement separator between statements.
Your else block is a statement and because it is not the final line
of the program and is followed by your readln(a), it requires a ';'
after it.
In fact, because your else clause contains only a single statement,
it does not require the begin & end.
So you could simply write
else
writeln('no tiene raices reales');
readln(a);
You need a semicolon (;) after the "end" statement right before the readln statement.

First program using Pascal

im new using Pascal and i have to program a game called seven eleven, can you guys give me some tips?
Ive tried:
program sevele;
var capitalinicial: integer
begin
writeln('ingrese un capital')
readln(capitalinicial)
writeln('su capital es capitalinicial')
Answering your question, There are a few things you need to know:
You have to put a semicolon ; at the end of every sentence, except after a keyword that denotes the beginning of a control structure (for,while,if,else,etc) or after a begin or end keyword.
Use the same amount of begins and ends keywords. Note that the last end in the program is followed by a dot.
When you use writeln you can print one or more variables or strings. You have to use simple quotes ' to print strings, and just the name of a variable without any quote to print it's value, also you need to separe the diferent arguments with commas ','.
for example:
program example;
var
a,b:integer;
begin
a:=3;
b:=5;
writeln ('this is just a string');
writeln (a);
writeln (a,b);
writeln ('the value of a is: ',a,' and the value of b is: ',b);
readln;
end.
The code you attemped to write probably is:
program sevele;
var capitalinicial: integer;
begin
writeln('ingrese un capital');
readln(capitalinicial);
writeln('su capital es ',capitalinicial);
readln; //use this to give you time to view the output
end.
This is how it works
program sevele;
var capitalinicial: integer;
begin
writeln('ingrese un capital');
readln(capitalinicial);
writeln('su capital es',capitalinicial);
end.

SQL - A simpler function than decode

I am working with a pl/sql procedure. I have an initialized variable myvar and I want to check its value : if it does not contain 'Z', I want it to contain 'P'.
I am currently doing it this way:
myvar := decode(myvar,'Z','Z','P');
I was just wondering if there was a simplier way to do this. I mean, decode is already simple, but I feel it's weird to specify the content of the variable while it is already in it !
If such a function would exist, it would look like this:
Function myfunction(a In Varchar2, b In Varchar2, c In Varchar2)
Return Varchar2
Is
Begin
if a <> b
then
return c;
end if;
return a;
End myfunction;
Any help would be appreciated !
There is no built-in function that does exactly what you want.
You could use CASE rather than DECODE:
CASE myvar WHEN 'Z' THEN 'Z' ELSE 'P' END
It doesn't make it any shorter though!
Put that function of yours to the program's declaration section and use it!
I agree the best option is to use CASE expression:
CASE myvar WHEN 'Z' THEN 'Z' ELSE 'P' END
Another approach if you feel happy with DECODE is to run this query:
SELECT decode(myvar,'Z','Z','P')
INTO myvar
FROM DUAL;
To answer your original question of whether there is a simpler way, there is also this:
if myvar <> 'Z' then
myvar := 'P'
end if;

Resources