[mercury-users] using mercury for parsing/scanning

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Dec 31 14:09:12 AEDT 2001

On 31-Dec-2001, Dave Slutzkin <dave_slutzkin at yahoo.com> wrote:
> When using DCGs to scan character strings, is there an
> alternative to this:
> keyword -->
>    ['K','E','Y','W','O','R','D']
> I'm hoping for some syntactic sugar - I'm too lazy to
> type in all the quotes and commas every time.

You can use

	keyword --> string("KEYWORD").

where string//1 can be defined as

	:- pred string(string, list(char), list(char)).
	string(String) -->
	:- pred chars(list(char), list(char), list(char)).
	chars([]) --> [].
	chars([C|Cs]) --> [C], chars(Cs).

or, if you prefer, as

	:- pred string(string, list(char), list(char)).
	string(String, L0, L) :-
		list__append(string__to_char_list(String), L, L0).

> Also, I'm currently reading input a word at a time and
> then passing around the character strings.  Is it
> better to do this or to read input straight from the
> stream every time?

I don't think it makes that much difference either way.

Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe

More information about the users mailing list