Recent

Author Topic: Fatal: Syntax Error, ")" expected but "," found  (Read 6677 times)

Klefo

  • Newbie
  • Posts: 1
Fatal: Syntax Error, ")" expected but "," found
« on: July 10, 2021, 04:36:42 am »
An error i have on this code
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$m+}{$N-}
  2. program Quadratic_fit;
  3. uses classes, Math;
  4. var i, j, k: integer;
  5.         x, y, coeff: array[0..100] of Double;
  6. const
  7.         x = (27.23103448,(*error from that comma*) 27.22068966, 27.25862069, 27.23793103, 27.12068966, 27.21034483, 27.59310345,
  8.         28.17931034, 28.54137931, 28.95517241, 29.45517241, 30.63793103, 30.17931034, 30.56551724, 30.13103448, 29.95172414,
  9.         29.68275862, 29.06206897, 28.38275862, 28.03793103, 27.66551724, 27.53448276, 27.43793103);
  10.         y = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19);
  11. type TLocalVector = array[0..100] of Real;
  12.      PolyReg = class //PolynomialRegression
  13.         private
  14.                 n, np1, np2, tnp1: LongInt;
  15.                 N1                               : LongInt;
  16.                 x1, y1, A                : TLocalVector;
  17.                 B: array of array of double;
  18.                 tmp, t : double;
  19.                 Len_x, Len_y, Len_a : LongInt;
  20.                 nm1: integer;
  21.                 result : boolean;
  22.         public
  23.  
  24.                 constructor init(); overload;
  25.                 destructor done(); virtual;
  26.                 function fitIt(const x: TLocalVector;
  27.                                            const y: TLocalVector;
  28.                                            const order: integer;
  29.                                            coeffs: TLocalVector) : boolean;
  30. end;
  31.  
  32. constructor PolyReg.init;
  33. Begin
  34.   inherited Create;
  35. End;
  36.  
  37. Destructor PolyReg.done;
  38. Begin
  39.   Inherited Destroy;
  40. end;
  41.  
  42. function PolyReg.fitIt(const x: TLocalVector;
  43.                                            const y: TLocalVector;
  44.                                            const order: integer;
  45.                                            coeffs: TLocalVector) : boolean;
  46. begin
  47.     result := true;
  48.         // The size of xValues and yValues should be same. (
  49.     if Length(x) <> Length(y) then
  50.     begin
  51.         writeln('Kich co cua mang X va mang Y khong bang nhau');
  52.         result := false;
  53.     end;
  54.         // The size of xValues and yValues cannot be 0, should not happen
  55.     Len_x := Length(x);
  56.     Len_y := Length(y);
  57.     if (Len_x = 0) or (Len_y = 0) then
  58.     begin
  59.         writeln('Kich co cua mang X hoac mang Y bang 0');
  60.         result := false;
  61.     end;
  62.  
  63.     n := order;
  64.     np1 := n+1;
  65.     np2 := n+2;
  66.     tnp1 := 2*n+1;
  67.         // X = vector that stores values of sigma(xi^2n).
  68.     SetLength(x1,tnp1);
  69.     for i := 0 to tnp1-1 do
  70.         begin
  71.             x1[i]:=0;
  72.                 for j := 0 to Len_x-1 do
  73.                     x1[i]+= power(x[j],i);
  74.         end;
  75.         // a = vector to store final coefficients.
  76.     SetLength(a,np1);
  77.         // B = normal augmented matrix that stores the equations.
  78.     SetLength(B,np1,np2); // n x m = np1 x np2
  79.     for i := 0 to n do
  80.       for j := 0 to n do
  81.         B[i][j] := x1[i+j];
  82.  
  83.         // Y = vector to store values of sigma(xi^n * yi).
  84.     SetLength(Y1,np1);
  85.     for i := 0 to np1-1 do
  86.         begin
  87.             y1[i] := 0;
  88.              for j := 0 to n1-1 do
  89.                 y1[i] += power(x[j],i)*y[i];
  90.         end;
  91.         // Load values of Y as last column of B.
  92.     for i := 0 to n do
  93.         B[i][np1] := y1[i];
  94.  
  95.     n += 1;
  96.     nm1 := n - 1;
  97.  
  98.         // Pivotisation of the B matrix.
  99.     for i := 0 to n-1 do
  100.         for k := i+1 to n-1 do
  101.            begin
  102.            if (B[i][i] < B[k][i]) then
  103.              for j := 0 to n do
  104.                 begin
  105.                     tmp := B[i][j];
  106.                     B[i][j] := B[k][j];
  107.                     B[k][j] := tmp;
  108.                 end;
  109.            end;
  110.  
  111.         // Performs the Gaussian elimination.
  112.         // (1) Make all elements below the pivot equals to zero
  113.         //     or eliminate the variable.
  114.     for i := 0 to nm1-1 do
  115.      for k := 0 to n-1 do
  116.         begin
  117.                         t := B[k][i] / B[i][i];
  118.               for j := 0 to n do
  119.                 B[k][i] -= t*B[i][j];   // (1)
  120.          end;
  121.  
  122.  
  123.         // Back substitution.
  124.         // (1) Set the variable as the rhs of last equation
  125.         // (2) Subtract all lhs values except the target coefficient.
  126.         // (3) Divide rhs by coefficient of variable being calculated.
  127.     for i := nm1 downto 0 do
  128.                 begin
  129.                         a[i] := B[i][n];                                        // (1)
  130.                         for j := 0 to n-1 do
  131.                           if j<>i then
  132.                                 a[i] -= B[i][j] * a[j];                 // (2)
  133.                         a[i] /= B[i][i];                                        // (3)
  134.                 end;
  135.     Len_a := Length(a);
  136.         SetLength(coeffs,Len_a);
  137.         for i := 0 to Length(a) - 1 do
  138.                 coeffs[i] := a[i];
  139.  
  140. end;
  141.  
  142. begin
  143.         PolyReg.init;
  144.         PolyReg.fitIt(x,y,10,coeffs);
  145.         write('coefficients:');
  146.         for j := 0 to 10 do
  147.                 write(' ',coeff[j]);
  148.         writeln;
  149.         PolyReg.done;
  150. end.
  151.  
