Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to terminate parsing on an error detection #68

Closed
Renegade85 opened this issue Apr 14, 2021 · 2 comments
Closed

How to terminate parsing on an error detection #68

Renegade85 opened this issue Apr 14, 2021 · 2 comments

Comments

@Renegade85
Copy link

Hello,

this is more a question then a bug report. How to properly terminate parsing, if the parser stumbles on an error? I just want to report that one error, but don't want to continue on parsing nor execute the AST. Just throw away everything and terminate gracefully reporting the line where the error has been detected.

Is there a way to clear the parsing stack and iterate over the rest of the tokens in the input or something similar?

My error function follows:

error(self, t):
    while True:
        tok = next(self.tokens, None)
        if not tok:
            break
    self.restart()

    return tok

However, this seems to be not working. Just one more point, which may be important, the error is "artificial" meaning, I got parts of my grammar which are unsupported and if the parser parses such code, the "error()" method is invoked.

@_(some_syntax)
def some_rule(self, t):
    self.error(t)

The error I'm getting from python:

File "C:\Users\nxf25094\Documents\sProjects\git_repos\spsdk\venv38\lib\site-packages\sly\yacc.py", line 2091, in parse
self.state = goto[statestack[-1]][pname]
IndexError: list index out of range

@dabeaz
Copy link
Owner

dabeaz commented Apr 14, 2021

The easiest way to terminate everything would be to raise an exception. For example:

raise SystemExit("Goodbye")

(Of course, you could make your own exception too).

@Renegade85
Copy link
Author

Renegade85 commented Apr 15, 2021

Seems the restart of the parser was not necessary. Just pumping out all the tokens and returning the last one does the trick.

def error(self, t):
        while True:
            tok = next(self.tokens, None)
            if not tok:
                break

        return tok

In case this will cause issues in the future, I'll go the exception way. :-)

Thank you very much for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants