Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

declare and define does not work at the top of a block without curly braces #2610

Closed
bgoodri opened this issue Aug 20, 2018 · 5 comments · Fixed by stan-dev/stanc3#1207
Closed

Comments

@bgoodri
Copy link
Contributor

bgoodri commented Aug 20, 2018

Summary:

Declare and define does not work at the top of a block without curly braces.

Description:

There is a parser error if you attempt to declare and define does not work at the top of a block without curly braces.

Reproducible Steps:

Parse this

functions {
  real foo() {
    if (1)
      real this_does_not_parse = 0;
    return 0;
  }
}

Current Output:

SYNTAX ERROR, MESSAGE(S) FROM PARSER:

Unknown variable: real
variable "real" does not exist.
  error in 'model_parser' at line 4, column 11
  -------------------------------------------------
     2:   real foo() {
     3:     if (1)
     4:       real this_does_not_parse = 0;
                  ^
     5:     return 0;
  -------------------------------------------------

Expected Output:

Nothing

Additional Information:

None

Current Version:

v2.18.0

@mitzimorris
Copy link
Member

mitzimorris commented Aug 20, 2018

a combined variable declaration and definition is a variable declaration; it is not a statement, according to the current set of grammars.

the body of a for loop can be either a single statement or a sequence of statements.
a sequence of statements consists of:

  • '{'
  • zero or more variable declarations
  • zero or more statements
  • '}'

the parser is doing the right thing here; agreed, the error message could be better.

@bob-carpenter
Copy link
Member

What we want to do is replace this issue with a feature request to turn variable declarations so that local variable declarations may be interleaved. It's awkward with the implicit block-variable/statement split in the blocks with statements---the statements start after the last declaration.

@VMatthijs
Copy link
Member

Why would we want this to parse? Is there any reason to declare a local variable and never use it?

I agree that mixing variable declarations and statements is useful, but that seems like a different issue.

@bob-carpenter
Copy link
Member

We want it to parse because it allows the syntactic definitions to be simple rather than tied up with semantics.

For example, C++ allows you to write:

int main() {
  for (int i = 0; i < 10; ++i)
    double x = 10;
  return 1;
}

and the compiler does not even produce a warning. Even if it's just double x; in the loop body there's no warning. Which is maybe default behavior and we turn on more warnings, because I thought we got unused variable warnings.

@VMatthijs
Copy link
Member

VMatthijs commented Dec 13, 2018

Hmm. OK. So maybe the grammar (and stanc3) should be changed then? I can't say I'm fully convinced this is useful. It might simplify the grammar a bit, but it's also work to implement and it would only allow the users to do something useful. If people feel strongly about this though, it wouldn't be hard to do (just would take away some time from other things).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants