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

Peter Driscoll peter_driscoll at bigpond.com
Wed Apr 3 16:34:18 AEST 2002

> I think your own example answers your question.  To me, the DCG syntax
> is much clearer and easier to read than the concatenation-based
> approach.  This would be doubly so if you were constructing a parse
> tree.

The following code demonstrates constructing a parse tree using functions.
It is almost as simple as the syntax checker.

:- type term_info --->
	times_term(factor_info, term_info) ;
	divide_term(factor_info, term_info) ;

:- func term(list(char)) = term_info is semidet.
term(Factor ++ ['*'] ++ Term) =
	times_term(factor(Factor), term(Term)).
term(Factor ++ ['/'] ++ Term) =
	divide_term(factor(Factor), term(Term)).
term(Factor) =

And if the ++ operator was a space operator it would look very similar to
the DCG.

> You might argue that parsing isn't really common enough to warrant a
> special syntax, but given that it's already there, it seems like the
> right way to code a grammar.

Don't get me wrong.  I think DCG's are awesome.  It's just that they seem
like an implementation strategy, which means that they could be taken out of
my hands and put in the hands of the compiler writer.

> For example, in your code above, factoring out the common initial goals in
a disjunction
> would really help.

I was hoping that the compiler might end up doing the factoring for me, at
some point in the future.

Kind Regards

Peter Driscoll

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