From 53f1b9251440b263d1605b86090e71cf09f6aad1 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sun, 15 May 2016 18:31:42 +0200 Subject: Hack in support for DCGs in Prolog parser --- prolog/parser.py | 38 ++++++-------------------------------- 1 file changed, 6 insertions(+), 32 deletions(-) (limited to 'prolog/parser.py') diff --git a/prolog/parser.py b/prolog/parser.py index 4a3f4b5..8fe96da 100644 --- a/prolog/parser.py +++ b/prolog/parser.py @@ -21,7 +21,7 @@ from .util import Token # PARSER precedence = ( - ('nonassoc', 'FROM'), + ('nonassoc', 'FROM', 'FROMDCG'), ('right', 'IMPLIES'), ('right', 'NOT'), ('nonassoc', 'EQU', 'NEQU', 'EQ', 'NEQ', 'UNIV', 'IS', 'EQA', 'NEQA', 'LT', 'LE', 'GT', 'GE', 'LTL', 'LEL', 'GTL', 'GEL', 'IN', 'INS', 'THROUGH', 'EQFD', 'NEQFD', 'LTFD', 'LEFD', 'GTFD', 'GEFD'), @@ -51,7 +51,8 @@ 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' + '''clause : head FROM or PERIOD + | head FROMDCG or PERIOD''' p[0] = Tree('clause', [p[1], make_token(p, 2), p[3], make_token(p, 4)]) def p_head(p): @@ -198,37 +199,10 @@ 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]]) - +# XXX temporary until the new parser is in place, this also covers { } notation for DCGs 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)]) + 'clpr_constr : term' + p[0] = p[1] def p_error(t): if t is None: -- cgit v1.2.1