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

Parse error in if-else #2697

Closed
VMatthijs opened this issue Nov 18, 2018 · 13 comments
Closed

Parse error in if-else #2697

VMatthijs opened this issue Nov 18, 2018 · 13 comments
Assignees
Milestone

Comments

@VMatthijs
Copy link
Member

Summary:

Conditional control flow constructs have strange parsing behaviour with block structure.

Description:

For an if-then-else statement, it seems that we are required to specify a block statement for the then-clause. Arbitrary statements seem disallowed.

Reproducible Steps:

Try to compile

model {
    if (1)
      print(5);
    else
      print(4);
}

Current Output:

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Variable "else" does not exist.
error in 'stan/src/test/test-models/good/if-else-blockless.stan' at line 4, column 9

 2:     if (1)
 3:       print(5);
 4:     else
            ^
 5:       print(4);

Expected Output:

I would expect this to parse just fine.

Current Version:

v2.18.0

@bob-carpenter
Copy link
Member

Yes, this should parse. Would you mind labeling these things with a deliverable (bugs should be next release) and an assignee (@mitzimorris is handling all the current bugs for the parser while @VMatthijs and @seantalts work on the Stan 3 parser).

@VMatthijs
Copy link
Member Author

VMatthijs commented Nov 18, 2018

Great! I already have all of these bugs I'm submitting fixed in the Stan 3 compiler. I am just submitting them to make sure that people agree they are bugs before I change the existing behaviour. I don't think it's worth anyone (@mitzimorris ) spending time on fixing them in the existing compiler. The new compiler should be finished in not too long. My sense is that for the moment, there are better things to spend time on than the old compiler.

@bob-carpenter
Copy link
Member

bob-carpenter commented Nov 18, 2018 via email

@VMatthijs
Copy link
Member Author

VMatthijs commented Nov 19, 2018

If you think it's worth writing out a spec, then I'm happy to write a document outlining what's implemented, though I'm hopeful that the implementation itself is rather readable as well, as almost all of it is written in a declarative style.

My expected timeline is as follows:

  • this week: get the error messages to the point that they are at least as good as those in the existing compiler
  • two weeks following that: get the code generation done
  • add to that another week to be conservative?

That would get us to the point of a complete beta version of the compiler around Christmas.
Then, we'd need time to integrate it with CmdStan, RStan and PyStan. I have never worked on that code so don't know how much work it would be. Perhaps @seantalts could give an estimate?

Are you still not convinced the new compiler will be done soon enough that we can pause bug fixes on the language for now and concentrate on the Math library and interfaces until then?

@bob-carpenter
Copy link
Member

bob-carpenter commented Nov 20, 2018 via email

@mitzimorris mitzimorris modified the milestones: 2.18.1, 2.18.1++ Dec 23, 2018
@msadeghpour
Copy link

Hi,
I have a similar error (variable "else" does not exist) with my if-else statement and was wondering if this issue is resolved?

@bob-carpenter
Copy link
Member

bob-carpenter commented Mar 19, 2019 via email

@seantalts
Copy link
Member

The problem is that it's taking way longer than anticipated to get the OCaml going and there's no end in sight yet.

I think we have another month and a half left until we're over the original 6 month budget no? Something like that.

@msadeghpour
Copy link

Thanks for your response.
In my model block, inside the function block, I have this:
real r1;
real r2;
if (x_i==1){
r1 = 0.2;
r2 = 0.2;
}
else if (x_i==2){
r1 = 0.2;
r2 = 0.5;
}

However, I receive the error "variable real does not exist." If I don't use the brackets above, I receive the error "variable else does not exist."
I was wondering if currently there is a fix for this? In general, can I use if-else statements in the function block that is inside the model block?

Thanks

@seantalts
Copy link
Member

seantalts commented Mar 20, 2019 via email

@seantalts seantalts modified the milestones: 2.18.1++, 2.19.0++ Mar 20, 2019
@bob-carpenter
Copy link
Member

The failing example is in the original issue:

model {
    if (1)
      print(5);
    else
      print(4);
}

It works if you put braces around the bodies, but it should work this way, too.

@seantalts
Copy link
Member

seantalts commented Mar 20, 2019 via email

@mcol
Copy link
Contributor

mcol commented Feb 21, 2020

Fixed in stanc3.

@mcol mcol closed this as completed Feb 21, 2020
@mcol mcol modified the milestones: 2.22.0++, 2.22.0 Feb 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants