Search code examples
haskellalex

Alex lex.x compilation : Not in scope 'begin'


I have the below statements in Lex.x to parse block comments.

<0>  "//".*                 { tokWValue LTokComment   }            
<0>  "/*"                   { begin blockcomment      }            
<blockcomment> "*/"         { begin 0                 }            
<blockcomment> .            { tokWValue LTokComment   }  

But If I generate Lex.hs using Alex, it does not add the 'begin' function. This results in the below compilation error.

src/Lex.x:367:18: Not in scope: ‘begin’
src/Lex.x:368:18: Not in scope: ‘begin’

Any idea what might be wrong?

I am using wrapper 'posn'


Solution

  • Start codes are only available when using any of the monad-... wrappers.

    If you read the docs for the monad wrapper -- Section 5.3.3 - The "monad" wrapper -- you see that it is the first wrapper which keeps track of the start code.

    You can also verify this by finding the alex wrapper files -- look for the directory containing the files AlexWrapper-basic, AlexWrapper-posn, etc. On OS X when installing the Haskell Platform they are located in a directory like /Library/Haskell/ghc-7.10.2-x86_64/share/alex-3.1.4. The functions begin and andBegin only occur in the monad-related wrappers.