Thanks,<br>adding state to lexer seems to be the way to go.<br><br><div class="gmail_quote">2011/2/16 Mihai Maruseac <span dir="ltr"><<a href="mailto:mihai.maruseac@gmail.com">mihai.maruseac@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div><div></div><div class="h5">On Wed, Feb 16, 2011 at 5:31 PM, Roman Dzvinkovsky <<a href="http://romand.ne" target="_blank">romand.ne</a>@<a href="http://gmail.com" target="_blank">gmail.com</a>> wrote:<br>
> Hi,<br>
><br>
> using alex+happy, how could I parse lines like these?<br>
><br>
>> "mr <username> says <message>\n"<br>
><br>
> where both <username> and <message> may contain arbitrary characters (except<br>
> eol)?<br>
><br>
> If I make lexer tokens<br>
><br>
>> "mr " { T_Mr }<br>
>> " says " { T_Says }<br>
>> \r?\n { T_Eol }<br>
>> . { T_Char $$ }<br>
><br>
> and parser<br>
><br>
>> 'mr ' { T_Mr }<br>
>> ' says ' { T_Says }<br>
>> eol { T_Eol }<br>
>> char { T_Char }<br>
><br>
> ...<br>
><br>
>> line :: { (String, String) }<br>
>> : 'mr ' string ' says ' string eol { ($2, $4) }<br>
><br>
>> string :: { String }<br>
>> : char { [ $1 ] }<br>
>> | char string { $1 : $2 }<br>
><br>
> then I get error when <username> or <message> contain "mr "<br>
> substrings, because parser encounters T_Mr token.<br>
><br>
> Workaround is mention all small tokens in my <string> definition:<br>
><br>
>> string :: { String }<br>
>> : { [] }<br>
>> | 'mr ' string { "mr " ++ $2 }<br>
>> | ' says ' string { " says " ++ $2 }<br>
>> | char string { $1 : $2 }<br>
><br>
> but that is weird and I'm sure there is a better way.<br>
><br>
<br>
</div></div>I don't have an implementation right now but you could try having some<br>
states or user data in which to record whether you have already parsed<br>
the 'mr ' part (etc..) Guess you could use monadUserData parser (just<br>
like I've found after a night without sleep [1] - solved now).<br>
<font color="#888888"><br>
--<br>
Mihai<br>
<br>
[1]: <a href="http://www.haskell.org/pipermail/haskell-cafe/2011-February/089330.html" target="_blank">http://www.haskell.org/pipermail/haskell-cafe/2011-February/089330.html</a><br>
</font></blockquote></div><br>