I am trying to figure out the issue with this piece of Pascal code
function Factorial(n: integer): integer;
begin
if n = 0 then
Result := 1
else if n > 0 then
Result := Factorial(n - 1) * n;
end;
When I run the code I get the error
-bash: syntax error near unexpected token `n:'
Anyone can tell why that is? I am using the fpc (free pascal compiler) is this code meant for a different Pascal compiler?
That code compiles fine in fpc.
From the error message you quote, as #KenWhite says, it sounds like you are using the wrong tool to try to compile it - bash is an operating system shell for Linux and it is a bash error message. bash is not for comiling Pascal code.
I suggest you download and use Lazarus, which is the freeware IDE for fpc and runs on Linux and Windows. Once you have Lazarus installed on your system, create a new project (a "simple project" from Lazarus's list of new project types. Then copy/paste the code above the begin ...end of the project source, then save and compile it and you will see that Lazarus reports successfully compiling the project.
Btw, there is an omission from the code - it only covers cases where n is greater or equal to zero, so the function has an undefined result for n less that zero.
Related
I study Computer Science in college and one of my group tasks is to create a simple statement lexical analyzer, along with a simple arithmetic expression, simple conditional If with block, and simple looping with conditional If block. I'm trying my hand at making at least a simple statement lexical analyzer.
However, I have no clue on how to code with Pascal language as I was never taught this language over the course of my 5 semesters in college.
I tried looking up documentations of Pascal programming language along with various examples of it online while trying to do it myself on an online compiler, cross referencing a lexical analyzer made with C language and tried converting it myself into Pascal language.
It turned out to be more complicated than I had anticipated.
Again, I am very new to Pascal language, just starting a few days ago in fact. So if there are any tips or examples, preferably short and to the point, it would be very much appreciated, thanks.
If you want to see my code, its not much, as I was more focused on grasping the language. The only languages I was taught and can understand is Java and C, along with a little bit of PHP.
program SimpleStatement;
var letters: array['a'...'z'] of char,
numbers: array['1'...'9'] of integer,
symbols: char;
function letVer(letters : char): boolean;
begin
clrscr;
if(letters = 'a'...'z') then
begin
for letters: 'a' to 'z' do
writeln('keywords: ', letters)
end
letVer = true;
end
begin
letVer;
writeln ();
end.
Along with the error code:
Free Pascal Compiler version 3.0.4+dfsg-23 [2019/11/25] for x86_64
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling main.pas
main.pas(12,23) Error: Error in type definition
main.pas(12,23) Fatal: Syntax error, "]" expected but "..." found
Fatal: Compilation aborted
Error: /usr/bin/ppcx64 returned an error exitcode
I am running a model which is written in Fortan (an executable), in some runs it started to deliver constant errors and apparently incoherent results, however when I closely checked the results file (a text with n columns of data) and I realized that when the concentration of certain mineral is very very low, lets say 2.9984199E-306, the code omits the 'E' and the number presented is 2.9984199-306 which of course causes problems. Since I have no access to the source code of the executable file, is there a way to avoid this problem in Windows? I have seen that in other computers these numbers are directly replaced by zero, however I was not able to find the specific configuration to achieve it.
You will need access to code to change the output formatting or you will need to post-process your output. You are seeing standard conforming Fortran behavior. Consider the simple program
program foo
implicit none
real(8) x
integer i
x = 1
do i = 1, 10
x = x / 5.4321e11
write(*,'(ES15.7)') x
end do
end program foo
It's output is
1.8409086E-12
3.3889446E-24
6.2387373E-36
1.1484945E-47
2.1142735E-59
3.8921843E-71
7.1651557E-83
1.3190397E-94
2.4282316-106
4.4701525-118
See Fortran 2018 Standard, 13.7.2.3.3 E and D editing, in particular, Table 13.1.
I'm a newbie in ML languages. I'm reading the ATS manual and I in mad now.
I read half of manual and there is no explanation in it how to compile a non-void function.
Only one example with "hello world" program.
ats
val x = "hello"
implement main0 () = print x
But if I change x to 2 * 2 it will raise an error.
Should I read the manual without capability to compile code and test it? Disgusting.
You need the following line for
some library functions:
#include "share/atspre_staload.hats"
ATS may not be a good language for beginners as it assumes
a fair amount of knowledge of ML and C.
I can't debug one of my programs for a year now. When I press the green Run button, the following error message appears:
The full text is:
The GDB command:
"-exec-run"
returned the error:
",msg="Error creating process C:/Users/leven/OneDrive/J\341t\351kpogramok/People/people.exe, (error 193).""
I've read many forums about this error, but my case looks a bit different...
As you can see, the file's path doesn't include any characters that could occur this problem (no spaces, no special letters). I've tried running outside OneDrive, same error.
I've spent a lot of time looking for something in my program that occurs this error and found that if I delete some parts of it (eg. a few procedures or functions, which contains a lot of code though), the program is debuggable again! So the trouble is with some parts of the program, but I still don't know the exact problem.
As I can remember, I've always debugged this program in a 64bit OP.
The one thing that could be problematic is that I probably started writing the program using Windows 7 or 8, and now I want to run it using Windows 10, but I still don't understand, why deleting some parts of the program is a solution...
Thanks in advance for your help!
UPDATE:
I've found, that the line
p[x,y,2,1]:=r;
cannot be debugged by the compiler.
Description:
p: array [1..15000, 1..10000, 1..7, 1..4] of integer;
p[] is a game field. The first two parameters are coordinates, the third and the fourth are not important.
x, y and r are integers.
So, the command seen above writes a number into the game field (p[]) array using the x, y coordinates.
I think we established through a series of queries in comments that the necessary and sufficient condition to provoke the debugger problem you've been getting is to include in your app the declaration of the array p that you've added to your q, that is:
var
p: array [1..15000, 1..10000, 1..7, 1..4] of integer;
For you, it seems that just including this declaration in your code is sufficient to make the debugger throw the error you quote.
For me the debugger starts fine but I get a SIGSEGV error on the assignment to p[] in the following code:
var
p: array [1..15000, 1..10000, 1..7, 1..4] of integer;
x,
y,
r : integer;
begin
x := 100;
y := 100;
r := 666;
p[x, y, 1, 1] := r;
writeln('Press any key ...');
readln;
end.
So, I would try smaller values for the first two bounds of the p array. If that works and you still need the original bounds, I would suggest looking for an FPC library which implements "sparse arrays" and declare p as one of those.
Good luck!
The following code:
program Default_Issue;
function SomeFunction(var myVar : integer;
defaulted : boolean = true) : integer;
begin
writeln('Inside SomeFunction');
SomeFunction := 0
end;
var
i : integer;
begin
SomeFunction(i,true);
end.
Gets a compiler error pointing to the = sign in the function declaration:
Default_Issue.pas(3,64) Fatal: Syntax error, ")" expected but "=" found
The program will compile if the function declaration line has the default parameter value removed:
function SomeFunction(var myVar : integer;
defaulted : boolean) : integer;
In the Free Pascal manual it shows default parameters as an option for value parameters and the syntax appears to be "var-name : type = value" so I don't understand why I am getting the compiler error.
Features are divided over languages modes. Default parameters is a delphi feature and some of those are only enabled in the objfpc and Delphi dialects.
The default (FPC) dialect is Turbo Pascal compatible, and TP didn't have default parameters.
Add -S2 or -Sd parameters. (FPC mode has overloading though, which can be used to mimic it)
Sometimes there is confusion because Lazarus defaults to objfpc mode by passing extra parameters.
Free Pascal has five compiler modes: (FPC, OBJFPC, TP, DELPHI, MACPAS).
The default compiler mode is FPC. Default function/procedure parameters are not supported in FPC mode. To use them you have to change the compiler mode to one that supports default function/procedure arguments. This can be done in the source code or on the command line. Default parameters are supported in the OBJFPC mode (the extended Free Pascal mode that gives you all the Free Pascal features) and also in the DELPHI mode (which aims for as much Delphi compatibility as possible).
To change the compiler mode in source code use the {$mode xxxx} compiler directive:
Program UseDefaultParameters;
{$mode OBJFPC}
The people who wrote the Lazarus IDE must think that this should be the default mode as they automatically add this source code compile mode whenever you ask it to create a new (Unit, Application, Program, Console Application, Library, InstantFPC program, FPCUnit Console Test Application, FCPUnit Test Application) file. The only time that the Lazarus IDE creates a file that doesn't have the OBJFPC compiler mode set is when it creates a new Simple Program.
To change the compiler mode on the command line use the -M compiler mode switch:
fpc -Mobjfpc my_program.pas
As I can see, your problem is in the function declaration, unexpected bracket after integer:
wrong:
function SomeFunction(var myVar : integer; defaulted : boolean = true) : integer )
right:
function SomeFunction(var myVar : integer; defaulted : boolean = true) : integer;