Christopher Sharpe writes:
> [...]
>
> What I am trying to do is find, in input such as an HTML file, like
> "ab<table>c", the string before the keyword such as "table". So I
> try the keyword parsers (in the list "ps" below) at each point in
> the input, and add the character to my answer if all the parsers
> fail.
How about taking the reverse out of the parser?
import ParseLib
tag :: Parser String
tag = ops [(string s, "") | s <- ["<table>", "</table>"]]
pbft :: Parser String -- Part Before First Tag
pbft = tag +++ do {c <- item; cs <- pbft; return (c:cs)} +++ return ""
rpbft :: String -> [(String, String)] -- Reverse Part Before First Tag
rpbft s = [(reverse v, out) | (v, out) <- papply pbft s]
test :: [(String, String)]
test = rpbft "ab<table>c"
That ruins the parser's prospects of tail recursion, but achieves some
Good Things:
- The parser produces the String you're trying to find as a lazy
list, with a fixed limit on its look-ahead, so the parser runs in
constant space,
- The tail recursion question is confined to reverse, and
- The P primitive is not used.
Regards,
Tom