LoginSignup
0
1

More than 3 years have passed since last update.

Left Recursive Grammar Answer Set Programming

Last updated at Posted at 2020-01-09

Just out of curiosity we made a little test whether answer set programming can be used to parse a left recursive grammer. Left recursive grammars are notoriously alien to Prolog, since the default search strategy of Prolog is depth first searching. The test grammar defines arithmetic expressions and we want to build a little calculator. Indicating actions with square brackets and directly manipulating the parameters we can formulate the grammar as follows:

EXPR(C) -> EXPR(A) + TERM(B) [ C is A+B ]
EXPR(C) -> EXPR(A) - TERM(B) [ C is A-B ]
EXPR(B) -> - TERM(A) [ B is -A] 
EXPR(A) -> TERM(A)

TERM(C) -> FACTOR(A) * TERM(B) [ C is A*B ]
TERM(C) -> FACTOR(A) / TERM(B) [ C is A/B ]
TERM(A) -> FACTOR(A)

FACTOR(A) -> ( EXPR(A) )
FACTOR(A) -> INTEGER(A)

One way to get out of the dilemma of left recursion is to rework the grammar and try to find a non-left recursion solution. A non-left recursive solution can then be solved by the usual Prolog definite clause grammars. The Prolog text calc.p contains such a rewriting. Basically we add a new non-terminal expr_rest so that we can perform a right recursion instead of a left recursion:

:- reexport(library(standard/dcg)).

expr(Z) --> ['-'], !, term(X), {Y is -X}, expr_rest(Y, Z).
expr(Y) --> term(X), expr_rest(X, Y).

expr_rest(X, T) --> ['+'], !, term(Y), {Z is X+Y}, expr_rest(Z, T).
expr_rest(X, T) --> ['-'], !, term(Y), {Z is X-Y}, expr_rest(Z, T).
expr_rest(X ,X) --> [].

In the end we will be able to parse an arithmetic expression and obtain a result at the same time:

?- phrase(expr(X),['-',12,'+',34,'*',56,'+',78]).
X = 1970

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. 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 again 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)).

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:

Unbenannt.PNG

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

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1