473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

python bytecode grammar

where I can find the grammar of python bytecode ? ( better if is in BCF
).

Jul 19 '05 #1
11 2482
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
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
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
Ok I tried :).

Jul 19 '05 #5
"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

"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
"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

"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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: Maurice LING | last post by:
Hi, I remembered reading a MSc thesis about compiling Perl to Java bytecodes (as in java class files). At least, it seems that someone had compiled scheme to java class files quite successfully....
114
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
14
by: Mark Dufour | last post by:
After nine months of hard work, I am proud to introduce my baby to the world: an experimental Python-to-C++ compiler. It can convert many Python programs into optimized C++ code, without any user...
5
by: xkenneth | last post by:
Hi All, I'll shortly be distributing a number of python applications that use proprietary. The software is part of a much larger system and it will need to be distributed securely. How can i...
53
by: Vicent Giner | last post by:
Hello. I am new to Python. It seems a very interesting language to me. Its simplicity is very attractive. However, it is usually said that Python is not a compiled but interpreted programming...
0
by: Robert Kern | last post by:
Ognjen Bezanov wrote: Mostly. It is worth noting, though, that the Python bytecode is at a higher level than Java bytecode. You can run other languages on top of the JVM using Java bytecode...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.