Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Parsing a source code with just one digit

Could some please walk me through, how CPython parses a file contain just one character 1?

In particular, why ast.parse("3") returns ...Expr(...)... as (I believe) Python’s source code is a list of statements?

In other words, reading the grammar how do I go from file to … atom (I guess)?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

An expression can also be a statement, the rule sequence is:

file -> statements -> statement -> 
    simple_stmts -> simple_stmt -> star_expressions -> star_expression -> 
    expression

After all, func(x) is also an expression, and a file print('Hello world!') is perfectly valid.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading