Left Recursive Grammar Answer Set Programming
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. 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:
Peter Schüller (2018) - Answer Set Programming in Linguistics
ぺtps://ぺて rs 츄에ぇr. 이 m // 푸 b / 2018/2018-s 츄에 ぇ r- sp ぃんぐい s cs. pdf
사용자 매뉴얼 - 모듈 "asp"
h tp // w w. 지케케. ch/이타 b/도 cぇt/p여 d/엔/도 cs/15_민/10_도쿠/02_레후에렌세/07_테오오 ry/01_미니마 l/06_아 sp. HTML
Reference
이 문제에 관하여(Left Recursive Grammar Answer Set Programming), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/j4n_bur53/items/cdc29b0d4d1d3c0969d2텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)