I am working on a project that uses the ply parsing tool. I need to implement the project in iPython but as seen in this post Problems with PLY LEX and YACC, ply is causing issues with the tool. As such, I wanted to know if there is a fix or workaround for the issue that will allow me to use ply and the notebook tool at the same time.
Asked
Active
Viewed 1,140 times
1
-
From the question you linked to: "Ply insists that the grammar be a module, which means it must be in a file." An IPython notebook is not a file. You can try saving the grammar as a file somewhere on your system and importing it into a notebook, I suppose. – Akshat Mahajan Apr 04 '16 at 01:29
-
@AkshatMahajan Just to be clear, a notebook is a file (`.ipynb`), but not a module. – Thomas K Apr 04 '16 at 04:20
1 Answers
3
Perhaps a little bit late for a response, but I just faced the same problem and managed to find a workaround.
To build the lexer, PLY requires a variable named __file__
. So, before calling lex.lex()
, you have to set __file__
to the name of your notebook file.
For example:
[...]
__file__ = "My_Notebook.ipynb"
lexer = lex.lex()
[...]
And if you are using a class (following this example):
class MyLexer(object):
# [...]
# lots and lots of token declarations
# [...]
# Build the lexer
def build(self,**kwargs):
self.lexer = lex.lex(module=self, **kwargs)
my_lexer = MyLexer()
__file__ = "My_Notebook.ipynb"
my_lexer.build()
Also you shouldn't define more than one lexer per notebook file, as pointed by the PLY documentation at the end of section 4.15.
Now, to use yacc
in IPython/Jupyter Notebook, you have to call it like this:
parser = yacc.yacc(write_tables=False)

Niteck
- 93
- 1
- 8