Hello. Can anyone help me writing a parser for parsing a simplified list of ABC fields (http://www.gre.ac.uk/~c.walshaw/abc/). The grammar I want to test with is the following ------------------ file ::= *field field ::= fieldA | fieldB fieldA ::= "A:" text endOfLine fieldB ::= "B:" text endOfLine restTextB restTextB ::= (text endOfLine) 'not starting with "A:" or "B:"' text ::= *(LETTER | DIGIT | SPACE | ":") endOfLine ::= LF | CR LF ------------------ The parser I am trying: ------------------ file = many field field = fieldA <|> fieldB fieldA = do string "A:" text endOfLine fieldB = do string "B:" text endOfLine restTextB -- not starting with "A:" or "B:" restTextB = do text endOfLine text = many (letter <|> digit <|> space <|> char ':') endOfLine = string "\n" | string "\r\n" ------------------ Example of input: ------------------ A:1 B:Traditional A:2 B:Line 1 Line 2 A Line 3 BBLine 4 A:3 ------------------ Any sugestion on how to implement the 'not starting with "A:" or "B:"' problem? Romildo -- Prof. José Romildo Malaquias Departamento de Computação - Universidade Federal de Ouro Preto http://www.decom.ufop.br/prof/romildo/ malaquias@iceb.ufop.br http://uber.com.br/romildo/ romildo@uber.com.br
Jose Romildo Malaquias wrote:
Any sugestion on how to implement the 'not starting with "A:" or "B:"' problem?
Hi, I'm not familiar with Parsec, but in ParseLib it's fairly straightforward, first you add a new combinator: ifnot :: Parser a -> Parser b -> Parser b ifnot (P p1) (P p2) = P ( \s -> if parseOk (p1 s) then [] else p2 s ) where parseOk :: [(a,String)] -> Bool parseOk [x] = True parseOk _ = False then you can say: ifnot parse_a_or_b parse_foo You will need the sources of ParseLib to try it out though, because "P" isn't exported. Jan
participants (2)
-
Jan Kort -
Jose Romildo Malaquias