ollef / Earley

Parsing all context-free grammars using Earley's algorithm in Haskell.
BSD 3-Clause "New" or "Revised" License
363 stars 24 forks source link

Control.Applicative.optional not working #7

Closed kindl closed 9 years ago

kindl commented 9 years ago

Hei Olle!

It drove me crazy to remove left recursion in Parsec. Therefore a big thank you for writing Earley. Maybe it will become a thing in the future. ;)

I am trying to parse a toy programming language but I had some problems with "optional" from Control.Applicative.

As an example, I want to parse an optional "a" before the letter "b".

{-# LANGUAGE RecursiveDo #-}
import Text.Earley
import Control.Applicative

grammar = mdo
    test <- rule $ (,) <$> (optional $ namedSymbol 'a') <*> namedSymbol 'b'
    return test

parse xs = fullParses $ parser grammar xs

Now parse "b" results in a Report with expected "a", unconsumed "b". But my desired result would be (Nothing, 'b').

ollef commented 9 years ago

Thanks for the detailed bug report. It made finding the issue very easy.

The issue was that the rule expansion limitation (only expand once per position in the input) was kicking in also for alternatives. This should be fixed now.

kindl commented 9 years ago

Wow. Thank you for the fast fix!