LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-08-2004, 10:03 AM   #1
reetep
Member
 
Registered: Oct 2003
Location: UK
Distribution: Debian
Posts: 122

Rep: Reputation: 15
newbie compile problem: parse errors


I've written my first program in c, which is a translation of another program I wrote in pascal.

It won't compile!!! Typically the error message is a parse error. I've got rid of most of them , but there are about 5-10 stubborn ones that I can't resolve, desipte two days' worth of googling.

If anyone would be willing to take a look and point me in the right direction I'd be very grateful.

Below is the program and the list of errors returned by the compiler.


The program gives a numerical solution to a differential equation using a choice of two methods: Euler method and RK4 method. To see the structure of it, simply scroll to the bottom and read int main()
and it will be obvious straight away.

If you want to try compiling this then let me know reetep29@hotmail.com and I can email it to you. Same goes for the source file and the errorlog.txt.


Many thanks, reetep.







#include <catam.h>

float X_0[2] = {-0.0008,0.0006};

int A[2][2] = {1,-3,
-4,2};
float x[2]; /*main variables x1,x2*/
float steplength;
int iterations;






void ArrayLinComb(int dim, float coeff1, float array1[dim], float coeff2, float array2[dim], float ObjectArray[dim])
/*Calculates given linear combination of arrays: ie does ObjectArray[i]=coeff1*array1[i] + coeff2*array2[i] for all i=0...dim-1 */
{
int i;
for(i=0;i<dim;i=i+1)
ObjectArray[i]=coeff1*array1[i] + coeff2*array2[i];
}





void Get_Ax(float v[2]) /*takes a vector x and premultiplies it by matrix A. Ax is then returned*/
{
float temp[2];

temp[0] = A[0][0]*v[0] + A[0][1]*v[1];
temp[1] = A[1][0]*v[0] + A[1][1]*v[1];

v[0]=temp[0];
v[1]=temp[1];
}





void GetParameters() /*prompts user for input of steplength and xmax*/
{
char dummychar;

re_GetP:
printf("Please enter the following parameters:\n\n");
printf("Step length h = ");
fflush(stdout);
scanf("%f",&steplength);
if (steplength <= 0)
{
printf("Please enter a step length h > 0.");
goto re_GetP;
}

printf("Number of steps desired = ");
fflush(stdout);
scanf("%d",&iterations);
if (iterations<0)
{printf("Please enter a value >= 0. Try again.");
goto re_GetP;
}
printf("done now with get.");
fflush(stdout);
} /* end procedure GetParameters */





void Do_RK() /*performs algorithm for Runge-Kutta method, stores results*/
{
int i,j; /*dummy counter for 'for' statement*/
float unitarray[2] = {1,1}; /*needed for use in ArrayLinComb*/
float tempvar[2] = {0,0};

float k_1[2],k_2[2],k_3[2],k_4[2]; /*vector variables for RK method*/
FILE *fp;
FILE *gp;

fp = fopen("RK2Dx.txt","w");
gp = fopen("RK2Dy.txt","w");



fprintf(fp,"Doing RK with steplength %f and %d iterations\n\n",steplength,iterations);
fprintf(gp,"Doing RK with steplength %f and %d iterations\n\n",steplength,iterations);


ArrayLinComb(2,1,X_0,0,unitarray,x); /* initialise x,y at start points. Equivalent to x[i]=X_0[i] for all i */



for(i=0;i <= iterations;i=i+1) /* This loop records current value of x,y then iterates the calculation and repeats */
{

fprintf(fp,"%f\n",x[0]);
fprintf(gp,"%f\n",x[1]);


ArrayLinComb(2,1,x,0,unitarray,tempvar); /* read as "tempvar=x" ie we really just need x here in next line but tempvar used to be consistent with following three chunks */
Get_Ax(tempvar);
ArrayLinComb(2,steplength,tempvar,0,unitarray,k_1); /* read as k_1 = steplength*tempvar ie all elements of array multiplied by steplength */

ArrayLinComb(2,0.5,k_1,1,x,tempvar);
Get_Ax(tempvar);
ArrayLinComb(2,steplength,tempvar,0,unitarray,k_2); /* as above but now k_2 = steplength*tempvar */

ArrayLinComb(2,0.5,k_2,1,x,tempvar);
Get_Ax(tempvar);
ArrayLinComb(2,steplength,tempvar,0,unitarray,k_3); /*as above but now k_3 = steplength*tempvar */

ArrayLinComb(2,1,k_3,1,x,tempvar);
Get_Ax(tempvar);
ArrayLinComb(2,steplength,tempvar,0,unitarray,k_4); /* as above but now k_4 = steplength*k_4 */


for(j=0;j<2;j=j+1)
x[j] = ( x[j] + (k_1[j] + 2*k_2[j] +2*k_3[j] + k_4[j])/6 );

}

fclose(fp);
fclose(gp);


printf("Solution (using RK method) complete.");
fflush(stdout);
/*delay(3000);*/
} /*end procedure Do_RK*/





