By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

python bytecode grammar

P: n/a
where I can find the grammar of python bytecode ? ( better if is in BCF
).

Jul 19 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
M1st0 wrote:
where I can find the grammar of python bytecode ? ( better if is in BCF
).


There is no grammar for bytecodes - the are like assembly instructions.
And what's BCF supposed to mean - BNF is a form for grammars, BCF I
never heard of.

And besides that: tell us what you're after, we might help and suggest
other ways than creating/manipulating bytecode, as this is considered a
implementation detail that is not supposed to work across
implementations (like jython) and is subject to unannounced changes
between major revisions of python.

Diez
Jul 19 '05 #2

P: n/a
Ops yes is BNF :P Bacus Normal Form if I am not wrong...

However......

I'am tryng to recognizing patterns in a bytecoded file in orderd to
optimize...

But I would like to "parse" i.e reconstruct it in something like a
tree..
in order to apply rules on a tree recursively.

I have seen compile.c in the Python dist...

Jul 19 '05 #3

P: n/a
M1st0 wrote:
Ops yes is BNF :P Bacus Normal Form if I am not wrong...

However......

I'am tryng to recognizing patterns in a bytecoded file in orderd to
optimize...

But I would like to "parse" i.e reconstruct it in something like a
tree..
in order to apply rules on a tree recursively.


But bytecode is like assembly - there is no tree-structure. A opcode is
followed by a number of arguments, and oopcodes are in a seqence.

However the bytecode is _generated_ from the AST somehow. Maybe you can
work on that. Also take a look at psyco, it already does optimizations
for numeric calculations.

Besides: I serously doubt you can do much optimization on the
bytecodelevel itself, as it is very highlevel. The optimization efforts
like in psyco don't alter bytecode - they replace it....

Diez
Jul 19 '05 #4

P: n/a
Ok I tried :).

Jul 19 '05 #5

P: n/a
"M1st0" <mi*******@gmail.com> writes:
Ops yes is BNF :P Bacus Normal Form if I am not wrong...

However......

I'am tryng to recognizing patterns in a bytecoded file in orderd to
optimize...

But I would like to "parse" i.e reconstruct it in something like a
tree..
in order to apply rules on a tree recursively.

I have seen compile.c in the Python dist...


I've never looked, but I'm assuming it is defined in the compiler and
in the interpreter, and hopefully documented elsehwere, like:
http://python.fyxm.net/peps/pep-0330.html

Related:
http://mail.python.org/pipermail/pyt...er/050542.html
http://search.cpan.org/search?query=...ecode&mode=all
--
ha************@boeing.com
6-6M21 BCA CompArch Design Engineering
Phone: (425) 294-4718
Jul 19 '05 #6

P: n/a

"M1st0" <mi*******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
where I can find the grammar of python bytecode ? ( better if is in BCF


I believe the top-level production is something like
BYTECODE := (OPCODE ARGS)*
See the documentation for the dis module for symbolic opcodes and
corresponding args.

Looking for patterns in bytecodes is one way CPython has been sped up.
Previous results have occasionally been reported on the Python development
list, archived somewhere at python.org. Examples I remember are the
frequencies of both individual bytecodes and of pair of codes.

For parse trees, you should look at the parser and compiler modules.

Terry J. Reedy

Jul 19 '05 #7

P: n/a
"Terry Reedy" <tj*****@udel.edu> writes:
"M1st0" <mi*******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
where I can find the grammar of python bytecode ? ( better if is in
BCF


I believe the top-level production is something like
BYTECODE := (OPCODE ARGS)*


ROTFL :)
Jul 19 '05 #8

P: n/a

"Peter Dembinski" <pd***@gazeta.pl> wrote in message
news:87************@hector.domek...
"Terry Reedy" <tj*****@udel.edu> writes:
I believe the top-level production is something like
BYTECODE := (OPCODE ARGS)*


ROTFL :)


Glad to make your day ;-)

I am aware that since ARGS depends on OPCODE, the above would lead to
context-dependent productions for ARGS. Upon thoroughly perusing
http://docs.python.org/lib/bytecodes.html
18.10.1 Python Byte Code Instructions

I discovered that there is at most one (non-stack) argument in the byte
stream (contrary to the possibly plural implication of "All of the
following opcodes expect arguments"). So the above could be written
context-freely as
BYTECODE := (NO_ARG_CODE | ARG_CODE TWO_BYTE_ARG)*

where the symbolic expansions of NO_ARG_CODE and ARG_CODE, with semantic
explanations, constitute the contents of the doc above.

Terry J. Reedy

Jul 19 '05 #9

P: n/a
"Terry Reedy" <tj*****@udel.edu> writes:
"Peter Dembinski" <pd***@gazeta.pl> wrote in message
news:87************@hector.domek...
"Terry Reedy" <tj*****@udel.edu> writes:
I believe the top-level production is something like
BYTECODE := (OPCODE ARGS)*


ROTFL :)


Glad to make your day ;-)


You are welcome.

"Asking stupid questions is the best way to get quick answers" :>
Jul 19 '05 #10

P: n/a
M1st0 wrote:
Ops yes is BNF :P Bacus Normal Form if I am not wrong...


Backus Naur Form.

John Backus and Peter Naur first used it to describe ALGOL around 1960.
See e.g.
http://cui.unige.ch/db-research/Ense.../AboutBNF.html
Jul 19 '05 #11

P: n/a
On Fri, 10 Jun 2005 16:46:32 +0200, "Diez B. Roggisch" <de***@web.de> wrote:
M1st0 wrote:
Ops yes is BNF :P Bacus Normal Form if I am not wrong...

However......

I'am tryng to recognizing patterns in a bytecoded file in orderd to
optimize...

But I would like to "parse" i.e reconstruct it in something like a
tree..
in order to apply rules on a tree recursively.


But bytecode is like assembly - there is no tree-structure. A opcode is
followed by a number of arguments, and oopcodes are in a seqence.

However the bytecode is _generated_ from the AST somehow. Maybe you can
work on that. Also take a look at psyco, it already does optimizations
for numeric calculations.

Besides: I serously doubt you can do much optimization on the
bytecodelevel itself, as it is very highlevel. The optimization efforts
like in psyco don't alter bytecode - they replace it....

Well, Raymond Hettinger wrote a decorator to optimize functions in various ways,
rewriting the byte code, and I wrote another decorator inspired by that.
His is in the recipes at
http://aspn.activestate.com/ASPN/Coo.../Recipe/277940
and mine is posted at
http://groups-beta.google.com/group/...85e89924?hl=en
(I think that's the last version ;-) Mine was to preset local variables without
using the default-argument hack. This led to a currying version of that, which
lets you modify the signature of an existing function by setting a local variable
to a constant and eliminating that name from the signature, so there is one level
of call as before, not the double call level from wrapping that you will get from some
other currying recipes.

I think you can see how byte code mods work from the code, and where the byte code definitions
come from etc. If you want to generate optimized code by replacing current code generation,
you will have to look into AST stuff as Diez mentions above. The sources are all there,
and there is pure python equivalents for the python run-time compiler (which is (all?) in C I think).

Import compiler and parser etc. and poke around. You'll find interesting things ;-)

Regards,
Bengt Richter
Jul 19 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.