summaryrefslogtreecommitdiff
path: root/prolog/parser.py
blob: 19d2da54bcd2010f58c37882ab9fdbc3bbd81ec6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# CodeQ: an online programming tutor.
# Copyright (C) 2015 UL FRI
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from nltk import Tree
import ply.yacc as yacc
from .lexer import tokens
from .util import Token

# PARSER
precedence = (
    ('nonassoc', 'FROM'),
    ('right', 'IMPLIES'),
    ('right', 'NOT'),
    ('nonassoc', 'EQU', 'NEQU', 'EQ', 'NEQ', 'UNIV', 'IS', 'EQA', 'NEQA', 'LT', 'LE', 'GT', 'GE', 'LTL', 'LEL', 'GTL', 'GEL'),
    ('left', 'PLUS', 'MINUS'),
    ('left', 'STAR', 'DIV', 'IDIV', 'MOD'),
    ('nonassoc', 'POW'),
    ('right', 'UMINUS', 'UPLUS'),
    ('nonassoc', 'UINTEGER', 'UREAL'),
    ('nonassoc', 'NAME', 'VARIABLE', 'STRING'),
    ('nonassoc', 'PERIOD'),
    ('nonassoc', 'LBRACKET', 'RBRACKET', 'LPAREN', 'RPAREN', 'COMMA', 'SEMI', 'PIPE', 'LBRACE', 'RBRACE')
)

def make_token(p, n):
    lextoken = p.slice[n]
    return Token(lextoken.type, lextoken.value, lextoken.lexpos)

def p_text_empty(p):
    'text : '
    p[0] = Tree('text', [])
def p_text_clause(p):
    'text : text clause'
    p[0] = p[1]
    p[0].append(p[2])

def p_clause_head(p):
    'clause : head PERIOD'
    p[0] = Tree('clause', [p[1], make_token(p, 2)])
def p_clause_rule(p):
    'clause : head FROM or PERIOD'
    p[0] = Tree('clause', [p[1], make_token(p, 2), p[3], make_token(p, 4)])

def p_head(p):
    'head : term'
    p[0] = Tree('head', [p[1]])

def p_or_single(p):
    'or : if'
    p[0] = p[1]
def p_or_if(p):
    'or : or SEMI if'
    if p[1].label() == 'or':
        p[0] = p[1]
    else:
        p[0] = Tree('or', [p[1]])
    p[0].append(make_token(p, 2))
    p[0].append(p[3])

def p_if_single(p):
    'if : and'
    p[0] = p[1]
def p_if_and(p):
    'if : and IMPLIES if'
    p[0] = Tree('if', [p[1], make_token(p, 2), p[3]])

def p_and_single(p):
    'and : term'
    p[0] = p[1]
def p_and_term(p):
    'and : and COMMA term'
    if p[1].label() == 'and':
        p[0] = p[1]
    else:
        p[0] = Tree('and', [p[1]])
    p[0].append(make_token(p, 2))
    p[0].append(p[3])

# Special case for zero-arity predicates supported by SWI-Prolog.
def p_term_functor_zero(p):
    'term : functor LPAREN RPAREN'
    # No whitespace allowed between functor and LPAREN.
    t2 = make_token(p, 2)
    if p[1][0].pos + len(p[1][0].val) < t2.pos:
        raise SyntaxError('whitespace before ' + str(t2))
    p[0] = Tree('term', [p[1], t2, make_token(p, 3)])
def p_term_functor(p):
    'term : functor LPAREN args RPAREN'
    # No whitespace allowed between functor and LPAREN.
    t2 = make_token(p, 2)
    if p[1][0].pos + len(p[1][0].val) < t2.pos:
        raise SyntaxError('whitespace before ' + str(t2))
    p[0] = Tree('term', [p[1], t2, p[3], make_token(p, 4)])

def p_term_or(p):
    'term : LPAREN or RPAREN'
    p[0] = Tree('term', [make_token(p, 1), p[2], make_token(p, 3)])
def p_term_binary(p):
    '''term : term PLUS term
            | term MINUS term
            | term STAR term
            | term POW term
            | term DIV term
            | term IDIV term
            | term MOD term

            | term EQU term
            | term NEQU term
            | term EQ term
            | term NEQ term
            | term UNIV term
            | term IS term
            | term EQA term
            | term NEQA term
            | term LT term
            | term LE term
            | term GT term
            | term GE term
            | term LTL term
            | term LEL term
            | term GTL term
            | term GEL term'''
    p[0] = Tree('term', [p[1], make_token(p, 2), p[3]])
