Using alex/happy with Cabal
Asked Answered
G

2

16

I'm writing a compiler for a class I'm taking. The class isn't specifically Haskell but I'm using Haskell to write my compiler and interpreter. I have a cabal package setup to hopefully make it easy for my prof to run/compile.

I have happy and alex in the build-tools field for both executables but Cabal ignores that and then complains that it cannot find the modules that Happy and Alex should be generating.

If I manually run:

alex LimpScanner.x
happy LimpParser.y

then cabal runs perfectly.

I thought I had cabal automatically running them earlier but perhaps I remember imperfectly.

Contents of limp.cabal:

Name:                limp
Version:             0.1
Synopsis:            LIMP Compiler (Compiler Construction course project)
Homepage:            http://www.cs.rit.edu/~eca7215/limp/
License:             AllRightsReserved
License-file:        LICENSE
Author:              Edward Amsden
Maintainer:          [email protected]
Category:            Language
Build-type:          Simple
Cabal-version:       >=1.2

Executable limp
  -- .hs or .lhs file containing the Main module.
  Main-is: Limp.hs

  hs-source-dirs: src     
  
  -- Packages needed in order to build this package.
  Build-depends: base, array, haskell98     
  
  -- Modules not exported by this package.
  -- Other-modules:       
  
  -- Extra tools (e.g. alex, hsc2hs, ...) needed to build the source.
  Build-tools:         alex, happy
Executable limpi
  Main-is: LimpInterpreter.hs
  hs-source-dirs: src
  Build-depends: base, array, haskell98
  Build-tools: alex, happy

Directory layout:

limp/
├── Setup.hs
├── limp.cabal
└── src/
    ├── Limp.hs
    ├── LimpInterpreter.hs
    ├── LimpParser.ly
    ├── LimpScanner.x
    └── LimpToken.hs
Gravure answered 16/12, 2010 at 21:45 Comment(4)
As long as you state that you use modules LimpParser and LimpScanner, have files named LimpParser.x and LimpScanner.ly, and have Alex and Happy installed, then any Cabal version from the last year or so should have automatically run alex and happy. Hmm.Colpin
We need more details. What is your directory layout? What is your cabal file?Festival
Isn't it supposed to be the other way around? LimpParser.ly and LimpScanner.x?Gravure
Edited to add .cabal file and directory layoutGravure
G
12

Apparently what I was missing was actually the Other-modules: field. Once this was added, cabal happily (pardon the pun) built my interpreter.

Gravure answered 17/12, 2010 at 1:31 Comment(2)
Good on you for answering this yourself.Nellynelms
Could you complete the example and show what Other-modules should be set to? Thanks,Coats
S
19

For Warren Harris and others like him (and myself) that may come along later, other-modules needs to be set to a list of module names that (I guess?) are expected to be built by the tools listed in build-tools.

So, in my case, the relevant sections of my .cabal file ended up looking like this:

build-tools:         alex, happy
other-modules:       Language.Heidi.Parser,
                     Language.Heidi.Lexer
Solenoid answered 4/4, 2013 at 8:44 Comment(1)
Note: use build-tool-depends: alex:alex (build-tools is deprecated/removed since cabal 2/3).Hirohito
G
12

Apparently what I was missing was actually the Other-modules: field. Once this was added, cabal happily (pardon the pun) built my interpreter.

Gravure answered 17/12, 2010 at 1:31 Comment(2)
Good on you for answering this yourself.Nellynelms
Could you complete the example and show what Other-modules should be set to? Thanks,Coats

© 2022 - 2024 — McMap. All rights reserved.