# Calling DCG with catch...stumped!

**URL:** <https://swi-prolog.discourse.group/t/calling-dcg-with-catch-stumped/2169>\
**Category:** Help!\
**Created:** [April 14, 2020, 3:11pm UTC](https://swi-prolog.discourse.group/t/calling-dcg-with-catch-stumped/2169 "2020-04-14T15:11:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![emacstheviking](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emacstheviking/32/228_2.png) [@emacstheviking](https://swi-prolog.discourse.group/u/emacstheviking)\
**Post date:** [April 14, 2020, 3:11pm UTC](https://swi-prolog.discourse.group/t/calling-dcg-with-catch-stumped/2169/1 "2020-04-14T15:11:51Z")

</div>

How do I call one DCG rule from inside another but be able to catch any exceptions it may throw ? I/ve tried a few things but I am getting just a little confused

```prolog
foo(In,Out) -->
    % do some stuff but decide you need to
    {throw(...)}.

bar(In,Out) -
  {catch(foo(In,Out), Error, --deal with Error --)}.

```

I have tried both manually listing the coed and attempting to add the extra parameters when making bar NOT use --\> and also I have tried writing a normal predicate that calls phrase on the rule that throws the exception but I am not succeeding yet!

Thanks in advance…I am still trying stuff…  
🤦‍♂️

---

<div class="post-metadata">

**Author:** ![jamesnvc](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jamesnvc/32/14_2.png) [@jamesnvc](https://swi-prolog.discourse.group/u/jamesnvc)\
**Post date:** [April 14, 2020, 3:16pm UTC](https://swi-prolog.discourse.group/t/calling-dcg-with-catch-stumped/2169/2 "2020-04-14T15:16:26Z")

</div>

I was able to make this work by putting the catch around the call to `phrase/3`

```prolog
foo -->
    [0'a],
    { throw(nope) }.
foo --> [X].

bar --> foo.

test_dcg :-
    catch(phrase(bar, `a`, Rest),
          Ex,
          format("Caught ~w", [Ex])
    ),
    format("Rest ~w", [Rest]).

```

---

<div class="post-metadata">

**Author:** ![emacstheviking](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emacstheviking/32/228_2.png) [@emacstheviking](https://swi-prolog.discourse.group/u/emacstheviking)\
**Post date:** [April 14, 2020, 3:17pm UTC](https://swi-prolog.discourse.group/t/calling-dcg-with-catch-stumped/2169/3 "2020-04-14T15:17:55Z")

</div>

Solved!

I was calling the auxiliary predicate in the phrase 🤦‍♂️ indeed.

What I have is this, which is exactly how I thought it should be! But is it doing what I think it’s doing hahaha…

```prolog
matchesF(In, Out, A, B) :-
    catch(phrase(matches(In, Out), A, B),
          ast_error(syntax(_, unexpected_token(_,_))),
          fail).

```

Is there another way to do this, out of curiosity?

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [April 14, 2020, 4:35pm UTC](https://swi-prolog.discourse.group/t/calling-dcg-with-catch-stumped/2169/4 "2020-04-14T16:35:45Z")

</div>

> [@emacstheviking](#):
>
> Is there another way to do this, out of curiosity?

Is it that you really want to throw an exception or just log a message and continue with the parsing, e.g. think of the input as source code and the messages as warnings for such as missing operators, etc.

If you goal is to record info from a stream while parsing the stream using DCGs then using [prolog\_wrap.pl – Wrapping predicates](https://www.swi-prolog.org/pldoc/doc/_SWI_/library/prolog_wrap.pl) may be the way to go. I have not tried this yet but it is on my to do list.

[Search](https://swi-prolog.discourse.group/search?q=prolog_wrap) here for prolog\_wrap for the noted post to get more history on this.

---

<div class="post-metadata">

**Author:** ![emacstheviking](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emacstheviking/32/228_2.png) [@emacstheviking](https://swi-prolog.discourse.group/u/emacstheviking)\
**Post date:** [April 14, 2020, 4:46pm UTC](https://swi-prolog.discourse.group/t/calling-dcg-with-catch-stumped/2169/5 "2020-04-14T16:46:30Z")

</div>

Thanks for the link @EricGT …not what I was after but I will read that later as it looks interesting. Reminiscent of Lisp CLOS :before, :after and :around I guess.

At that point in the parsing process, the clause has reached a point where it knows there is either a syntax error or a premature end of buffer error mid-parse and so I want to fail and bail right there and then and throw() seemed like the way to do it.

It _is_ the way I am doing it…I have ripped apart a lot of stuff today and it’s put me behind a little as to where I wanted to be today but hell, I have learned loads more and still got back to where I was last night and it’s only 6PM in the UK!

---

<div class="post-metadata">

**Author:** ![swi](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@swi](https://swi-prolog.discourse.group/u/swi)\
**Post date:** [April 14, 2020, 9:00pm UTC](https://swi-prolog.discourse.group/t/calling-dcg-with-catch-stumped/2169/6 "2020-04-14T21:00:41Z")

</div>

I would also suggest to look at the brand new [library(intercept)](https://www.swi-prolog.org/pldoc/man?section=intercept) which seems to match your use cases nicely.

EDIT: And it probably has much less overhead than throwing an exception.

---

<div class="post-metadata">

**Author:** ![emacstheviking](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emacstheviking/32/228_2.png) [@emacstheviking](https://swi-prolog.discourse.group/u/emacstheviking)\
**Post date:** [April 14, 2020, 9:11pm UTC](https://swi-prolog.discourse.group/t/calling-dcg-with-catch-stumped/2169/7 "2020-04-14T21:11:10Z")

</div>

Interesting! Feels like a mix of the edcg library and IOC pattern.  
Still not quite what I needed but more stuff to read “later”, I just love the sheer amount of libraries and support for SWI Prolog!

🙂

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [April 15, 2020, 2:10pm UTC](https://swi-prolog.discourse.group/t/calling-dcg-with-catch-stumped/2169/8 "2020-04-15T14:10:17Z")

</div>

Was looking at SWI-Prolog [library(quasi\_quotations): Define Quasi Quotation syntax](https://www.swi-prolog.org/pldoc/man?section=quasiquotations) which might give you some ideas. Specifically look at the [source](https://www.swi-prolog.org/pldoc/doc/_SWI_/library/quasi_quotations.pl?show=src) code.
