diff --git a/alex.cabal b/alex.cabal
--- a/alex.cabal
+++ b/alex.cabal
@@ -1,5 +1,5 @@
 name: alex
-version: 3.0.5
+version: 3.1.0
 license: BSD3
 license-file: LICENSE
 copyright: (c) Chis Dornan, Simon Marlow
@@ -9,11 +9,17 @@
 stability: stable
 homepage: http://www.haskell.org/alex/
 synopsis: Alex is a tool for generating lexical analysers in Haskell
-description: Alex is a tool for generating lexical analysers in Haskell.
-             It takes a description of tokens based on regular
-             expressions and generates a Haskell module containing code
-             for scanning text efficiently. It is similar to the tool
-             lex or flex for C/C++.
+description:
+  Alex is a tool for generating lexical analysers in Haskell.
+  It takes a description of tokens based on regular
+  expressions and generates a Haskell module containing code
+  for scanning text efficiently. It is similar to the tool
+  lex or flex for C/C++.
+  .
+  Changes in 3.1.0:
+  .
+  * necessary changes to work with GHC 7.8.1
+
 category: Development
 cabal-version: >= 1.8
 build-type: Custom
@@ -107,4 +113,4 @@
 test-suite tests
   type: exitcode-stdio-1.0
   main-is: test.hs
