Question: Creating a Parser object by subclassing ?

112 views
Skip to first unread message

Geoff White

unread,
Feb 8, 2011, 3:35:38 PM2/8/11
to ply-...@googlegroups.com
Hi there,
I want to build a parser for  DSL (Domain Specific Language) that I'm creating for a tool that I'm writing.  I want to put most of the parser and rule definitions in a single file but want to reference a bunch of objects that are defined and initiated in other files.  It would be nice if I could build a Parser baseclass that I could inherit and then just override the the p_functions that I want to provide actions to.
so something like

class myParser(Plyparser):

    def p_function(self,p):
        """ term : a b c """
        #new code to do some stuff overriding p_function in the base class

newparser = myParser()
newparser.activate()  # this invokes myParser.lex(0 and myParser.yacc()
newparser.parse(some_stream)

That way I could keep my interface more clean and could work on the backend without having to have the entire parser definition present in the file yet not changing much.

I suppose I could just go the opposite way and include the backend code into the file that defines the parser but this seems kinda messy to me.


I've tried to build this in a naive sense and  all I got was...

l0r3zz@ambika:~/workspace/NVPtool/src$ ./nvplang.py
nvptool > save ;
WARNING: No t_error rule is defined
ERROR: no rules of the form p_rulename are defined


I can see this is a namespace issue in that when the parser is built, it expects all of the rules to begine with p_  and this is not the case when the rules are methods in a class definition.  I can make a baseclass if I define all of the rules in the __init__ method but then I can't override the rules individually.

Any suggestions?
Reply all
Reply to author
Forward
0 new messages