title: Using Parsec to parse regular expressions
body: <p>I'm trying to learn Parsec by implementing a small regular expression parser. In BNF, my grammar looks something like:</p>
  
  <pre><code>EXP  : EXP *
       | LIT EXP
       | LIT
  </code></pre>
  
  <p>I've tried to implement this in Haskell as:</p>
  
  <pre><code>expr = try star
         &lt;|&gt; try litE
         &lt;|&gt; lit
  
  litE  = do c &lt;- noneOf "*"
             rest &lt;- expr
             return (c : rest)
  
  lit   = do c &lt;- noneOf "*"
             return [c]
  
  star = do content &lt;- expr
            char '*'
            return (content ++ "*")
  </code></pre>
  
  <p>There are some infinite loops here though (e.g. expr -> star -> expr without consuming any tokens) which makes the parser loop forever. I'm not really sure how to fix it though, because the very nature of <code>star</code> is that it consumes its mandatory token at the end.</p>
  
  <p>Any thoughts?</p>