Colin G
Colin G

Reputation: 417

Need help starting with Tatsu to parse grammar

I am getting a Tatsu error

"tatsu.exceptions.FailedExpectingEndOfText: (1:1) Expecting end of text"

running a test, using a grammar I supplied - it is not clear what the problem is.

In essence, the statement calling the parser is:

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

The whole python file follows:

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()

Can anyone help me with a fix?

Upvotes: 2

Views: 457

Answers (2)

Adam Trhon
Adam Trhon

Reputation: 2945

I can see two problems with that grammar.

As written in man pages, rule names that start with upper case character have special meaning. Change all the rule names to lower case.

Also let's review IDENTIFIER rule:

IDENTIFIER = {Identifier} ;

This means that identifier can be used multiple times, or may be missing at all. Remove the closure by defining IDENTIFIER directly:

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

You can do the same for NUMERICLITERAL and STRINGLITERAL.

When I did those steps, the expression could be parsed.

Upvotes: 3

Apalala
Apalala

Reputation: 9244

You need to pass the name of the "start" symbol to parse().

You can also define:

start = KIF ;

in the grammar.

Upvotes: 0

Related Questions