I don't know why that comma is wrong, it nothing special.

440bx

  • Hero Member
  • *****
  • Posts: 6342
Re: Fatal: Syntax Error, ")" expected but "," found
« Reply #1 on: July 10, 2021, 05:11:22 am »
The reason it doesn't work is because your "x" and "y" constant declarations are attempting to assign multiple constants to a single constant name.

The solution is simply to make a minor change in the type and constant declaration.  Something along the lines of
Code: Pascal  [Select][+][-]
  1. type
  2.  TDARRAY = array[0..2] of double;
  3.  
  4.  
  5. const
  6.   x : TDARRAY = (27.23103448, 27.22068966, 27.25862069);
  7.  
That will be acceptable to the compiler.  (change the bounds to whatever bounds you need.)

HTH.

FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Fatal: Syntax Error, ")" expected but "," found
« Reply #2 on: July 10, 2021, 05:52:41 am »
Or if they are variables:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$m+}{$N-}
  2. program Quadratic_fit;
  3.  
  4. uses
  5.   Classes,
  6.   Math;
  7.  
  8. var
  9.   i, j, k: integer;
  10.   x: array of double = (27.23103448, 27.22068966,
  11.     27.25862069, 27.23793103, 27.12068966, 27.21034483, 27.59310345,
  12.     28.17931034, 28.54137931, 28.95517241, 29.45517241, 30.63793103,
  13.     30.17931034, 30.56551724, 30.13103448, 29.95172414, 29.68275862,
  14.     29.06206897, 28.38275862, 28.03793103, 27.66551724, 27.53448276, 27.43793103);
  15.   y: array of double = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  16.     12, 13, 14, 15, 16, 17, 18, 19,20,21,22,23);
  17.   coeffs: array of double;
  18. type
  19.   TLocalVector = array of double;//real;
  20.  
  21.   TPolyReg = class //PolynomialRegression
  22.   private
  23.     n, np1, np2, tnp1: longint;
  24.     N1: longint;
  25.     x1, y1, A: TLocalVector;
  26.     B: array of array of double;
  27.     tmp, t: double;
  28.     Len_x, Len_y, Len_a: longint;
  29.     nm1: integer;
  30.     Result: boolean;
  31.   public
  32.  
  33.     constructor init(); overload;
  34.     destructor done(); virtual;
  35.     function fitIt(const x: TLocalVector; const y: TLocalVector;
  36.       const order: integer; var coeffs: TLocalVector): boolean;
  37.   end;
  38.  
  39.   constructor TPolyReg.init;
  40.   begin
  41.     inherited Create;
  42.   end;
  43.  
  44.   destructor TPolyReg.done;
  45.   begin
  46.     inherited Destroy;
  47.   end;
  48.  
  49.   function TPolyReg.fitIt(const x: TLocalVector; const y: TLocalVector;
  50.   const order: integer; var coeffs: TLocalVector): boolean;
  51.   begin
  52.     Result := True;
  53.     // The size of xValues and yValues should be same. (
  54.     if Length(x) <> Length(y) then
  55.     begin
  56.       writeln('Kich co cua mang X va mang Y khong bang nhau');
  57.       Result := False;
  58.     end;
  59.     // The size of xValues and yValues cannot be 0, should not happen
  60.     Len_x := Length(x);
  61.     Len_y := Length(y);
  62.     if (Len_x = 0) or (Len_y = 0) then
  63.     begin
  64.       writeln('Kich co cua mang X hoac mang Y bang 0');
  65.       Result := False;
  66.     end;
  67.  
  68.     n := order;
  69.     np1 := n + 1;
  70.     np2 := n + 2;
  71.     tnp1 := 2 * n + 1;
  72.     // X = vector that stores values of sigma(xi^2n).
  73.     SetLength(x1, tnp1);
  74.     for i := 0 to tnp1 - 1 do
  75.     begin
  76.       x1[i] := 0;
  77.       for j := 0 to Len_x - 1 do
  78.         x1[i] += power(x[j], i);
  79.     end;
  80.     // a = vector to store final coefficients.
  81.     SetLength(a, np1);
  82.     // B = normal augmented matrix that stores the equations.
  83.     SetLength(B, np1, np2); // n x m = np1 x np2
  84.     for i := 0 to n do
  85.       for j := 0 to n do
  86.         B[i][j] := x1[i + j];
  87.  
  88.     // Y = vector to store values of sigma(xi^n * yi).
  89.     SetLength(Y1, np1);
  90.     for i := 0 to np1 - 1 do
  91.     begin
  92.       y1[i] := 0;
  93.       for j := 0 to n1 - 1 do
  94.         y1[i] += power(x[j], i) * y[i];
  95.     end;
  96.     // Load values of Y as last column of B.
  97.     for i := 0 to n do
  98.       B[i][np1] := y1[i];
  99.  
  100.     n += 1;
  101.     nm1 := n - 1;
  102.  
  103.     // Pivotisation of the B matrix.
  104.     for i := 0 to n - 1 do
  105.       for k := i + 1 to n - 1 do
  106.       begin
  107.         if (B[i][i] < B[k][i]) then
  108.           for j := 0 to n do
  109.           begin
  110.             tmp := B[i][j];
  111.             B[i][j] := B[k][j];
  112.             B[k][j] := tmp;
  113.           end;
  114.       end;
  115.  
  116.     // Performs the Gaussian elimination.
  117.     // (1) Make all elements below the pivot equals to zero
  118.     //     or eliminate the variable.
  119.     for i := 0 to nm1 - 1 do
  120.       for k := 0 to n - 1 do
  121.       begin
  122.         t := B[k][i] / B[i][i];
  123.         for j := 0 to n do
  124.           B[k][i] -= t * B[i][j];   // (1)
  125.       end;
  126.  
  127.  
  128.     // Back substitution.
  129.     // (1) Set the variable as the rhs of last equation
  130.     // (2) Subtract all lhs values except the target coefficient.
  131.     // (3) Divide rhs by coefficient of variable being calculated.
  132.  
  133.     {I am not sure about this correction
  134.     n:=n-1;//engkin
  135.     nm1:=n-1;//engkin}
  136.     for i := nm1 downto 0 do
  137.     begin
  138.       a[i] := B[i][n];                                        // (1)
  139.       for j := 0 to n - 1 do
  140.         if j <> i then
  141.           a[i] -= B[i][j] * a[j];                 // (2)
  142.       a[i] /= B[i][i];                                        // (3)
  143.     end;
  144.     Len_a := Length(a);
  145.     SetLength(coeffs, Len_a);
  146.     for i := 0 to Length(a) - 1 do
  147.       coeffs[i] := a[i];
  148.  
  149.   end;
  150.  
  151. var
  152.   PolyReg: TPolyReg;
  153. begin
  154.   PolyReg := TPolyReg.init;
  155.   WriteLn('fitIt: ',PolyReg.fitIt(x, y, 10, coeffs));
  156.   WriteLn('coefficients:');
  157.   for j := 0 to High(coeffs) do
  158.     WriteLn(' ', coeffs[j]);
  159.   writeln;
  160.   PolyReg.done;
  161.   ReadLn;
  162. end.

Paolo

  • Hero Member
  • *****
  • Posts: 694
Re: Fatal: Syntax Error, ")" expected but "," found
« Reply #3 on: July 10, 2021, 10:57:37 am »
but to me it seems strange that the compiler did not say "duplicate identifier" in the original code, "x "seems declared twice both var and const !

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$m+}{$N-}
  2. program Quadratic_fit;
  3. uses classes, Math;
  4. var i, j, k: integer;
  5.         x, y, coeff: array[0..100] of Double;
  6. const
  7.         x = (27.23103448,(*error from that comma*) 27.22068966, 27.25862069, 27.23793103, 27.12068966, 27.21034483, 27.59310345,
  8.         28.17931034, 28.54137931, 28.95517241, 29.45517241, 30.63793103, 30.17931034, 30.56551724, 30.13103448, 29.95172414,
  9.         29.68275862, 29.06206897, 28.38275862, 28.03793103, 27.66551724, 27.53448276, 27.43793103);
  10.         y = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19);
  11.  

PascalDragon

  • Hero Member
  • *****
  • Posts: 6381
  • Compiler Developer
Re: Fatal: Syntax Error, ")" expected but "," found
« Reply #4 on: July 11, 2021, 01:15:31 pm »
but to me it seems strange that the compiler did not say "duplicate identifier" in the original code, "x "seems declared twice both var and const !

Because the compiler did not come that far. A syntax error is a critical error and thus will usually lead to a stop of the parsing. At this point the compiler has not yet added the constant x to the symbol table and thus it could not yet detect that it's a duplicate of the variable x.

Paolo

  • Hero Member
  • *****
  • Posts: 694
Re: Fatal: Syntax Error, ")" expected but "," found
« Reply #5 on: July 11, 2021, 07:08:35 pm »
@PascalDragon, thanks to clarify this.

 

TinyPortal © 2005-2018