Skip to content

Instantly share code, notes, and snippets.

@AndreasHassing
Last active August 8, 2022 06:47
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AndreasHassing/16567f299b77b0090d94441115a5d031 to your computer and use it in GitHub Desktop.
Save AndreasHassing/16567f299b77b0090d94441115a5d031 to your computer and use it in GitHub Desktop.
How to get FsYacc and FsLex (FsLexYacc) on OS X

Installing FsYacc and FsLex (FsLexYacc) on OS X

Step by step guide to getting FsYacc and FsLex for development on OS X.

1. Install Mono

By doing one of these:

2. Get NuGet Package Manager (CLI)

Download nuget.exe to ~/fsharp.

3. Install FsLexYacc

Copy paste into a terminal:

cd ~/fsharp                      # move to the ~/fsharp directory
mono nuget.exe install FsLexYacc # install fslexyacc with nuget
ls                               # list which files are in the current directory

# Should output something like:
# FsLexYacc.6.1.0/  FsLexYacc.Runtime.6.1.0/  nuget.exe

4. Create links to FsLex and FsYacc binaries

Copy paste into a terminal:

cd /usr/local/bin
echo -e '#!/bin/bash\nmono /Users/'$USER'/fsharp/FsLexYacc.6.1.0/build/fslex.exe $*' > fslex
echo -e '#!/bin/bash\nmono /Users/'$USER'/fsharp/FsLexYacc.6.1.0/build/fsyacc.exe $*' > fsyacc
chmod a+x fslex fsyacc

5. Link the runtime DLL to your ~/fsharp folder

The FsLexYacc.Runtime.dll file needs to be referred to when lexing and compiling your language,

# link the Runtime to your ~/fsharp folder (once):
ln -s ~/fsharp/FsLexYacc.Runtime.6.1.0/lib/net40/FsLexYacc.Runtime.dll ~/fsharp

# then henceforth, you can use the following from your project directory
# (referring to the link you just made):
fsharpi -r ~/fsharp/FsLexYacc.Runtime.dll Absyn.fs FunPar.fs FunLex.fs Parse.fs

5. Profit!

You should now totally have FsLexYacc on your OS X computer, ready to create awesome languages and compilers!

Footnotes

  1. When building from Homebrew, the Mono runtime gets odd parameters which sets tail-call optimizations to disabled for some reason, avoid this by using the downloadable installer from mono-project.
@EuroEdit
Copy link

EuroEdit commented Mar 28, 2018

Is this guide still valid? When running fsharpi -r ~/fsharp/FsLexYacc.Runtime.dll Absyn.fs FunPar.fs FunLex.fs Parse.fs I get Could not find file "/Users/europa/fsharp/FsLexYacc.Runtime.dll", but checking the folder, the file is there.

@AndreasHassing
Copy link
Author

AndreasHassing commented Jun 27, 2018

@EuroEdit: The guide should still be valid 🙂.

Did you remember to link the runtime to ~/fsharp/FsLexYacc.Runtime.dll? Instructions for that are on the lines above the command you're having problems with.

@runeksvendsen
Copy link

runeksvendsen commented Sep 3, 2019

@EuroEdit please note that step 4 may require manual editing of the two commands that start with echo. This is because these commands refer to a specific version of FsLexYacc, whereas the nuget command in step 3 won't necessarily install that specific version.

Also, it seems that fslex.exe and fsyacc.exe are now inside subfolders in /Users/'$USER'/fsharp/FsLexYacc.x.x.x/build/.

In my case, I had to (for step 4) use the commands (changing both version number and subfolders):

echo -e '#!/bin/bash\nmono /Users/'$USER'/fsharp/FsLexYacc.9.0.2/build/fslex/net46/fslex.exe $*' > fslex
echo -e '#!/bin/bash\nmono /Users/'$USER'/fsharp/FsLexYacc.9.0.2/build/fsyacc/net46/fsyacc.exe $*' > fsyacc

and for step 5 change the ln-command to (changing both version number and the net40 subfolder):

ln -s ~/fsharp/FsLexYacc.Runtime.9.0.2/lib/net46/FsLexYacc.Runtime.dll ~/fsharp

@steinzi
Copy link

steinzi commented Mar 27, 2021

Ok so Hi from 2021, and anyone using this text book http://www.itu.dk/people/sestoft/plc/

So Updated info.
There are no .exe files, they are .dll now.

Your version of FsLex may differ so replace FsLexYacc.10.2.0 with your version FsLexYacc.X.X.X

echo -e '#!/bin/bash\nmono /Users/'$USER'/fsharp/FsLexYacc.10.2.0/build/fsyacc/netcoreapp3.1/fsyacc.dll $*' > fsyacc
echo -e '#!/bin/bash\nmono /Users/'$USER'/fsharp/FsLexYacc.10.2.0/build/fslex/netcoreapp3.1/fslex.dll $*' > fslex

Also you will need to replace the “Microsoft.FSharp.Text.Lexing” with “FSharp.Text.Lexing”

ack "Microsoft.FSharp.Text"

@liweijian
Copy link

