dabeaz / sly

Sly Lex Yacc
Other
817 stars 108 forks source link

AttributeError: 'function' object has no attribute 'rules' in the sly Parser #35

Closed MonliH closed 5 years ago

MonliH commented 5 years ago

I recently got this error on Ubuntu 18.04:

Traceback (most recent call last):
  File "chain_interpreter.py", line 1, in <module>
    import chain_lexer, chain_parser
  File "/home/jonathan/Documents/programming/python/Programming Languages/chAIn/chain/src/chain_parser.py", line 6, in <module>
    class ChainParser(Parser):
  File "/home/jonathan/.local/lib/python3.6/site-packages/sly/yacc.py", line 1586, in __new__
    cls._build(list(attributes.items()))
  File "/home/jonathan/.local/lib/python3.6/site-packages/sly/yacc.py", line 1785, in _build
    if not cls.__build_grammar(rules):
  File "/home/jonathan/.local/lib/python3.6/site-packages/sly/yacc.py", line 1677, in __build_grammar
    parsed_rule = _collect_grammar_rules(func)
  File "/home/jonathan/.local/lib/python3.6/site-packages/sly/yacc.py", line 1545, in _collect_grammar_rules
    for rule, lineno in zip(func.rules, range(lineno+len(func.rules)-1, 0, -1)):

AttributeError: 'function' object has no attribute 'rules'

This is intersting because a few mothns ago when I was working on this project, I did not have this problem. I have not changed any of the source code since then. I am on python 3. The start of my chain_parser.py file looks something like this:

from sly import Parser
from chain_lexer import ChainLexer
import pprint

class ChainParser(Parser):  # error occurs on this line?
    tokens = ChainLexer.tokens
    debugfile = 'parser.out'

    @_("program statement")
    def program(self, p):
        return p.program + (p.statement, )

I have looked online but as far as I can tell, no one has gotten this issue before...

All help would be appreciated.

MonliH commented 5 years ago

Nevermind. I solved it. It turns out that I forgot to put an @_() statment before one of my grammar definitions. Here was the error:

# There should  be a @_("id") statment here
def expression(self, p):
    return p.id