int main() /*main program*/
{
GetParameters();
Do_RK();
return 0;
} /*end main program*/










Now the error log:

diff.c: In function `GetParameters':
diff.c:87: parse error before `;'
diff.c: At top level:
diff.c:93: parse error before `if'
diff.c:97: conflicting types for `Xmax'
diff.c:6: previous declaration of `Xmax'
diff.c:97: parse error before `;'
diff.c:97: warning: data definition has no type or storage class
diff.c:100: parse error before string constant
diff.c:100: warning: data definition has no type or storage class
diff.c:101: parse error before string constant
diff.c:101: warning: data definition has no type or storage class
diff.c:102: parse error before string constant
diff.c:102: warning: data definition has no type or storage class
diff.c:104: parse error before string constant
diff.c:104: warning: data definition has no type or storage class
diff.c:105: parse error before string constant
diff.c:105: warning: data definition has no type or storage class
diff.c:106: parse error before `('
diff.c:107: parse error before string constant
diff.c:107: warning: data definition has no type or storage class
diff.c: In function `Get_Local_Error':
diff.c:130: parse error before `;'
diff.c: At top level:
diff.c:147: parse error before `}'
diff.c: In function `main':
diff.c:267: warning: assignment makes integer from pointer without a cast
 
Old 01-08-2004, 10:09 AM   #2
keegan
Member
 
Registered: Jan 2004
Distribution: Redhat WS and VectorLinux
Posts: 41

Rep: Reputation: 15
do you think you could make line 87 bold? i cant figure out which like is 87 heh.
 
Old 01-08-2004, 10:11 AM   #3
luxitan
Member
 
Registered: Aug 2003
Location: Portugal
Distribution: Gentoo
Posts: 78

Rep: Reputation: 15
very strange bu i changed the
#include <catam.h>

to

#include <stdio.h>

and it compiled:

Doing RK with steplength 1.000000 and 2 iterations

-0.000800
-0.039292
-2.564357

Doing RK with steplength 1.000000 and 2 iterations

0.000600
0.052233
3.419090
 
Old 01-08-2004, 10:24 AM   #4
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
What is catam.h? I am pretty sure that isn't a standard link C library.
 
Old 01-08-2004, 04:06 PM   #5
reetep
Member
 
Registered: Oct 2003
Location: UK
Distribution: Debian
Posts: 122

Original Poster
Rep: Reputation: 15
catam.h is provided by my university and gives access to a set of prewritten libraries.

I don't really know the details but if you want to see the file then i can post it for you; it's not very big.

Thank you very much for discovering that it compiles without catam.h. That's great. I wasn't using any of the provided libraries anyway, so I really should have thought of that. I simply assumed I had made a syntax error or something. I'm new to c, you see.

Thanks to all for helping, I'll try compiling that tomorrow when I'm back on my own computer. If it works for me on my machine then I consider the problem solved, so thank you very much indeed. That's great.

Of course if somebody has an idea why it's not working then please let us all know, cos it's worth learning!

Thank you again, reetep.
 
Old 01-09-2004, 08:32 AM   #6
reetep
Member
 
Registered: Oct 2003
Location: UK
Distribution: Debian
Posts: 122

Original Poster
Rep: Reputation: 15
Oh no!!!

I'm back on my computer and it still won't compile despite the change from catam.h to stdio.h. I get exactly the same set of errors as before.

I tried booting into linux and compiling it there instead, but same result. Did you make any other changes luxitan?

If anybody can help, then please do.

Here is the code again, with (most) errors in bold:


#include <catam.h>

#define X_0 0;
#define Y_0 4;

float Xmax, steplength, x,y, old_x, old_y;
int method, iterations; /* method is boolean */

float Get_Grad (float,float); /* function prototype for
get_grad */
int Choose_method (); /* (boolean-style) function
prototype for Choose_method */
void GetParameters(); /* function declaration prototype
for GetParameters*/
float Get_Local_Error(); /* function declaration prototype
for Get_Local_Error */
void DoEuler(); /* function declaration prototype
for DoEuler */
void Do_RK(); /*function declaration prototype
for Do_RK*/
void Get_exact_values(); /*function declaration prototype
for Get_exact_calues*/









float Get_Grad(float s,float t)
{
return -(6*t + 2*s*s*s -11*s*s + 20*s + 4);
}




int Choose_method ()
{ /*begin procedure ChooseMethod*/
int dummyint;

Choose_methodrebegin:
/*clrscr;*/
printf("This program computes numerical solutions to the equation: \n");
printf("f(x,y) = -(6y + 2x^3 -11x^2 + 20x + 4)\n");
printf("subject to the initial condition: y(o) = 4\n\n");
printf("You may choose between two methods:\n\n");
printf("1) EULER method.\n");
printf("2)RUNGE-KUTTA method.\n\n");
printf("Please enter your choice (1 or 2): ");
scanf("%d",&dummyint);
switch(dummyint)
{
case 1:
printf("EULER method selected.");
return 1;
break;
case 2:
printf("RUNGE-KUTTA method selected.");
return 2;
break;
default:
printf("Bad entry; please try again.");
goto Choose_methodrebegin;
break;
}

} /*end procedure ChooseMethod*/



void GetParameters() /*prompts user for input of
steplength and xmax*/
{
char dummychar;

re_GetParameters:

printf("Please enter the following parameters:\n\n");
printf("Step length h = ");
fflush(stdout);
scanf("%f",&steplength);
if (steplength<=0)
{
printf("Please enter a step length h > 0.");
goto re_GetParameters;
}

printf("Maximum x-value = ");
fflush(stdout);
scanf("%f",&Xmax);

if (Xmax<=X_0) /*line 87*/
{
printf("Please enter a value Xmax >= X_0=%f",X_0);
goto re_GetParameters;
}

if ( (Xmax/Steplength) != rint((Xmax/Steplength)) ) /*line 93*/
{
iterations = floor( (Xmax/steplength) ) +1;
/*iterations becomes no. steps
required to pass Xmax with given step length*/
Xmax = ( X_0 + steplength*iterations );
/*see note on variable iterations 2
lines above*/

printf("\n The maximum x-value you supplied for Xmax is not an integer multiple of the step value h. "); /*line 100*/
printf("\n Xmax has been changed to Xmax= ");
printf("%f",Xmax);

printf("\n\n This is the least integer multiple of the steplength greater
than the given value of Xmax.");
printf("\n\n Press r to re-enter data, or any other key to continue. Your
choice: ");
fflush(stdout);
scanf("%c",&dummychar); /*line 107*/
switch(dummychar)
{
case r:
goto re_GetParameters;
break;
default:
break;
}
}
} /* end procedure GetParameters */



float Get_Local_Error() /*returns value of local error
using global variables x,y,old_x,old_y*/
{
float old_exact_y, /*exact value of y at x_(n-1)*/
exact_y, /*exact value of y at x_n*/
projected_y, /*projected value of y at x_n,
based on exact y at x_(n-1)*/
h, /*=steplength - shorter name is
more convenient//
k_1,k_2,k_3,k_4; /*temp variables used for RK
method*/


if (x==X_0) return 0; /*line 130*/ /*e_0 doesn't exist but call it 0*/
else
{
h = steplength;
old_exact_y =
4*exp(-6*(old_x))-(old_x)*(old_x)*(old_x)/3+2*(old_x)*(old_x)-4*(old_x);
exact_y = 4*exp(-6*x)-x*x*x/3+2*x*x-4*x;
if (method == 1) projected_y = old_exact_y + steplength *
get_gradient(old_x,old_exact_y); /*finds local error if Euler Method is
being used*/
else /*finds local error if RK is being
used*/
{
k_1 = h*Get_gradient(old_x,old_exact_y);
k_2 = h*Get_gradient(old_x + 0.5 * h, old_exact_y+0.5*k_1);
k_3 = h*Get_gradient(old_x + 0.5 * h, old_exact_y+0.5*k_2);
k_4 = h*Get_gradient(old_x + h, old_exact_y+k_3);
projected_y = old_exact_y + (k_1 + 2*k_2 +2*k_3 + k_4)*(1/6);
}
return projected_y - exact_y;
}
} /*line 147*/ /*end functionGet_Local_Error*/



void DoEuler() /*performs Algorithm for
Euler's method. Stores resutls in files*/
{
int counter; /*dummy var acts as
counter for the for statement*/
float LocalError, GlobalError;
FILE *fp;
FILE *gp;
FILE *hp;

iterations=rint(Xmax/steplength); /*have enforced good
parameter entry, so Xmax/steplength is already integer; round converts to
integer type*/
fp = fopen("c:\\tp\\datfiles\\catam\\c\\1_1EULER.txt","w");
gp = fopen("c:\\tp\\datfiles\\catam\\c\\1_1E-LEr.txt","w");
hp = fopen("c:\\tp\\datfiles\\catam\\c\\1_1E-Ger.txt","w");

y=Y_0;
x=X_0;

for(counter = 0;counter <= iterations;counter=counter+1) /*count from 0
to no of iterations so that include x_0 as well as subsequent n points*/
{
fprintf(fp,"%f\n",y); /*write y_n to file, each
entry ending with a newline marker*/
fprintf(gp,"%f\n",Get_Local_Error);
GlobalError = y - (4*exp(-6*x) - x*x*x/3 + 2*x*x - 4*x);
fprintf(hp,"%f\n",GlobalError);

old_y = y;
y = y + Get_gradient(x,y) * steplength; /*finds projected
approx value y_n*/

old_x = x;
x = x + steplength;
}

fclose(fp);
fclose(gp);
fclose(hp);

printf("Solution (using Euler method) complete.");
delay(3000);
} /*end procedure
DoEuler*/



void Do_RK() /*performs algorithm
for Runge-Kutta method, stores results*/
{
int counter; /*dummy counter for for
statement*/

float k_1,k_2,k_3,k_4, /*variables for RK
method*/
localerror,globalerror; /*temp variables for
RK algorithm*/
FILE *fp;
FILE *gp;
FILE *hp;

iterations = rint(Xmax/steplength); /*we have enforced
good parameter entry, so Xmax/steplength is already integer; Round converts to
integer type*/

fp = fopen("c:\\tp\\datfiles\\catam\\c\\1_1R-Kut.txt","w");
gp = fopen("c:\\tp\\datfiles\\catam\\c\\1_1RKLer.txt","w");
hp = fopen("c:\\tp\\datfiles\\catam\\c\\1_1RKGer.txt","w");

y = Y_0;
x = X_0;

for(counter=0;counter <= iterations;counter++)
{
fprintf(fp,"%f\n",y);
fprintf(gp,"%f\n",Get_Local_Error);
globalerror = y - ( 4*exp(-6*x) - x*x*x/3 + 2*x*x - 4*x );
fprintf(hp,"%f\n",globalerror);

k_1 = steplength*Get_gradient(x,y);
k_2 = steplength*Get_gradient(x+0.5*steplength,y+0.5*k_1);
k_3 = steplength*Get_gradient(x+0.5*steplength,y+0.5*k_2);
k_4 = steplength*Get_gradient(x+steplength,y+k_3);

old_y = y;
y = y + (k_1 + 2*k_2 +2*k_3 + k_4)*(1/6);

old_x = x;
x = x + steplength;
}

fclose(fp);
fclose(gp);
fclose(hp);

printf("Solution (using RK method) complete.");
delay(3000);
} /*end procedure
Do_RK*/







void Get_exact_values()
{
int counter;

FILE *ip;
ip = fopen("c:\\tp\\datfiles\\catam\\1_1exact.txt","w");

iterations=rint(Xmax/steplength); /*we have enforced
good parameter entry, so Xmax/steplength is already integer; rint converts to
integer type*/

x = 0;
for(counter=0;counter<=iterations;counter++)
{fprintf(ip,"%f",(4*exp(-6*x) - x*x*x/3 + 2*x*x -4*x));
x = x+steplength;
}
fclose(ip);
} /*end procedure
Get_exact_values*/






int main() /*main program*/
{
method = Choose_method; /*line 267*/
GetParameters;
if (method == 1) DoEuler;
else Do_RK;
Get_exact_values;
return 0;
} /*end main
program*/
 
Old 01-09-2004, 10:24 AM   #7
reetep
Member
 
Registered: Oct 2003
Location: UK
Distribution: Debian
Posts: 122

Original Poster
Rep: Reputation: 15
Sorry to mess you all about, a friend has now helped me. For some of it, we just chopped the 'bad' code. It only made sure you input sensible values, so it wasn't essential to my work.



I now just receive one warning when I compile:

diff.c:236: warning: assignment makes integer from pointer without a cast.

I have an integer variable, and a function returning an integer and my code says:

myintvariable=myintfunction


well actually it says:

method = Choose_method;


and i have made the declarations thus:

int method;

int Choose_method ()
{
blah blah blah
}




However, if just press f8 (ie compile) again, it compiles without the warning. Is my code bad, or does it give a warning every time you use this kind of tool?

If anyone can help me understand, then please do.

Many thanks, reetep.
 
Old 01-09-2004, 10:29 AM   #8
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Try this:
method = Choose_method();

If you don't have the () then Choose_method is a pointer to the function.
 
Old 01-09-2004, 10:40 AM   #9
reetep
Member
 
Registered: Oct 2003
Location: UK
Distribution: Debian
Posts: 122

Original Poster
Rep: Reputation: 15
Thank you so much!!!


My program was doing absolutely nothing after compiling that way, and I knew something was fishy because it was supposed to ask for input before it even did anything else. It simply finished saying process myfile.exe finished.

It all works as it should now, my data even matches the test data so I'm very pleased.

Thanks for pointing out my mistake jtshaw, that's one syntax difference between pascal and c i didn't pick up. Thank you also to everyone who took the time to view my post and help me.

reetep.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie bash path parse problem ericcarlson Linux - Newbie 4 08-05-2004 10:43 AM
how to solve parse errors in C program?? divya Linux - Software 4 04-01-2004 11:19 AM
Driver compile parse errors Livia Linux - General 1 02-18-2004 09:25 AM
pthread problems and parse errors Stabby Red Hat 1 01-28-2004 10:56 PM
Parse errors in kernel.h using gcc 2.95 hate_msft Programming 2 07-24-2002 07:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration