473,394 Members | 1,813 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,394 software developers and data experts.

using Python's AST generator for other languages

Hello,

I'm building a parser in Python, and while pondering on the design of
my ASTs had the idea to see what Python uses. I quickly got to the
compiler.ast module, and understood it's automatically generated. So I
went to the source, ast.txt and tools/compiler/astgen.py, where I was
this unexpected message:

"""Generate ast module from specification

This script generates the ast module from a simple specification,
which makes it easy to accomodate changes in the grammar. This
approach would be quite reasonable if the grammar changed often.
Instead, it is rather complex to generate the appropriate code. And
the Node interface has changed more often than the grammar.
"""

Now, to me the design of the AST in Python looks quite elegant,
especially from the point of view of the AST's user (using Visitors to
walk the AST). And astgen.py looks like a nice approach to generate
tons of boilerplate code.
So, my questions:
1) Is the compiler.ast module really employed during the compilation
of Python into .pyc files ?
2) What is the meaning of the comment in astgen.py ? Are the Python
maintainers unhappy with the design of the AST ?
3) What other approach would be recommended to generate a very
detailed AST hierarchy, if the one in astgen.py is dissapointing ?

Thanks in advance
Eli
Jul 10 '08 #1
3 2481
On Jul 10, 12:40 pm, eliben <eli...@gmail.comwrote:
Hello,

I'm building a parser in Python, and while pondering on the design of
my ASTs had the idea to see what Python uses. I quickly got to the
compiler.ast module, and understood it's automatically generated. So I
went to the source, ast.txt and tools/compiler/astgen.py, where I was
this unexpected message:

"""Generate ast module from specification

This script generates the ast module from a simple specification,
which makes it easy to accomodate changes in the grammar. This
approach would be quite reasonable if the grammar changed often.
Instead, it is rather complex to generate the appropriate code. And
the Node interface has changed more often than the grammar.
"""

Now, to me the design of the AST in Python looks quite elegant,
especially from the point of view of the AST's user (using Visitors to
walk the AST). And astgen.py looks like a nice approach to generate
tons of boilerplate code.
So, my questions:
1) Is the compiler.ast module really employed during the compilation
of Python into .pyc files ?
No, the comment refers to the builtin _ast module. The compiler
package is a compiler for Python written in Python.
2) What is the meaning of the comment in astgen.py ? Are the Python
maintainers unhappy with the design of the AST ?3
Node, I think, is talking about a node in the parse tree. (AST is
generated from another parse tree.) See PEP 339 for details.
3) What other approach would be recommended to generate a very
detailed AST hierarchy, if the one in astgen.py is dissapointing ?
astgen.py contains things that are specific to writing Python's AST C
code. Have a look at spark.py in the Parser dir. It is what astgen.py
is based on.
>
Thanks in advance
Eli
Jul 10 '08 #2
<snip>
2) What is the meaning of the comment in astgen.py ? Are the Python
maintainers unhappy with the design of the AST ?3

Node, I think, is talking about a node in the parse tree. (AST is
generated from another parse tree.) See PEP 339 for details.
<snip>

Thanks, PEP 339 clarified a lot to me. I wonder, though, at the need
for two Python compilation frameworks in the same code base. While
CPython uses the flow described in PEP 339 (parsing to an AST
generated from ASDL), the compiler module of the standard library
takes a different approach, with a custom AST description syntax in
ast.txt
Why aren't the two methods unified. I.e. can't the compiler.ast module
be also generated from ASDL, and provide a more unified interface to
the real thing ?

Eli
Jul 11 '08 #3
On Jul 11, 12:27 am, eliben <eli...@gmail.comwrote:
<snip2) What is the meaning of the comment in astgen.py ? Are the Python
maintainers unhappy with the design of the AST ?3
Node, I think, is talking about a node in the parse tree. (AST is
generated from another parse tree.) See PEP 339 for details.

<snip>

Thanks, PEP 339 clarified a lot to me. I wonder, though, at the need
for two Python compilation frameworks in the same code base. While
CPython uses the flow described in PEP 339 (parsing to an AST
generated from ASDL), the compiler module of the standard library
takes a different approach, with a custom AST description syntax in
ast.txt
Why aren't the two methods unified. I.e. can't the compiler.ast module
be also generated from ASDL, and provide a more unified interface to
the real thing ?
You are correct on all points and this is one of the main reasons that
the compiler package is going away in 3.0.
>
Eli
Jul 11 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
4
by: The_Incubator | last post by:
As the subject suggests, I am interested in using Python as a scripting language for a game that is primarily implemented in C++, and I am also interested in using generators in those scripts... ...
14
by: Viktor Rosenfeld | last post by:
Hi, I need to create a parser for a Python project, and I'd like to use process kinda like lex/yacc. I've looked at various parsing packages online, but didn't find anything useful for me: -...
19
by: Leif K-Brooks | last post by:
Has anyone ever tried implementing a simple unstructured BASIC dialect in Python? I'm getting interested in language implementation, and looking at a reasonably simple example like that could be...
40
by: Douglas Alan | last post by:
While writing a generator, I was just thinking how Python needs a "yield_all" statement. With the help of Google, I found a pre-existing discussion on this from a while back in the Lightweight...
3
by: Michael Sparks | last post by:
Hi, I'm posting a link to this since I hope it's of interest to people here :) I've written up the talk I gave at ACCU Python UK on the Kamaelia Framework, and it's been published as a BBC...
53
by: Krystian | last post by:
Hi are there any future perspectives for Python to be as fast as java? i would like to use Python as a language for writing games. best regards krystian
72
by: Gregory Petrosyan | last post by:
Please visit http://www.python.org/peps/pep-0204.html first. As you can see, PEP 204 was rejected, mostly because of not-so-obvious syntax. But IMO the idea behind this pep is very nice. So,...
30
by: Ivan Reborin | last post by:
Hello everyone, I was wondering if anyone here has a moment of time to help me with 2 things that have been bugging me. 1. Multi dimensional arrays - how do you load them in python For...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.