def p_term_unary(p):
    '''term : NOT term
            | MINUS term %prec UMINUS
            | PLUS term %prec UPLUS'''
    p[0] = Tree('term', [make_token(p, 1), p[2]])
def p_term_list(p):
    'term : list'
    p[0] = Tree('term', [p[1]])

def p_term_simple(p):
    '''term : STRING
            | NAME
            | UINTEGER
            | UREAL
            | VARIABLE'''
    p[0] = Tree('term', [make_token(p, 1)])

def p_term_clpr(p):
    'term : LBRACE clpr RBRACE'
    p[0] = Tree('term', [make_token(p, 1), p[2], make_token(p, 3)])

def p_args_single(p):
    'args : term'
    p[0] = Tree('args', [p[1]])
def p_args_term(p):
    'args : args COMMA term'
    p[0] = p[1]
    p[0].append(make_token(p, 2))
    p[0].append(p[3])

def p_list_empty(p):
    'list : LBRACKET RBRACKET'
    p[0] = Tree('list', [make_token(p, 1), make_token(p, 2)])
def p_list(p):
    'list : LBRACKET args RBRACKET'
    p[0] = Tree('list', [make_token(p, 1)] + list(p[2]) + [make_token(p, 3)])
def p_list_tail(p):
    'list : LBRACKET args PIPE term RBRACKET'
    p[0] = Tree('list', [make_token(p, 1)] + list(p[2]) + [make_token(p, 3), p[4], make_token(p, 5)])

def p_functor(p):
    'functor : NAME'
    p[0] = Tree('functor', [make_token(p, 1)])

# CLP(R) syntax
def p_clpr_single(p):
    'clpr : clpr_constr'
    p[0] = Tree('clpr', [p[1]])
def p_clpr_more(p):
    '''clpr : clpr_constr COMMA clpr
            | clpr_constr SEMI clpr'''
    p[0] = Tree('clpr', [p[1], make_token(p, 2), p[3]])

def p_clpr_constr(p):
    '''clpr_constr : clpr_expr LT clpr_expr
                   | clpr_expr GT clpr_expr
                   | clpr_expr LE clpr_expr
                   | clpr_expr GE clpr_expr
                   | clpr_expr NEQA clpr_expr
                   | clpr_expr EQA clpr_expr
                   | clpr_expr EQU clpr_expr'''
    p[0] = Tree('clpr_constr', [p[1], make_token(p, 2), p[3]])

def p_clpr_expr_value(p):
    '''clpr_expr : VARIABLE
                 | UINTEGER
                 | UREAL'''
    p[0] = Tree('clpr_expr', [make_token(p, 1)])
def p_clpr_expr_unary(p):
    '''clpr_expr : MINUS clpr_expr %prec UMINUS
                 | PLUS clpr_expr %prec UPLUS'''
    p[0] = Tree('clpr_expr', [make_token(p, 1), p[2]])
def p_clpr_expr_binary(p):
    '''clpr_expr : clpr_expr PLUS clpr_expr
                 | clpr_expr MINUS clpr_expr
                 | clpr_expr STAR clpr_expr
                 | clpr_expr DIV clpr_expr
                 | clpr_expr POW clpr_expr
                 | clpr_expr NAME clpr_expr'''  # XXX a hack to allow (X)*(X) without spaces
    p[0] = Tree('clpr_expr', [p[1], make_token(p, 2), p[3]])
def p_clpr_expr_parens(p):
    'clpr_expr : LPAREN clpr_expr RPAREN'
    p[0] = Tree('clpr_expr', [make_token(p, 1), p[2], make_token(p, 3)])

def p_error(t):
    if t is None:
        raise SyntaxError('unexpected end of file')
    else:
        raise SyntaxError('{}: unexpected {}'.format(t.lexpos, t.value))

parser = yacc.yacc(debug=False)

if __name__ == '__main__':
    from .util import stringify
    while True:
        try:
            s = input('> ')
        except EOFError:
            break
        if not s:
            continue
        ast = parser.parse(s)
        print(stringify(ast))