1

I am using Alex 3.0.5, Happy 1.18.10, Cabal 1.16.0.2

I have a small compiler project that is built using Cabal. I am exposing the compiler's internals as a library, so I have in the exposed modules section, MyLangLex and MyLangPar. If I delete the .hs files that are generated by Alex and Happy, then running cabal configure, and then cabal build will run Alex and Happy first, generate the files, and then proceed with the build, and everything works as expected. However, if I do not delete these files, Alex and Happy either do not build the files, or they don't put them in the right place. I think Happy runs, because I see a message from Happy; however, when I look at the .hs file that should be generated it is incorrect (doesn't have a change in it), and I can tell for sure that the version of the .hs file that Cabal uses in the build is the wrong one because the behaviour that should have changed does not. I.e. The change to the .y file does not get incorporated into the built program, so I suspect that while Happy is run, Cabal places this file in some temp directory, and then uses the old .hs file, which is still there for the build. But I am not sure about this.

Is the error on my part or is one of the tools misbehaving?

ErikR
  • 51,541
  • 9
  • 73
  • 124
Jonathan Gallagher
  • 2,115
  • 2
  • 17
  • 31
  • Perhaps this answer is relevant? [Using alex/happy with Cabal](http://stackoverflow.com/questions/4465666/using-alex-happy-with-cabal?rq=1) In any case it would be helpful to see your cabal file. – ErikR Jul 12 '14 at 15:21
  • Thanks for the comment. If I delete Lex.hs and Par.hs (so that I only have Lex.x and Par.y), and cabal clean -> cabal configure -> cabal build -- the build fails. If I run alex and happy on the corresponding files, the build succeeds. Also, the build used to work when I had a flat directory, but started failing when I moved to a hierarchical structure. The lexer is in src/Compiler/RSL/Syntax/Lex.x. The module header in the Lex.x and Par.y uses the directory structure (e.g. in the Lex.x module field it is "module Compiler.RSL.Syntax.Lex where"). My Cabal file is http://lpaste.net/107438 – Jonathan Gallagher Jul 13 '14 at 17:56
  • Did you find the answer to this? I too am really shocked that Cabal is dumping the generated file in my directory, rather than keeping it in some hidden folder with other generated files. – sid-kap Feb 06 '18 at 05:35

1 Answers1

1

It sounds like you need a "other-modules:" directive in your library section for Lex.x and Par.y:

library
  ...
  build-tools: alex, happy
  other-modules: Compiler.RSL.Syntax.Lex, Compiler.RSL.Syntax.Par

The other-modules directive together with build-tools will instruct cabal to use alex and to create Compiler/RSL/Syntax/Lex.hs from the .x file if it doesn't exist (and the same for Par.hs).

Alternatively, add Compiler.RSL.Syntax.Lex to your 'exposed-modules' list. This tells cabal that the Lex.hs file should exist, and so if it doesn't cabal will look for ways to build it using the tools in the build-tools line.

ErikR
  • 51,541
  • 9
  • 73
  • 124