-  build-depends: process
+  build-depends: base, process
diff --git a/dist/build/alex/alex-tmp/Scan.hs b/dist/build/alex/alex-tmp/Scan.hs
--- a/dist/build/alex/alex-tmp/Scan.hs
+++ b/dist/build/alex/alex-tmp/Scan.hs
@@ -1,4 +1,4 @@
-{-# LANGUAGE CPP,MagicHash,BangPatterns #-}
+{-# LANGUAGE CPP,MagicHash #-}
 {-# LINE 13 "src/Scan.x" #-}
 
 {-# OPTIONS_GHC -w #-}
@@ -39,7 +39,7 @@
 alex_deflt :: AlexAddr
 alex_deflt = AlexA# "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x52\x00\xff\xff\xff\xff\xff\xff\x09\x00\xff\xff\x0b\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x2e\x00\x2e\x00\x34\x00\x34\x00\x36\x00\x36\x00\xff\xff\x3f\x00\x3f\x00\x40\x00\x40\x00\x46\x00\x46\x00\x4a\x00\x4a\x00\x4d\x00\x4d\x00\x4e\x00\x4e\x00\x50\x00\x50\x00\xff\xff\x52\x00\x52\x00\x52\x00\x57\x00\x57\x00\x58\x00\x58\x00\x5a\x00\x5a\x00\x5f\x00\x5f\x00\x0b\x00\x0b\x00\x0b\x00\x09\x00\x09\x00\x09\x00\x62\x00\x62\x00\x65\x00\x65\x00\x52\x00\xff\xff\xff\xff\x67\x00\x67\x00\x67\x00\x6a\x00\x6a\x00\x6e\x00\x6e\x00\x93\x00\x93\x00\x93\x00\x93\x00\x82\x00\x82\x00\x82\x00\x7d\x00\x7d\x00\x7d\x00\xff\xff\x67\x00\x71\x00\x71\x00\x71\x00\x6f\x00\x6f\x00\x6f\x00\x6f\x00\xff\xff\xff\xff\x71\x00\xff\xff\xff\xff\x52\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x67\x00\xff\xff\xff\xff\xff\xff\xff\xff\x09\x00\xff\xff\x0b\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"#
 
-alex_accept = listArray (0::Int,147) [[],[(AlexAcc (alex_action_21))],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[(AlexAccSkip)],[(AlexAcc (alex_action_0))],[(AlexAcc (alex_action_0))],[(AlexAcc (alex_action_1))],[(AlexAcc (alex_action_2))],[(AlexAcc (alex_action_2))],[(AlexAccPred  (alex_action_3) (alexRightContext 108)),(AlexAcc (alex_action_4))],[(AlexAcc (alex_action_4))],[(AlexAcc (alex_action_4))],[(AlexAcc (alex_action_4))],[(AlexAcc (alex_action_5))],[(AlexAcc (alex_action_6))],[(AlexAcc (alex_action_7))],[(AlexAcc (alex_action_8))],[(AlexAcc (alex_action_9))],[(AlexAcc (alex_action_9))],[(AlexAcc (alex_action_9))],[(AlexAcc (alex_action_10))],[(AlexAcc (alex_action_10))],[(AlexAcc (alex_action_10))],[(AlexAcc (alex_action_10))],[(AlexAcc (alex_action_10))],[(AlexAcc (alex_action_10))],[(AlexAcc (alex_action_11))],[(AlexAcc (alex_action_11))],[(AlexAcc (alex_action_12))],[(AlexAcc (alex_action_12))],[(AlexAcc (alex_action_13))],[(AlexAcc (alex_action_13))],[(AlexAcc (alex_action_14))],[(AlexAcc (alex_action_14))],[(AlexAcc (alex_action_15))],[(AlexAcc (alex_action_16))],[(AlexAcc (alex_action_17))],[(AlexAcc (alex_action_18))],[(AlexAcc (alex_action_19))],[(AlexAcc (alex_action_20))]]
+alex_accept = listArray (0::Int,147) [AlexAccNone,AlexAcc (alex_action_21),AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccNone,AlexAccSkip,AlexAcc (alex_action_0),AlexAcc (alex_action_0),AlexAcc (alex_action_1),AlexAcc (alex_action_2),AlexAcc (alex_action_2),AlexAccPred  (alex_action_3) (alexRightContext 108)(AlexAcc (alex_action_4)),AlexAcc (alex_action_4),AlexAcc (alex_action_4),AlexAcc (alex_action_4),AlexAcc (alex_action_5),AlexAcc (alex_action_6),AlexAcc (alex_action_7),AlexAcc (alex_action_8),AlexAcc (alex_action_9),AlexAcc (alex_action_9),AlexAcc (alex_action_9),AlexAcc (alex_action_10),AlexAcc (alex_action_10),AlexAcc (alex_action_10),AlexAcc (alex_action_10),AlexAcc (alex_action_10),AlexAcc (alex_action_10),AlexAcc (alex_action_11),AlexAcc (alex_action_11),AlexAcc (alex_action_12),AlexAcc (alex_action_12),AlexAcc (alex_action_13),AlexAcc (alex_action_13),AlexAcc (alex_action_14),AlexAcc (alex_action_14),AlexAcc (alex_action_15),AlexAcc (alex_action_16),AlexAcc (alex_action_17),AlexAcc (alex_action_18),AlexAcc (alex_action_19),AlexAcc (alex_action_20)]
 {-# LINE 75 "src/Scan.x" #-}
 
 
@@ -225,9 +225,9 @@
 -- -----------------------------------------------------------------------------
 -- INTERNALS and main scanner engine
 
-{-# LINE 37 "templates/GenericTemplate.hs" #-}
+{-# LINE 35 "templates/GenericTemplate.hs" #-}
 
-{-# LINE 47 "templates/GenericTemplate.hs" #-}
+{-# LINE 45 "templates/GenericTemplate.hs" #-}
 
 
 data AlexAddr = AlexA# Addr#
@@ -241,10 +241,10 @@
 #ifdef WORDS_BIGENDIAN
   narrow16Int# i
   where
-        !i    = word2Int# ((high `uncheckedShiftL#` 8#) `or#` low)
-        !high = int2Word# (ord# (indexCharOffAddr# arr (off' +# 1#)))
-        !low  = int2Word# (ord# (indexCharOffAddr# arr off'))
-        !off' = off *# 2#
+        i    = word2Int# ((high `uncheckedShiftL#` 8#) `or#` low)
+        high = int2Word# (ord# (indexCharOffAddr# arr (off' +# 1#)))
+        low  = int2Word# (ord# (indexCharOffAddr# arr off'))
+        off' = off *# 2#
 #else
   indexInt16OffAddr# arr off
 #endif
@@ -258,14 +258,14 @@
 #ifdef WORDS_BIGENDIAN
   narrow32Int# i
   where
-   !i    = word2Int# ((b3 `uncheckedShiftL#` 24#) `or#`
+   i    = word2Int# ((b3 `uncheckedShiftL#` 24#) `or#`
 		     (b2 `uncheckedShiftL#` 16#) `or#`
 		     (b1 `uncheckedShiftL#` 8#) `or#` b0)
-   !b3   = int2Word# (ord# (indexCharOffAddr# arr (off' +# 3#)))
-   !b2   = int2Word# (ord# (indexCharOffAddr# arr (off' +# 2#)))
-   !b1   = int2Word# (ord# (indexCharOffAddr# arr (off' +# 1#)))
-   !b0   = int2Word# (ord# (indexCharOffAddr# arr off'))
-   !off' = off *# 4#
+   b3   = int2Word# (ord# (indexCharOffAddr# arr (off' +# 3#)))
+   b2   = int2Word# (ord# (indexCharOffAddr# arr (off' +# 2#)))
+   b1   = int2Word# (ord# (indexCharOffAddr# arr (off' +# 1#)))
+   b0   = int2Word# (ord# (indexCharOffAddr# arr off'))
+   off' = off *# 4#
 #else
   indexInt32OffAddr# arr off
 #endif
@@ -340,36 +340,41 @@
 
 
 
-	let
-		(!(base)) = alexIndexInt32OffAddr alex_base s
-		(!((I# (ord_c)))) = fromIntegral c
-		(!(offset)) = (base +# ord_c)
-		(!(check))  = alexIndexInt16OffAddr alex_check offset
+      case fromIntegral c of { (I# (ord_c)) ->
+        let
+                base   = alexIndexInt32OffAddr alex_base s
+                offset = (base +# ord_c)
+                check  = alexIndexInt16OffAddr alex_check offset
 		
-		(!(new_s)) = if (offset >=# 0#) && (check ==# ord_c)
+                new_s = if (offset >=# 0#) && (check ==# ord_c)
 			  then alexIndexInt16OffAddr alex_table offset
 			  else alexIndexInt16OffAddr alex_deflt s
 	in
-	case new_s of 
+        case new_s of
 	    -1# -> (new_acc, input)
 		-- on an error, we want to keep the input *before* the
 		-- character that failed, not after.
     	    _ -> alex_scan_tkn user orig_input (if c < 0x80 || c >= 0xC0 then (len +# 1#) else len)
                                                 -- note that the length is increased ONLY if this is the 1st byte in a char encoding)
 			new_input new_s new_acc
-
+      }
   where
-	check_accs [] = last_acc
-	check_accs (AlexAcc a : _) = AlexLastAcc a input (I# (len))
-	check_accs (AlexAccSkip : _)  = AlexLastSkip  input (I# (len))
-	check_accs (AlexAccPred a predx : rest)
+	check_accs (AlexAccNone) = last_acc
+	check_accs (AlexAcc a  ) = AlexLastAcc a input (I# (len))
+	check_accs (AlexAccSkip) = AlexLastSkip  input (I# (len))
+
+	check_accs (AlexAccPred a predx rest)
 	   | predx user orig_input (I# (len)) input
 	   = AlexLastAcc a input (I# (len))
-	check_accs (AlexAccSkipPred predx : rest)
+	   | otherwise
+	   = check_accs rest
+	check_accs (AlexAccSkipPred predx rest)
 	   | predx user orig_input (I# (len)) input
 	   = AlexLastSkip input (I# (len))
-	check_accs (_ : rest) = check_accs rest
+	   | otherwise
+	   = check_accs rest
 
+
 data AlexLastAcc a
   = AlexNone
   | AlexLastAcc a !AlexInput !Int
@@ -381,11 +386,13 @@
     fmap f (AlexLastSkip x y) = AlexLastSkip x y
 
 data AlexAcc a user
-  = AlexAcc a
+  = AlexAccNone
+  | AlexAcc a
   | AlexAccSkip
-  | AlexAccPred a (AlexAccPred user)
-  | AlexAccSkipPred (AlexAccPred user)
 
+  | AlexAccPred a   (AlexAccPred user) (AlexAcc a user)
+  | AlexAccSkipPred (AlexAccPred user) (AlexAcc a user)
+
 type AlexAccPred user = user -> AlexInput -> Int -> AlexInput -> Bool
 
 -- -----------------------------------------------------------------------------
@@ -410,6 +417,7 @@
 	-- TODO: there's no need to find the longest
 	-- match when checking the right context, just
 	-- the first match will do.
+
 
 -- used by wrappers
 iUnbox (I# (i)) = i
diff --git a/doc/alex.xml b/doc/alex.xml
--- a/doc/alex.xml
+++ b/doc/alex.xml
@@ -362,7 +362,7 @@
     <para>Alex takes a description of tokens based on regular
     expressions and generates a Haskell module containing code for
     scanning text efficiently.  Alex is designed to be familiar to
-    exisiting lex users, although it does depart from lex in a number
+    existing lex users, although it does depart from lex in a number
     of ways.</para>
 
     <figure id="fig-tokens" float="1"><title>A simple Alex specification.</title>
@@ -429,7 +429,7 @@
 
 <programlisting><replaceable>regexp</replaceable>   { <replaceable>code</replaceable> }</programlisting>
 
-    <para>The meaming of a this rule is "if the input matches
+    <para>The meaning of a this rule is "if the input matches
     <replaceable>regexp</replaceable>, then return
     <replaceable>code</replaceable>".  The code part along with the
     braces can be replaced by simply
@@ -588,7 +588,7 @@
 	<para>The rules are heralded by the sequence
 	&lsquo;<literal><replaceable>id</replaceable> :-</literal>&rsquo;
         in the file.  It doesn't matter what you use for the
-        identifer, it is just there for documentation purposes.  In
+        identifier, it is just there for documentation purposes.  In
 	fact, it can be omitted, but the <literal>:-</literal> must be
 	left in.</para>
 
@@ -723,7 +723,7 @@
 	  specification, such that only certain rules will match for a
 	  given state.</para>
 
-	  <para>A startcode is simply an identifer, or the special
+	  <para>A startcode is simply an identifier, or the special
 	  start code &lsquo;<literal>0</literal>&rsquo;.  Each rule
 	  may be given a list of startcodes under which it
 	  applies:</para>
@@ -851,7 +851,7 @@
 	<varlistentry>
 	  <term><literal><replaceable>r</replaceable>*</literal></term>
 	  <listitem>
-	    <para>Matches zero or more occurences of
+	    <para>Matches zero or more occurrences of
 	    <replaceable>r</replaceable>.</para>
 	  </listitem>
 	</varlistentry>
@@ -859,7 +859,7 @@
 	<varlistentry>
 	  <term><literal><replaceable>r</replaceable>+</literal></term>
 	  <listitem>
-	    <para>Matches one or more occurences of
+	    <para>Matches one or more occurrences of
 	    <replaceable>r</replaceable>.</para>
 	  </listitem>
 	</varlistentry>
@@ -867,7 +867,7 @@
 	<varlistentry>
 	  <term><literal><replaceable>r</replaceable>?</literal></term>
 	  <listitem>
-	    <para>Matches zero or one occurences of
+	    <para>Matches zero or one occurrences of
 	    <replaceable>r</replaceable>.</para>
 	  </listitem>
 	</varlistentry>
@@ -1278,7 +1278,7 @@
 
 	<para>The type signature for <literal>alexScanTokens</literal>
         is commented out, because the <literal>token</literal> type is
-        unkonwn.  All of the actions in your lexical specification
+        unknown.  All of the actions in your lexical specification
         should have type:</para>
 
 <programlisting>{ ... } :: String -> token</programlisting>
@@ -1374,6 +1374,12 @@
 <programlisting>type AlexAction result = AlexInput -> Int -> Alex result
 { ... }  :: AlexAction result</programlisting>
 
+        <para>The Alex file must also define a function
+        <literal>alexEOF</literal>, which will be executed on when the
+        end-of-file is scanned:</para>
+
+<programlisting>alexEOF :: Alex result</programlisting>
+
 	<para>The <literal>monad</literal> wrapper also provides some
 	useful combinators for constructing token actions:</para>
 
@@ -1403,7 +1409,7 @@
     it during the whole lexing phase.</para>
 
     <para>The generated code is the same as in the <literal>monad</literal> 
-    wrapper, except in 2 places:</para>
+    wrapper, except in 3 places:</para>
     <para>1) The definition of the general state, which now refers to a
     type (<literal>AlexUserState</literal>) that must be defined in the Alex file.</para>
 
@@ -1433,6 +1439,13 @@
                                           Right ( _, a ) -> Right a
 </programlisting>
 
+    <para>3) Two helper functions (<literal>alexGetUserState</literal>
+    and <literal>alexSetUserState</literal>) are defined.</para>
+
+<programlisting>
+alexGetUserState :: Alex AlexUserState
+alexSetUserState :: AlexUserState -> Alex ()
+</programlisting>
     <para>Here is an example of code in the user's Alex file defining
     the type and function:</para>
 
@@ -1450,19 +1463,19 @@
                    }
 
 getLexerCommentDepth :: Alex Int
-getLexerCommentDepth = Alex $ \s@AlexState{alex_ust=ust} -> Right (s, lexerCommentDepth ust)
+getLexerCommentDepth = do ust &lt;- alexGetUserState; return (lexerCommentDepth ust)
 
 setLexerCommentDepth :: Int -> Alex ()
-setLexerCommentDepth ss = Alex $ \s -> Right (s{alex_ust=(alex_ust s){lexerCommentDepth=ss}}, ())
+setLexerCommentDepth ss = do ust &lt;- alexGetUserState; alexSetUserState ust{lexerCommentDepth=ss}
 
 getLexerStringValue :: Alex String
-getLexerStringValue = Alex $ \s@AlexState{alex_ust=ust} -> Right (s, lexerStringValue ust)
+getLexerStringValue = do ust &lt;- alexGetUserState; return (lexerStringValue ust)
 
 setLexerStringValue :: String -> Alex ()
-setLexerStringValue ss = Alex $ \s -> Right (s{alex_ust=(alex_ust s){lexerStringValue=ss}}, ())
+setLexerStringValue ss = do ust &lt;- alexGetUserState; alexSetUserState ust{lexerStringValue=ss}
 
 addCharToLexerStringValue :: Char -> Alex ()
-addCharToLexerStringValue c = Alex $ \s -> Right (s{alex_ust=(alex_ust s){lexerStringValue=c:lexerStringValue (alex_ust s)}}, ())
+addCharToLexerStringValue c = do ust &lt;- alexGetUserState; alexSetUserState ust{lexerStringValue=c:(lexerStringValue ust)}
 </programlisting>
       </section>
 
@@ -1503,7 +1516,7 @@
 	
 	<para>The point of using these wrappers is that
 	<literal>ByteString</literal>s provide a more memory efficient
-	representaion of an input stream. They can also be somewhat faster to
+	representation of an input stream. They can also be somewhat faster to
         process. Note that using these wrappers adds a dependency
 	on the <literal>ByteString</literal> modules, which live in the
 	<literal>bytestring</literal> package (or in the
@@ -1712,7 +1725,7 @@
 	<term><option>--debug</option></term>
 	<listitem>
 	  <para>Causes Alex to produce a lexer which will output
-	  debugging messsages as it runs.</para>
+	  debugging messages as it runs.</para>
 	</listitem>
       </varlistentry>
 
diff --git a/templates/GenericTemplate.hs b/templates/GenericTemplate.hs
--- a/templates/GenericTemplate.hs
+++ b/templates/GenericTemplate.hs
@@ -8,12 +8,28 @@
 -- INTERNALS and main scanner engine
 
 #ifdef ALEX_GHC
+#undef __GLASGOW_HASKELL__
+#define ALEX_IF_GHC_GT_500 #if __GLASGOW_HASKELL__ > 500
+#define ALEX_IF_GHC_LT_503 #if __GLASGOW_HASKELL__ < 503
+#define ALEX_IF_GHC_GT_706 #if __GLASGOW_HASKELL__ > 706
+#define ALEX_ELIF_GHC_500 #elif __GLASGOW_HASKELL__ == 500
+#define ALEX_IF_BIGENDIAN #ifdef WORDS_BIGENDIAN
+#define ALEX_ELSE #else
+#define ALEX_ENDIF #endif
+#define ALEX_DEFINE #define
+#endif
+
+#ifdef ALEX_GHC
 #define ILIT(n) n#
 #define IBOX(n) (I# (n))
 #define FAST_INT Int#
-#define LT(n,m) (n <# m)
-#define GTE(n,m) (n >=# m)
-#define EQ(n,m) (n ==# m)
+ALEX_IF_GHC_GT_706
+ALEX_DEFINE GTE(n,m) (tagToEnum# (n >=# m))
+ALEX_DEFINE EQ(n,m) (tagToEnum# (n ==# m))
+ALEX_ELSE
+ALEX_DEFINE GTE(n,m) (n >=# m)
+ALEX_DEFINE EQ(n,m) (n ==# m)
+ALEX_ENDIF
 #define PLUS(n,m) (n +# m)
 #define MINUS(n,m) (n -# m)
 #define TIMES(n,m) (n *# m)
@@ -23,7 +39,6 @@
 #define ILIT(n) (n)
 #define IBOX(n) (n)
 #define FAST_INT Int
-#define LT(n,m) (n < m)
 #define GTE(n,m) (n >= m)
 #define EQ(n,m) (n == m)
 #define PLUS(n,m) (n + m)
@@ -31,16 +46,6 @@
 #define TIMES(n,m) (n * m)
 #define NEGATE(n) (negate (n))
 #define IF_GHC(x)
-#endif
-
-#ifdef ALEX_GHC
-#undef __GLASGOW_HASKELL__
-#define ALEX_IF_GHC_GT_500 #if __GLASGOW_HASKELL__ > 500
-#define ALEX_IF_GHC_LT_503 #if __GLASGOW_HASKELL__ < 503
-#define ALEX_ELIF_GHC_500 #elif __GLASGOW_HASKELL__ == 500
-#define ALEX_IF_BIGENDIAN #ifdef WORDS_BIGENDIAN
-#define ALEX_ELSE #else
-#define ALEX_ENDIF #endif
 #endif
 
 #ifdef ALEX_GHC
diff --git a/templates/wrappers.hs b/templates/wrappers.hs
--- a/templates/wrappers.hs
+++ b/templates/wrappers.hs
@@ -195,12 +195,20 @@
 alexSetStartCode :: Int -> Alex ()
 alexSetStartCode sc = Alex $ \s -> Right (s{alex_scd=sc}, ())
 
+#ifdef ALEX_MONAD_USER_STATE
+alexGetUserState :: Alex AlexUserState
+alexGetUserState = Alex $ \s@AlexState{alex_ust=ust} -> Right (s,ust)
+
+alexSetUserState :: AlexUserState -> Alex ()
+alexSetUserState ss = Alex $ \s -> Right (s{alex_ust=ss}, ())
+#endif
+
 alexMonadScan = do
   inp <- alexGetInput
   sc <- alexGetStartCode
   case alexScan inp sc of
     AlexEOF -> alexEOF
-    AlexError inp' -> alexError "lexical error"
+    AlexError ((AlexPn _ line column),_,_,_) -> alexError $ "lexical error at line " ++ (show line) ++ ", column " ++ (show column)
     AlexSkip  inp' len -> do
         alexSetInput inp'
         alexMonadScan
@@ -289,7 +297,7 @@
   sc <- alexGetStartCode
   case alexScan inp sc of
     AlexEOF -> alexEOF
-    AlexError inp' -> alexError "lexical error"
+    AlexError ((AlexPn _ line column),_,_) -> alexError $ "lexical error at line " ++ (show line) ++ ", column " ++ (show column)
     AlexSkip  inp' len -> do
         alexSetInput inp'
         alexMonadScan
@@ -388,7 +396,7 @@
   where go inp@(pos,_,_,str) =
           case alexScan inp 0 of
                 AlexEOF -> []
-                AlexError ((AlexPn _ line column),_,_,_) -> error $ "lexical error at " ++ (show line) ++ " line, " ++ (show column) ++ " column"
+                AlexError ((AlexPn _ line column),_,_,_) -> error $ "lexical error at line " ++ (show line) ++ ", column " ++ (show column)
                 AlexSkip  inp' len     -> go inp'
                 AlexToken inp' len act -> act pos (take len str) : go inp'
 #endif
@@ -403,7 +411,7 @@
   where go inp@(pos,_,str) =
           case alexScan inp 0 of
                 AlexEOF -> []
-                AlexError ((AlexPn _ line column),_,_) -> error $ "lexical error at " ++ (show line) ++ " line, " ++ (show column) ++ " column"
+                AlexError ((AlexPn _ line column),_,_) -> error $ "lexical error at line " ++ (show line) ++ ", column " ++ (show column)
                 AlexSkip  inp' len     -> go inp'
                 AlexToken inp' len act -> act pos (ByteString.take (fromIntegral len) str) : go inp'
 #endif
diff --git a/tests/simple.x b/tests/simple.x
--- a/tests/simple.x
+++ b/tests/simple.x
@@ -69,5 +69,5 @@
 out1 = Right ["BOL:a","b","c","D","E","F","PING!","MAGIC","EOL:eol", "BOL:bol", "BOTH:both", "stopped."]
 
 str2 = "."
-out2 = Left "lexical error"
+out2 = Left "lexical error at line 1, column 1"
 }
diff --git a/tests/unicode.x b/tests/unicode.x
--- a/tests/unicode.x
+++ b/tests/unicode.x
@@ -75,7 +75,7 @@
 out1 = Right ["PING!",".","stopped."]
 
 str2 = "\n"
-out2 = Left "lexical error"
+out2 = Left "lexical error at line 1, column 1"
 
 
 str3 = "αω --"
