Group: comp.lang.python


Subject: How does python build its AST
From: Terry Reedy
Date: 12/7/2007 5:29:10 PM
"MonkeeSage" <MonkeeSage@gmail.com> wrote in message news:79c1f3ea-aeeb-4607-b30d-48ad51b52996@x69g2000hsx.googlegroups.com... |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?) The direct answer is that names cannot be entered into namespaces and bound to objects to be looked up until the corresponding object is created by executing the corresponding code. Compiling Python code creates the internal code needed to create Python objects, but only exceptionally creates Python objects in the process. In particular, compiling a function may create code objects (since the code is a constant) referenced by the function creation code, but not function objects themselves. A less direct answer is the Python is designed to by usable interactively. In CPython interactive mode, you enter and the interpreter compiles and executes one top(module)-level statement at a time. Calling a function you have not yet entered would be magical. Terry Jan Reedy

Subject: How does python build its AST
From: Terry Reedy
Date: 12/8/2007 1:20:15 AM
"MonkeeSage" <MonkeeSage@gmail.com> wrote in message news:7e222fec-8b84-49d7-a9bc-113520d7ed91@t47g2000hsc.googlegroups.com... | 1.) What is the benefit of doing a two phase compilation (parsing/ | compiling), rather than a single, joint parse + compile phase (as in | interactive mode)? As far as I know (without looking at the code), there is no difference between interactive and batch mode except that the unit processed is a statement versus file. | 2.) Wouldn't it be possible on the parsing phase to "tag" names as | valid, even if they occur prior to the assignment of the name, if on a | later branch that assignment is found (and have the compiler be aware | of such tags)? What would be the point? The semantics of Python code is essentially independent of whether it is executed in interactive or batch mode. (The exceptions are not relevant to your question.) So there is no point I can see to doing something in file mode that could not be done in statement mode. tjr