Hacker News new | ask | show | jobs
by YeGoblynQueenne 607 days ago
(Not OP)

>> How do you debug DCGs? I get "false." instead of "syntax error at line 23", which is unacceptable for bigger inputs.

You need to include exception handling in your DCG rules. For example, in Prolog-like pseudocode:

  pink_apples([A|As]) --> [A], { red_apple(A), throw(error(type_error(pink_apple,red_apple),_)) }.

  % Raises type error:
  ERROR: Type error: `pink_apple' expected, found `red_apple' (an atom)
  ERROR: In:
  ERROR:   [12] throw(error(type_error(pink_apple,red_apple),_10070))
Called from a source file the error output will list the line in the source file where the exception was raised. There are more tools to debug the error:

https://www.swi-prolog.org/pldoc/man?section=exception

DCGs parse lists, not strings, as such. So the input can be anything you can put in the form of a list.

1 comments

And this is the person who saved me from nearly dropping Prolog!!
Hope you don't end up cursing me down the line :P