PLTools / Lama

Teaching language LaMa for a compiler course
GNU General Public License v3.0
67 stars 28 forks source link

Segfault in Ostap #7

Closed bash-spbu closed 4 years ago

bash-spbu commented 4 years ago

Problem:

I faced some strange behaviour when wanted to use Do-notation with Ostap's parsers.

Consider the following example:

import Ostap;

fun pure(v) {
  empty @ lift(v)
}

local parse = 
  token(".") |> fun(e1) { 
  token(".") |> fun(e2) {
  pure(Dot(e1, e2)) }};

case parseString(parse |> bypass(eof), "..") of
   Succ(_)       -> printf("Yes")
 | Fail(_, _, _) -> printf("No")
esac

When I compile & run this I get a segfault somewhere (unfortunatelly GDB cannot locate it precisely).

But if we inline pure definition (what should not change the semantics, should it?), everything will be OK:

import Ostap;

fun pure(v) {
  empty @ lift(v)
}

local parse = 
  token(".") |> fun(e1) { 
  token(".") |> fun(e2) {
  empty @ lift(Dot(e1, e2)) }};

case parseString(parse |> bypass(eof), "..") of
   Succ(_)       -> printf("Yes")
 | Fail(_, _, _) -> printf("No")
esac

Result: Yes

Expected behaviour:

Same semantics for both snippets.

Environment:

dboulytchev commented 4 years ago

Looking like some tail-call optimization issue.

bash-spbu commented 4 years ago

Also I probably should mention that this example is not an issue when there is only one token combinator:

import Ostap;

fun pure(v) {
  empty @ lift(v)
}

local parse = 
  token(".") |> fun(e1) { 
  pure(Dot(e1)) };

case parseString(parse |> bypass(eof), ".") of
   Succ(_)       -> printf("Yes")
 | Fail(_, _, _) -> printf("No")
esac

Works as expected.

Maybe it will help to localize the problem.

dboulytchev commented 4 years ago

Fixed in 7748144.