@steinzi Awesome thanks for your reply. I just got another issue when running fsi -r FSharp.PowerPack Absyn.fs ExprPar.fs ExprLex.fs as:

dotnet fsi -r FSharp.PowerPack Absyn.fs ExprPar.fs ExprLex.fs

Microsoft (R) F# Interactive version 12.0.4.0 for F# 6.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;



error FS0078: Unable to find the file 'FSharp.PowerPack' in any of
 /usr/local/share/dotnet/shared/Microsoft.NETCore.App/6.0.7
 /usr/local/share/dotnet/packs/Microsoft.NETCore.App.Ref/6.0.7/ref/net6.0
 /Users/lwj/Downloads/todel/fs101
 /usr/local/share/dotnet/sdk/6.0.302/FSharp/

Exception by fsi.exe:
System.Exception: Error creating evaluation session: StopProcessingExn
   at Microsoft.FSharp.Core.PrintfModule.PrintFormatToStringThenFail@1439.Invoke(String message)
   at FSharp.Compiler.Interactive.Shell.FsiEvaluationSession..ctor(FsiEvaluationSessionHostConfig fsi, String[] argv, TextReader inReader, TextWriter outWriter, TextWriter errorWriter, Boolean fsiCollectible, FSharpOption`1 legacyReferenceResolver) in D:\a\_work\1\s\src\fsharp\fsi\fsi.fs:line 3265
   at FSharp.Compiler.Interactive.Shell.FsiEvaluationSession.Create(FsiEvaluationSessionHostConfig fsiConfig, String[] argv, TextReader inReader, TextWriter outWriter, TextWriter errorWriter, FSharpOption`1 collectible, FSharpOption`1 legacyReferenceResolver) in D:\a\_work\1\s\src\fsharp\fsi\fsi.fs:line 3594
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.get_Value()
   at Sample.FSharp.Compiler.Interactive.Main.evaluateSession(String[] argv) in D:\a\_work\1\s\src\fsharp\fsi\fsimain.fs:line 270

Just wondering if maybe you met this error before?

@AndreasHassing
Copy link
Author

AndreasHassing commented Aug 8, 2022

@steinzi Awesome thanks for your reply. I just got another issue when running fsi -r FSharp.PowerPack Absyn.fs ExprPar.fs ExprLex.fs as:

dotnet fsi -r FSharp.PowerPack Absyn.fs ExprPar.fs ExprLex.fs

Microsoft (R) F# Interactive version 12.0.4.0 for F# 6.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;



error FS0078: Unable to find the file 'FSharp.PowerPack' in any of
 /usr/local/share/dotnet/shared/Microsoft.NETCore.App/6.0.7
 /usr/local/share/dotnet/packs/Microsoft.NETCore.App.Ref/6.0.7/ref/net6.0
 /Users/lwj/Downloads/todel/fs101
 /usr/local/share/dotnet/sdk/6.0.302/FSharp/

Exception by fsi.exe:
System.Exception: Error creating evaluation session: StopProcessingExn
   at Microsoft.FSharp.Core.PrintfModule.PrintFormatToStringThenFail@1439.Invoke(String message)
   at FSharp.Compiler.Interactive.Shell.FsiEvaluationSession..ctor(FsiEvaluationSessionHostConfig fsi, String[] argv, TextReader inReader, TextWriter outWriter, TextWriter errorWriter, Boolean fsiCollectible, FSharpOption`1 legacyReferenceResolver) in D:\a\_work\1\s\src\fsharp\fsi\fsi.fs:line 3265
   at FSharp.Compiler.Interactive.Shell.FsiEvaluationSession.Create(FsiEvaluationSessionHostConfig fsiConfig, String[] argv, TextReader inReader, TextWriter outWriter, TextWriter errorWriter, FSharpOption`1 collectible, FSharpOption`1 legacyReferenceResolver) in D:\a\_work\1\s\src\fsharp\fsi\fsi.fs:line 3594
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.get_Value()
   at Sample.FSharp.Compiler.Interactive.Main.evaluateSession(String[] argv) in D:\a\_work\1\s\src\fsharp\fsi\fsimain.fs:line 270

Just wondering if maybe you met this error before?

Hey @liweijian, is it intentional that you added FSharp.PowerPack as-is to the fsi -r arguments?
Doesn't look right to me. I believe you should be referencing the FsLexYacc.Runtime, see 5. for an example invocation of fsi with reference to the runtime.

@liweijian
Copy link

@AndreasHassing Nice! You made my day, thank you.

$ ln -s ~/fsharp/FsLexYacc.Runtime.10.2.0/lib/netstandard2.0/FsLexYacc.Runtime.dll ~/fsharp

$ fsharpi -r ~/fsharp/FsLexYacc.Runtime.dll Absyn.fs ExprPar.fs ExprLex.fs

Microsoft (R) F# Interactive version 11.0.0.0 for F# 5.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;

[Loading /Users/lwj/Downloads/todel/fs101/Absyn.fs
 Loading /Users/lwj/Downloads/todel/fs101/ExprPar.fs
 Loading /Users/lwj/Downloads/todel/fs101/ExprLex.fs]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment