Answer set programming (ASP) provides another route to implement grammars.
ASP can be implemented with non-deterministic forward chaining and this is what
our library(minimal/asp) provides. The result of ASP are then different models
of the given rules. We use here ASP models to represent a Cocke-Younger-Kasami
chart. We begin our chart with the given words we want to parse which are
represented by word/3 facts. Compared to DCG we do not pass around anymore
lists but instead word positions. The Prolog text calc2.p shows such an implementation
of an ASP based parser. All rules are now (<=)/2 rules, means they are forward
chaining rules. And all heads are now choose/1 heads, means they make a ASP
model choice. We explain how expr is realized, the term is realized similary.
Since we do not have an automatic translation, we did the translation manually.
We will provide the words from right to left and only trigger at the beginning
of each attributed grammar rule:
choose([expr(C, I, O)]) <= posted(expr(A, I, H)), word('+', H, J), term(B, J, O), C is A+B.
choose([expr(C, I, O)]) <= posted(expr(A, I, H)), word('-', H, J), term(B, J, O), C is A-B.
choose([expr(B, I, O)]) <= posted(word('-', I, H)), term(A, H, O), B is -A.
choose([expr(A, I, O)]) <= posted(term(A, I, O)).
As can be seen no extra predicate expr_rest was needed and the translation from
grammar to rules was 1-1. The same happens for term. The execution of such a grammar
requires that first the words are posted from right to left, and the result can
then be read off from the corresponding non-terminal:
?- post(word(78,7,8)), post(word('+',6,7)), post(word(56,5,6)), post(word('*',4,5)),
post(word(34,3,4)), post(word('+',2,3)), post(word(12,1,2)), post(word('-',0,1)),
expr(X,0,8).
X = 1970
We have also made a Prolog text show.p which allows visualizing the ASP model as
a parsing chart. We simply use the common triangular matrix representation. The
parsing chart for the above arithmetic expression has looks as follows:
Peter Schüller (2018) - Answer Set Programming in Linguistics
https://peterschueller.com//pub/2018/2018-schueller-asp-linguistics.pdf
User Manual - Module "asp"
http://www.jekejeke.ch/idatab/doclet/prod/en/docs/15_min/10_docu/02_reference/07_theory/01_minimal/06_asp.html