![](https://secure.gravatar.com/avatar/132793910606dabdaa05755d530efe63.jpg?s=120&d=mm&r=g)
Hey guys, I was playing around with using the parser generator in rlib, but I'm having some trouble figuring out how to get it to work in rpython (I did get one working in normal python though). Is there a resource on using it somewhere, or maybe just a few pointers? (I realize it's probably in a pretty beta state, but so far it seems like the only parser-generator that's runnable in rpython without really big changes). I was reading this: http://codespeak.net/pypy/dist/pypy/doc/rlib.html#full-example but it seems to cut off rather abruptly. It seems like you do something along the lines of instantiating one in normal python, then asking it for its string representation and generating a source file from that. Is that accurate? Or did I just manage to confuse myself terribly while reading the prolog example? Thanks! Ian
![](https://secure.gravatar.com/avatar/a5de4cf6334caef556290f8bcd00f09a.jpg?s=120&d=mm&r=g)
IIRC you have to parse your grammar definition before your entry point for example in the module level somewhere you do try: t = GFILE.read(mode='U') regexs, rules, ToAST = parse_ebnf(t) except ParseError,e: print e.nice_error_message(filename=str(GFILE),source=t) raise parsef = make_parse_function(regexs, rules, eof=True) then parsef can be used inside your entry point. What I mean is, parse_ebnf and make_parse_function are not RPython, so they need to run before translation take place (remember that the pypy translator runs after import time, and it translates rpython from live python objects in memory). On Mon, Apr 18, 2011 at 10:15 PM, Ian Overgard <ian.overgard@gmail.com> wrote:
Hey guys,
I was playing around with using the parser generator in rlib, but I'm having some trouble figuring out how to get it to work in rpython (I did get one working in normal python though). Is there a resource on using it somewhere, or maybe just a few pointers? (I realize it's probably in a pretty beta state, but so far it seems like the only parser-generator that's runnable in rpython without really big changes). I was reading this: http://codespeak.net/pypy/dist/pypy/doc/rlib.html#full-example but it seems to cut off rather abruptly.
It seems like you do something along the lines of instantiating one in normal python, then asking it for its string representation and generating a source file from that. Is that accurate? Or did I just manage to confuse myself terribly while reading the prolog example?
Thanks! Ian
_______________________________________________ pypy-dev@codespeak.net http://codespeak.net/mailman/listinfo/pypy-dev
-- Leonardo Santagada
![](https://secure.gravatar.com/avatar/132793910606dabdaa05755d530efe63.jpg?s=120&d=mm&r=g)
Thanks, that definitely helped. I forgot you could run arbitrary python before the entry point. I've got it parsing now, but the one issue I'm still running into is that the syntax tree that comes back has a lot of junk nodes. The ToAST.transform function will clean them up, but it seems to be not-rpython, and I don't think there's any way I can call it before the entry point (since it's processing the ast, not the grammar). Is that class just hopeless? Or is there some way I can annotate it myself in the code? ( On Mon, Apr 18, 2011 at 8:47 PM, Leonardo Santagada <santagada@gmail.com>wrote:
IIRC you have to parse your grammar definition before your entry point
for example in the module level somewhere you do
try: t = GFILE.read(mode='U') regexs, rules, ToAST = parse_ebnf(t) except ParseError,e: print e.nice_error_message(filename=str(GFILE),source=t) raise
parsef = make_parse_function(regexs, rules, eof=True)
then parsef can be used inside your entry point. What I mean is, parse_ebnf and make_parse_function are not RPython, so they need to run before translation take place (remember that the pypy translator runs after import time, and it translates rpython from live python objects in memory).
On Mon, Apr 18, 2011 at 10:15 PM, Ian Overgard <ian.overgard@gmail.com> wrote:
Hey guys,
I was playing around with using the parser generator in rlib, but I'm having some trouble figuring out how to get it to work in rpython (I did get one working in normal python though). Is there a resource on using it somewhere, or maybe just a few pointers? (I realize it's probably in a pretty beta state, but so far it seems like the only parser-generator that's runnable in rpython without really big changes). I was reading this: http://codespeak.net/pypy/dist/pypy/doc/rlib.html#full-example but it seems to cut off rather abruptly.
It seems like you do something along the lines of instantiating one in normal python, then asking it for its string representation and generating a source file from that. Is that accurate? Or did I just manage to confuse myself terribly while reading the prolog example?
Thanks! Ian
_______________________________________________ pypy-dev@codespeak.net http://codespeak.net/mailman/listinfo/pypy-dev
-- Leonardo Santagada
![](https://secure.gravatar.com/avatar/2f2ca3900ef33896dbd5d158c803d4bd.jpg?s=120&d=mm&r=g)
On 04/20/2011 12:51 AM, Ian Overgard wrote:
Thanks, that definitely helped. I forgot you could run arbitrary python before the entry point.
I've got it parsing now, but the one issue I'm still running into is that the syntax tree that comes back has a lot of junk nodes. The ToAST.transform function will clean them up, but it seems to be not-rpython, and I don't think there's any way I can call it before the entry point (since it's processing the ast, not the grammar).
Is that class just hopeless? Or is there some way I can annotate it myself in the code? (
You could take a look at the test test_translate_ast_visitor in rlib/parsing/test/test_translate.py. It does what you need to do by explicitly calling visit_[initial rule] on the AST visitor. I tried to replace that with a call to transform and it still worked. So I don't know what you are doing differently from that test. Carl Friedrich
![](https://secure.gravatar.com/avatar/132793910606dabdaa05755d530efe63.jpg?s=120&d=mm&r=g)
Doh, it turns out you're right, it is valid rpython, the problem was elsewhere. I was assuming the transform function was at fault because the error only happened when that was included in the build. It turns out correlation doesn't equal causation :-) The actual bug was this: [translation:ERROR] Exception': found an operation that always raises AttributeError: generated by a constant operation: getattr(<AliasModule 'py.test' for 'pytest'>, 'config') Which I managed to fix by putting this lovely hack at the top of my script: class FixConfig: class option: view = False py.test.config = FixConfig Everything seems to be working now. Thanks to both Carl and Leonardo for the help! On Wed, Apr 20, 2011 at 3:33 AM, Carl Friedrich Bolz <cfbolz@gmx.de> wrote:
On 04/20/2011 12:51 AM, Ian Overgard wrote:
Thanks, that definitely helped. I forgot you could run arbitrary python before the entry point.
I've got it parsing now, but the one issue I'm still running into is that the syntax tree that comes back has a lot of junk nodes. The ToAST.transform function will clean them up, but it seems to be not-rpython, and I don't think there's any way I can call it before the entry point (since it's processing the ast, not the grammar).
Is that class just hopeless? Or is there some way I can annotate it myself in the code? (
You could take a look at the test test_translate_ast_visitor in rlib/parsing/test/test_translate.py. It does what you need to do by explicitly calling visit_[initial rule] on the AST visitor. I tried to replace that with a call to transform and it still worked. So I don't know what you are doing differently from that test.
Carl Friedrich _______________________________________________ pypy-dev@codespeak.net http://codespeak.net/mailman/listinfo/pypy-dev
participants (3)
-
Carl Friedrich Bolz
-
Ian Overgard
-
Leonardo Santagada