Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the fsyacc equivalent for the following ocamlyacc code?

Tags:

f#

fsyacc

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#?

like image 230
Cygwin98 Avatar asked Jan 29 '26 05:01

Cygwin98


1 Answers

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>).

like image 173
Laurent Avatar answered Jan 31 '26 21:01

Laurent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!