I'm trying to write a Parsec parser for a language which authorizes
(this is a simplified example) a or a,b,c or a,c or a,b. (I
can change the grammar but not the language.)
The first attempt was:
* CUT HERE
import Text.ParserCombinators.Parsec
import System (getArgs)
comma = char ','
On Mon, Jul 31, 2006 at 09:04:32AM +0200, Stephane Bortzmeyer wrote:
minilang = do
char 'a'
try (optional (do {comma ; char 'b'}))
optional (do {comma ; char 'c'})
eof
return OK
parse error at (line 1, column 2):
unexpected c
expecting b
Stephane Bortzmeyer wrote:
minilang = do
char 'a'
try (optional (do {comma ; char 'b'}))
optional (do {comma ; char 'c'})
eof
return OK
* CUT HERE ***
parse error at (line 1, column 2):
unexpected c
expecting b
Apparently,
The semantics of Parsec's optional operation are what is causing the problem.
optional foo can have 3 results:
1) foo can succeed, optional succeeds, proceed to next command
2) foo can fail without consuming any input, optional succeeds proceed to
next command
3) foo can fail after