Search code examples
f#fsyacc

What is the fsyacc equivalent for the following ocamlyacc code?


I'm working on a toy compiler using F#, i.e., the combo of FsLex and FsYacc. To get familiar with them, I've read the Lexer/Parser chapter of Expert F# (v2) book (a good book btw). Right now, I've half way through the well-recommended ocamlyacc tutorial, and stuck at the last example Multi-Function Calculator mfcalc. Particularly, the following statement

%token <float->float> FNCT

in the parser file keeps getting error "error: parse error" in my F# version. Am I missing anything here, or is this a feature currently not supported by F#?


Solution

  • This looks like a bug. Adding parens doesn't help. I have tried various workarounds, but I couldn't find a clean way. You should do a bug report.

    If you have only one function (like in the tutorial example), you should define a type in the prelude:

    type floatFunction = float -> float
    ...
    %token <floatFunction> FNCT
    

    If you have many functions, you could also define a generic type:

    type functionType<'a, 'b> = 'a -> 'b
    ...
    %token < ('a, 'b) functionType > FNCT
    

    Any angle bracket in the type leads to a parse error (even functionType<float,float>).