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

Ply can crash if SyntaxError is raised inside a production #93

Closed
PJBaker opened this issue Mar 17, 2016 · 1 comment
Closed

Ply can crash if SyntaxError is raised inside a production #93

PJBaker opened this issue Mar 17, 2016 · 1 comment

Comments

@PJBaker
Copy link

PJBaker commented Mar 17, 2016

According to the documentation, production rules can signal errors by raising the SyntaxError exception. However when I try this, my program crashes due to an IndexError exception inside Ply.

A simple example:

import ply.lex as lex
import ply.yacc as yacc

tokens = ('A', 'B')

t_A = 'a'
t_B = 'b'

t_ignore = ' \t'

def t_error(t):
    print("Illegal character '%s'" % t.value[0])
    t.lexer.skip(1)

lexer = lex.lex()

def p_foo(p):
    """foo : A B"""
    raise SyntaxError()

def p_error(p):
    print("Syntax error in input!")

parser = yacc.yacc()

print(parser.parse('a b'))

The result of running this code is:

Generating LALR tables
Traceback (most recent call last):
  File "PlyTest3.py", line 26, in <module>
    print(parser.parse('a b'))
  File "<...>\ply\yacc.py", line 331, in parse
    return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
  File "<...>\ply\yacc.py", line 1115, in parseopt_notrack
    state = statestack[-1]
IndexError: list index out of range

Should I be doing something differently when raising an error?

@dabeaz
Copy link
Owner

dabeaz commented Aug 30, 2016

Fixed (I hope).

@dabeaz dabeaz closed this as completed Aug 30, 2016
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