# Ill-formed phrase. "{" should be followed by a statement, variable declaration or expression

**URL:** <https://discourse.mc-stan.org/t/ill-formed-phrase-should-be-followed-by-a-statement-variable-declaration-or-expression/32400>\
**Category:** General\
**Tags:** specification\
**Created:** [August 9, 2023, 10:54am UTC](https://discourse.mc-stan.org/t/ill-formed-phrase-should-be-followed-by-a-statement-variable-declaration-or-expression/32400 "2023-08-09T10:54:15Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bus\_Acc](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bus_acc/32/16899_2.png) [@Bus\_Acc](https://discourse.mc-stan.org/u/Bus_Acc)\
**Post date:** [August 9, 2023, 10:54am UTC](https://discourse.mc-stan.org/t/ill-formed-phrase-should-be-followed-by-a-statement-variable-declaration-or-expression/32400/1 "2023-08-09T10:54:15Z")

</div>

I am still new to rstan library, and I am trying to apply Bayesian inference using rstan, I wrote the following stan file, but I am receiving the following error.

```plaintext
functions {
  real matern_kernel_2_5(vector x1, vector x2, real length_scale) {
    real nu = 2.5;
    real kappa = sqrt(5 * nu);
    real dist = sqrt(dot_self(x1 - x2));
    real term1 = 1 + kappa * dist / length_scale;
    real term2 = exp(-kappa * dist / length_scale);
    real covariance = term1 * term2;
    return covariance;
  }
}

data {
  int<lower=1> N; // Number of data points
  vector[N] size; // Size variable
  vector[N] strain; // Strain variable
  matrix[N, N] x; // Input features for covariance matrix
  vector[N] stress; // Observed stress data
}

parameters {
  real<lower=0> sigma; // Noise parameter
  real a; // Intercept term
  real bsize; // Coefficient of size variable
  real bstrain; // Coefficient of strain variable
}

model {
  // Priors for hyperparameters
  a ~ normal(0, 1); // Prior for intercept term
  bsize ~ normal(0, 1); // Prior for coefficient of size variable
  bstrain ~ normal(0, 1); // Prior for coefficient of strain variable
  
  matrix[N, N] cov_matrix;
  
  // Construct the covariance matrix using the Matérn kernel
  for (i in 1:N) {
    for (j in 1:N) {
      cov_matrix[i, j] = matern_kernel_2_5(x[i], x[j], 1.0); // Use 1.0 as length_scale for simplicity
      if (i == j) cov_matrix[i, j] = cov_matrix[i, j] + sigma^2;
    }
  }
  
  // Model likelihood
  stress ~ multi_normal(a + bsize * size + bstrain * strain, cov_matrix);
}

```

I recieved the following error:

```plaintext
stan_model <- stan_model(file = "C:/Users/GTS/Desktop/R file/multivariate_matern_priors.stan")
Error in stanc(file = file, model_code = model_code, model_name = model_name, : 
  0

Syntax error in 'string', line 36, column 6 to column 16, parsing error:

Ill-formed phrase. "{" should be followed by a statement, variable declaration or expression.

In addition: Warning message:
In readLines(file, warn = TRUE) :
  incomplete final line found on 'C:\Users\GTS\Desktop\R file\multivariate_matern_priors.stan'
> 

```

I really appreciate you help.

---

<div class="post-metadata">

**Author:** ![Bus\_Acc](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bus_acc/32/16899_2.png) [@Bus\_Acc](https://discourse.mc-stan.org/u/Bus_Acc)\
**Post date:** [August 9, 2023, 6:04pm UTC](https://discourse.mc-stan.org/t/ill-formed-phrase-should-be-followed-by-a-statement-variable-declaration-or-expression/32400/3 "2023-08-09T18:04:07Z")

</div>

it did not work I modified the code as following

```plaintext
functions {
  real matern_kernel_2_5(vector x1, vector x2, real length_scale) {
    real nu = 2.5;
    real kappa = sqrt(5 * nu);
    real dist = sqrt(dot_self(x1 - x2));
    real term1 = 1 + kappa * dist / length_scale;
    real term2 = exp(-kappa * dist / length_scale);
    real covariance = term1 * term2;
    return covariance;
  }
}

data {
  int<lower=1> N; // Number of data points
  vector[N] size; // Size variable
  vector[N] strain; // Strain variable
  matrix[N, N] x; // Input features for covariance matrix
  vector[N] stress; // Observed stress data
}

parameters {
  real<lower=0> sigma; // Noise parameter
  real a; // Intercept term
  real bsize; // Coefficient of size variable
  real bstrain; // Coefficient of strain variable
}

model {
  // Priors for hyperparameters
  a ~ normal(0, 1); // Prior for intercept term
  bsize ~ normal(0, 1); // Prior for coefficient of size variable
  bstrain ~ normal(0, 1); // Prior for coefficient of strain variable
  matrix[N, N] cov_matrix;
  for (i in 1:N) {
    for (j in 1:N) {
      cov_matrix[i, j] = matern_kernel_2_5(x[i], x[j], 1.0); // Use 1.0 as length_scale for simplicity
      if (i == j) cov_matrix[i, j] = cov_matrix[i, j] + sigma^2;
    }
  }
  
  // Model likelihood
  stress ~ multi_normal(a + bsize * size + bstrain * strain, cov_matrix);
}

```

And I still getting the following error:

```plaintext
stan_model <- stan_model(file = "C:/Users/GTS/Desktop/R file/multivariate_matern_priors.stan")
Error in stanc(file = file, model_code = model_code, model_name = model_name, : 
  0

Syntax error in 'string', line 33, column 6 to column 16, parsing error:

Ill-formed phrase. "{" should be followed by a statement, variable declaration or expression.

In addition: Warning message:
In readLines(file, warn = TRUE) :
  incomplete final line found on 'C:\Users\GTS\Desktop\R file\multivariate_matern_priors.stan'

```

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [August 9, 2023, 7:21pm UTC](https://discourse.mc-stan.org/t/ill-formed-phrase-should-be-followed-by-a-statement-variable-declaration-or-expression/32400/4 "2023-08-09T19:21:20Z")

</div>

> [@Bus\_Acc](#):
>
> `matrix[N, N] cov_matrix;`

I think the error is because `cov_matrix` is also the name of a type in Stan. Can you try changing all uses of `cov_matrix` to `cov_mat` or something else and see if that fixes the error (if it does then @mitzimorris I think we need a better error message here… well actually either way we need a better error message).

If that fixes the error I think you’ll get another error though because in

> [@Bus\_Acc](#):
>
> `matern_kernel_2_5(x[i], x[j], 1.0)`

`x[i]` and `x[j]` are rows of the `x` matrix and I think `matern_kernel_2_5` will want vectors and not row vectors. If that’s the case then you can transpose them using `'`

```stan
matern_kernel_2_5(x[i]', x[j]', 1.0)

```

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [August 9, 2023, 7:45pm UTC](https://discourse.mc-stan.org/t/ill-formed-phrase-should-be-followed-by-a-statement-variable-declaration-or-expression/32400/5 "2023-08-09T19:45:42Z")

</div>

here’s the list of reserved words: [6.2 Variables | Stan Reference Manual](https://mc-stan.org/docs/reference-manual/variables.html#reserved-words-from-stan-language)

regarding error messages - @WardBrian - can the parser detect reserved words used for a variable name?

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [August 9, 2023, 7:50pm UTC](https://discourse.mc-stan.org/t/ill-formed-phrase-should-be-followed-by-a-statement-variable-declaration-or-expression/32400/6 "2023-08-09T19:50:26Z")

</div>

> [@mitzimorris](#):
>
> here’s the list of reserved words: [6.2 Variables | Stan Reference Manual](https://mc-stan.org/docs/reference-manual/variables.html#reserved-words-from-stan-language)

Thanks, so yeah that’s definitely the issue here. `cov_matrix` is in that list. But there would be no way to know to check that from reading the error message, so it would be great if we could detect this and have a more informative message.

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [August 9, 2023, 9:57pm UTC](https://discourse.mc-stan.org/t/ill-formed-phrase-should-be-followed-by-a-statement-variable-declaration-or-expression/32400/7 "2023-08-09T21:57:34Z")

</div>

> [@mitzimorris](#):
>
> regarding error messages - @WardBrian - can the parser detect reserved words used for a variable name?

It can, but only if it succeeds in parsing. If you comment out everything below the declaration, the error message is

```no-highlight
Semantic error in 'string', line 34, column 15 to column 25:
   -------------------------------------------------
    32: bstrain ~ normal(0, 1); // Prior for coefficient of strain variable
    33:    
    34: matrix[N, N] cov_matrix;
                        ^
    35:    
    36: // Construct the covariance matrix using the Matérn kernel
   -------------------------------------------------

Identifier 'cov_matrix' clashes with reserved keyword.

```

But we never get there because the like ` cov_matrix[i, j] = matern_kernel_2_5(x[i], x[j], 1.0); // Use 1.0 as length_scale for simplicity` fails to parse (in this case with not a great error).

We have a note in the parsing code that says “Keywords cannot be identifiers but semantic check produces a better error message.”. I suspect this is not (or at least is not any longer) true, so we can probably fix this.

---

<div class="post-metadata">

**Author:** ![nhuurre](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@nhuurre](https://discourse.mc-stan.org/u/nhuurre)\
**Post date:** [August 10, 2023, 8:22am UTC](https://discourse.mc-stan.org/t/ill-formed-phrase-should-be-followed-by-a-statement-variable-declaration-or-expression/32400/8 "2023-08-10T08:22:57Z")

</div>

> [@WardBrian](#):
>
> We have a note in the parsing code that says “Keywords cannot be identifiers but semantic check produces a better error message.”. I suspect this is not (or at least is not any longer) true, so we can probably fix this.

Looking at the [pull request that introduced this](https://github.com/stan-dev/stanc3/pull/771/files#diff-1e015a1a407a4f859ed77d70d1caba7a232a04796bc3fdeebf17609da78c0858), I believe it was intended to get rid of the “`UNREACHABLE` token” hack, but the author forgot that of course parsing could fail before semantic check anyway and there are no added tests whatsoever. Bringing back `UNREACHABLE` is the best option here.

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [August 10, 2023, 12:34pm UTC](https://discourse.mc-stan.org/t/ill-formed-phrase-should-be-followed-by-a-statement-variable-declaration-or-expression/32400/9 "2023-08-10T12:34:13Z")

</div>

I think I found an option I like better than `UNREACHABLE`, I will open a PR later today

---

<div class="post-metadata">

**Author:** ![Bus\_Acc](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bus_acc/32/16899_2.png) [@Bus\_Acc](https://discourse.mc-stan.org/u/Bus_Acc)\
**Post date:** [August 10, 2023, 2:20pm UTC](https://discourse.mc-stan.org/t/ill-formed-phrase-should-be-followed-by-a-statement-variable-declaration-or-expression/32400/10 "2023-08-10T14:20:02Z")

</div>

Thank you it worked fine.  
However, I am wondering what is the sampling method used by stan  
by running the following ` sampling(stan_model, data = stan_data, chains = 4, iter = 1000)` is it the HMC (Hamiltonian Monte Carlo)?

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [August 10, 2023, 2:31pm UTC](https://discourse.mc-stan.org/t/ill-formed-phrase-should-be-followed-by-a-statement-variable-declaration-or-expression/32400/11 "2023-08-10T14:31:06Z")

</div>

yes

---

<div class="post-metadata">

**Author:** ![Bus\_Acc](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bus_acc/32/16899_2.png) [@Bus\_Acc](https://discourse.mc-stan.org/u/Bus_Acc)\
**Post date:** [August 10, 2023, 2:48pm UTC](https://discourse.mc-stan.org/t/ill-formed-phrase-should-be-followed-by-a-statement-variable-declaration-or-expression/32400/12 "2023-08-10T14:48:33Z")

</div>

Thank you for your prompt response. I noticed that it takes too long to sample. It took about 13974.6 seconds to finish chain 1 and its now in the start of chain 2. Do you think pystan is faster? I have about 670 lines of data

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [August 10, 2023, 2:55pm UTC](https://discourse.mc-stan.org/t/ill-formed-phrase-should-be-followed-by-a-statement-variable-declaration-or-expression/32400/13 "2023-08-10T14:55:05Z")

</div>

I always recommend [CmdStanR](http://mc-stan.org/cmdstanr/) or [CmdStanPy](http://mc-stan.org/cmdstanpy/).  
they will use the latest version of Stan, which may be faster.  
however, the slowness is probably due to the model, specifically, the number of gradient evaluations, not the interface.
