[clean-list] Please answer this parsing question

Thomas van Noort thomas at cs.ru.nl
Fri Aug 29 15:50:35 MEST 2008


Hi,

It may sound like a strong claim, but I think most parser combinators
are associative. Meaning, the order in which the arguments of a
combinator are evaluated does not affect its behaviour. Choosing a
certain associativity only improves the readability of defined parsers
since you can omit parentheses. For example, the following three parsers
accept the same sentences:

-- Associative &>
p = twoDigits &> twoDigits &> (<!?> twoDigits undef undef) &> (symbol >>
'Z')

-- Left associative &>
q = ((twoDigits &> twoDigits) &> (<!?> twoDigits undef undef) &>)
(symbol >> 'Z')

-- Right associative &>
r = twoDigits &> (twoDigits &> ((<!?> twoDigits undef undef) &> (symbol
 >> 'Z')))

However, the user has to take the associativity into account when using
different combinators that have different priorities and
associativities. It is the responsibility of the implementation of the
library to keep the left-to-right order of the combinators.

However, I'm not the author of the library so he should correct me if
I'm wrong :)

Regards,
Thomas

metaperl wrote:
> 
> 
> terrence.brannon wrote:
>> (&>) is right associative:
>> // and-combinator that only retains the right hand side result
>> (&>)	infixr 6 // :: (Parser s r t) (Parser s r` t) -> Parser s r` t
>> (&>) p1 p2 :== p1 <&> const p2
>>
>>
>> So when I read this expression:
>> p =  twoDigits &> twoDigits &> (<!?> twoDigits undef undef) &> (symbol
>> 'Z')
>>
>> I should rewrite it to this:
>> p =  ( twoDigits &> ( twoDigits &> ( (<!?> twoDigits undef undef) &>
>> (symbol 'Z') ) ) )
>>
>> But then I get confused about order of execution...
>> won't the innermost parenthetical expression execute first and the parse
>> will attempt to execute this first:
>> ( (<!?> twoDigits undef undef) &> (symbol 'Z') ) 
>>
>> Which would not be the desired parsing order.
>>
>>
> 
> I am right about what right-associativity does to an unparenthesized
> expression <http://en.wikipedia.org/wiki/Associative#More_examples>
> 
> So the question becomes: isn't the first function which attempts to "eat"
> the input string (symbol 'Z')
> 
> But I think the input string is supposed to be parsed from left to right. So
> that is undesirable behavior.
> I would think left-associativity would be more preferred.
> 
> 
> 



More information about the clean-list mailing list