How does python build its AST

Kay Schluehr kay.schluehr at gmx.net
Fri Dec 7 15:57:27 EST 2007


On Dec 7, 5:03 pm, MonkeeSage <MonkeeS... at gmail.com> wrote:
> On Dec 7, 9:50 am, Kay Schluehr <kay.schlu... at gmx.net> wrote:
>
>
>
> > On Dec 7, 3:23 pm, MonkeeSage <MonkeeS... at gmail.com> wrote:
>
> > > A quick question about how python parses a file into compiled
> > > bytecode. Does it parse the whole file into AST first and then compile
> > > the AST, or does it build and compile the AST on the fly as it reads
> > > expressions? (If the former case, why can't functions be called before
> > > their definitions?)
>
> > > Thanks,
> > > Jordan
>
> > Python uses a highly optimized table based LL(1) parser to create a
> > syntax tree. In Python 2.5 it transforms the concrete syntax tree
> > ( CST ) into an AST before compilation. Before that it compiled the
> > CST directly. I'm not sure what you are asking for ( in parentheses )?
> > Parser actions or preprocessing the tree? The latter is definitely
> > possible and you can build your own compilation machinery using the
> > parser module and the compile function.
>
> > Kay
>
> Thanks for your reply. You answered my main question. The secondary
> question is why is it a NameError to try to use a variable/function
> prior to the declaration in a source file, since python has already
> seen the declaration on the first pass building the CST/AST? At
> compile time, shouldn't it already know about it? (Forgive my
> ignorance.)
>
> Regards,
> Jordan

When the compiler finds a name not being bound in the function scope
it supposes the name is global and a LOAD_GLOBAL opcode is created
instead of LOAD_FAST ( accessing locals ). Other than the local/
function scope the global ( module level ) scope is dynamic and the
compiler can't know whether a name will be present at runtime.

Kay



More information about the Python-list mailing list