[mercury-users] Is DCG really appropriate, even for the things it does well?

Ralph Becket rafe at cs.mu.OZ.AU
Wed Apr 3 16:47:19 AEST 2002


Hi,

are you suggesting a fix for a problem with DCGs ('fraid I still don't
see what the problem is with them in this context!)?

Peter Driscoll, Wednesday,  3 April 2002:
> 
> I am really suggesting that the compiler would see,
> 
> term(Factor ['*'] Term) :-
> 	factor(Factor), term(Term).
> 
> and translate it (internally) into,
> 
> term --> factor, ['*'], term.
> 
> How.  Sorry if this is a little informal.
> 
> Step 1:
> 	Identify that predicate is a candidate.
> 	-	Has a list(T) parameter.
> 	-	Formal parameter is like list(T) ++ list(T) ... (If space operator like

[Point of style: please stick to 80 character columns as God intended.]

> list(T) list(T) ...).
> 	-	Other checks?

Okay, so you'd have to use juxtaposition since ++ is already taken.  And
you're also leaving the DCG arguments implicit.  Which, on top of your
translation, means you really just want a different syntax for DCGs.
DCG syntax is already pretty lightweight as far as grammars are
concerned.

Also, how could the compiler know if

	term(Factor) :- ...

should be translated by your scheme or treated literally?

> Step 2:
> 	Change list(T) parameter to a DCG form.
> 	-	Replace the parameter with a list(T)::out parameter used to return the
> text read from DCG.
> 	-	Add two parameters for DCG.
> 
> :- pred term(list(char)::out, list(char)::in, list(char)::out) is semidet.
> term(Result) -->
> 	(
> 		Factor, ['*'], Term			%	The DCG bit.
> 	),
> 	(
> 		Result = Factor ++ ['*'] ++ Term	%	Calculate the result
> 	),
> 	(
> 		factor(Factor), term(Term)		%	The body.
> 	).

Your Result is just the prefix that was consumed - why would you want to
reproduce it?  Usually you want to return the result of some processing
of that prefix (e.g. a parse tree.)

Cheers,

Ralph

--------------------------------------------------------------------------
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