需要帮助以Tatsu开头来解析语法

时间:2019-02-21 20:42:04

标签: tatsu

我遇到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()

有人可以帮助我解决问题吗?

谢谢。

科林·戈德堡

1 个答案:

答案 0 :(得分:0)

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

您还可以定义:

start = KIF ;
语法中的