1

我收到 Tatsu 错误

“tatsu.exceptions.FailedExpectingEndOfText: (1:1) 期望文本结束”

使用我提供的语法运行测试 - 目前尚不清楚问题是什么。

本质上,调用解析器的语句是:

ast = parse(GRAMMAR, '(instance ?FIFI Dog)')

整个python文件如下:

GRAMMAR = """

@@grammar::SUOKIF

KIF      = {KIFexpression}* $ ;

WHITESPACE = /\s+/ ;
StringLiteral = /['"'][A-Za-z]+['"']/ ;
NumericLiteral = /[0-9]+/ ;
Identifier = /[A-Za-z]+/ ;
LPAREN = "(" ;
RPAREN = ")" ;
QUESTION = "?" ;
MENTION = "@" ;
EQUALS = "=" ;
RARROW = ">" ;
LARROW = "<" ;
NOT = "not"|"NOT" ;
OR = "or"|"OR" ;
AND = "and"|"AND" ;
FORALL = "forall"|"FORALL" ;
EXISTS = "exists"|"EXISTS" ;
STRINGLITERAL = {StringLiteral} ;
NUMERICLITERAL = {NumericLiteral} ;
IDENTIFIER = {Identifier} ;

KIFexpression
     = Word
       | Variable
       | String
       | Number
       | Sentence
       ;

Sentence = Equation
       | RelSent
       | LogicSent
       | QuantSent
       ;

LogicSent
     = Negation
       | Disjunction
       | Conjunction
       | Implication
       | Equivalence
       ;

QuantSent
     = UniversalSent
       | ExistentialSent
       ;

Word     = IDENTIFIER ;

Variable = ( QUESTION | MENTION ) IDENTIFIER ;

String   = STRINGLITERAL ;

Number   = NUMERICLITERAL ;

ArgumentList
     = {KIFexpression}*
     ;

VariableList
     = {Variable}+
     ;

Equation = LPAREN EQUALS KIFexpression KIFexpression RPAREN ;

RelSent  = LPAREN ( Variable | Word ) ArgumentList RPAREN ;

Negation = LPAREN NOT KIFexpression RPAREN ;

Disjunction
     = LPAREN OR ArgumentList RPAREN
     ;

Conjunction
     = LPAREN AND ArgumentList RPAREN
     ;

Implication
     = LPAREN EQUALS RARROW KIFexpression KIFexpression RPAREN
     ;

Equivalence
     = LPAREN LARROW EQUALS RARROW KIFexpression KIFexpression RPAREN
     ;

UniversalSent
     = LPAREN FORALL LPAREN VariableList RPAREN KIFexpression RPAREN
     ;

ExistentialSent
     = LPAREN EXISTS LPAREN VariableList RPAREN KIFexpression RPAREN
     ;

"""

if __name__ == '__main__':
    import pprint
    import json
    from tatsu import parse
    from tatsu.util import asjson

    ast = parse(GRAMMAR, '(instance ?FIFI Dog)')
    print('# PPRINT')
    pprint.pprint(ast, indent=2, width=20)
    print()

    print('# JSON')
    print(json.dumps(asjson(ast), indent=2))
    print()

任何人都可以帮我解决问题吗?

谢谢。

科林·戈德堡

4

2 回答 2

1

我可以看到该语法有两个问题。

手册页中所述,以大写字符开头的规则名称具有特殊含义。将所有规则名称更改为小写。

另外让我们回顾一下IDENTIFIER规则:

IDENTIFIER = {Identifier} ;

这意味着标识符可以被多次使用,或者可能完全丢失。通过直接定义移除闭包IDENTIFIER

IDENTIFIER = /[A-Za-z]+/ ;

你可以对NUMERICLITERAL和做同样的事情STRINGLITERAL

当我执行这些步骤时,可以解析表达式。

于 2020-12-25T23:48:57.830 回答
0

您需要将“开始”符号的名称传递给parse().

您还可以定义:

start = KIF ;

在语法中。

于 2019-02-25T16:43:10.727 回答