[Haskell-cafe] happy + alex parsing question

Roman Dzvinkovsky romand.ne at gmail.com
Wed Feb 16 16:31:31 CET 2011


Hi,

using alex+happy, how could I parse lines like these?

> "mr <username> says <message>\n"

where both <username> and <message> may contain arbitrary characters (except
eol)?

If I make lexer tokens

> "mr "    { T_Mr }
> " says " { T_Says }
> \r?\n    { T_Eol }
> .        { T_Char $$ }

and parser

> 'mr '    { T_Mr }
> ' says ' { T_Says }
> eol      { T_Eol }
> char     { T_Char }

...

> line :: { (String, String) }
>      : 'mr ' string ' says ' string eol { ($2, $4) }

> string :: { String }
>        : char        { [ $1 ] }
>        | char string { $1 : $2 }

then I get error when <username> or <message> contain "mr "
substrings, because parser encounters T_Mr token.

Workaround is mention all small tokens in my <string> definition:

> string :: { String }
>        :                 { [] }
>        | 'mr ' string    { "mr "    ++ $2 }
>        | ' says ' string { " says " ++ $2 }
>        | char string     { $1 : $2 }

but that is weird and I'm sure there is a better way.

Thanks for advance,
Roman.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110216/460d3779/attachment.htm>


More information about the Haskell-Cafe mailing list