[m-rev.] lex and moose changed
Ralph Becket
rbeck at microsoft.com
Thu Aug 2 19:54:22 AEST 2001
> From: Michael Day [mailto:mikeday at bigpond.net.au]
> Sent: 02 August 2001 02:56
>
> What do people think about scanning using predicates and backtracking?
> I've written some rules like this:
>
> whitespace --> \[' ','\t','\n'].
> digit --> \['0','1','2','3','4','5','6','7','8','9'].
> number --> +digit.
> fraction --> \('.'), number.
> exponent --> \['e','E'], ?(\['+','-']), number.
> float --> ((number, ?(fraction)) ; fraction), ?(exponent).
>
> rule([]) --> +whitespace.
> rule([float]) --> float.
> rule([number]) --> number.
>
> that all operate on strings using first_char (before I found
> out that it's
> horrendously inefficient...) and would probably be better off
> working on
> lists of characters or generic streams or something.
Lists of characters are unfortunately horrendously inefficient for
all but small files.
It's hard to make this sort of thing work on files without reading
the whole thing in first, although that's probably much less of a
problem these days.
I confess I've often found it hard to cleanly accumulate the
matched characters to the point where I can do something useful
with them. For example, your number rule would be more useful
if it told you what number it had identified. You then end up
with stuff like
digit(0) --> ['0'].
...
digit(9) --> ['9'].
number(N) --> number(0, N).
number(N0, N) --> ( if digit(D) then number(10 * N0 + D, N)
else { N = N0 }
).
It's starting to become less attractive.
That said, DCGs are certainly great tools.
- Ralph
--------------------------------------------------------------------------
mercury-reviews mailing list
post: mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------
More information about the reviews
mailing list