How to process syntax errors

Steve D'Aprano steve+python at pearwood.info
Wed Oct 12 12:35:12 EDT 2016


On Thu, 13 Oct 2016 12:46 am, Pierre-Alain Dorange wrote:

> Terry Reedy <tjreedy at udel.edu> wrote:
> 
>> > Using this function, the code is "compiled".
>> > I do not think this function is often used and most python project
>> > simply use the interpreter (which do a small translation into byte-code
>> > to be faster and check syntax error before running interpretation
>> 
>> You seem to be confusing CPython with, for instance, simple BASIC
>> interpreters that tokenized the code, translated keywords to function
>> numbers, and did other 'small translations' before execution.
>> 
>> The CPython compiler lexes (tokenizes), ll(1) parses to a syntax tree,
>> does some analysis and transformation of the tree, and translates it to
>> the bytecode for an stack machine.  All done using standard compiler
>> theory.
> 
> Yes i'm probably confusing things ; i've not explore Python
> implentation, i'm just an amateur developer.
> But what confuse me, is that Python require "real live" interpratation
> of the code to work properly 

That is true, but not as much as you would think. Only two things in Python
absolutely require runtime interpretation: eval() and exec().

But that depends on what you mean by "real live" interpretation. Compiled
languages like Java and C++ also include runtime features like runtime
dispatch, V-tables, 

> (or perhaps i also confuse on that but 
> Python rely on interpretation of the code to conform to its own
> standard, ie variables can change type during execution...)

Variables changing type is the least important part of this.

The Rust programming language is strongly typed, compiled to efficient
machine code like C, and it allows you to change variable types. Say you
have code like this in Rust:

let x = 1;
f(x);
let x = 'this is a string';
g(x);

The Rust compiler is clever enough to tell that when you call f(), x is an
integer, and when you call f(), x is a string. It can do that at compile
time, not just runtime.

If you can read a piece of source code, and work out what type a variable
has, then so can the compiler -- if it is smart enough. The type systems of
older languages like Pascal and C are not very smart, so they are very
restrictive: they insist that variables have only a single type, which must
be explicitly declared. But modern languages like Rust have very smart
compilers with a very powerful type system, and they don't have that
restriction.

 

-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list