2

For example::

>>> import ast
>>> print(type(ast.parse('1.2', mode='eval').body.n)
float

How do I let the parser convert a python source file into a syntax tree, while preserving the original values of nodes in str type? Because I need to convert for example '1.2' into exact values using fractions as precise as possible, without loosing any precision at all (the value 1.2 cannot be precisely represented in floating-point format).

Preferably I wish to do this without reimplementing the parser. Perhaps there are other parsers more suitable for this than the ast module.

BTW, I need to parse not only expressions but programs.

Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
koo
  • 2,888
  • 1
  • 23
  • 29
  • Parsing Python source code involves parsing literals into the appropriate types. Can you use `'"1.2"'` instead and just treat the value as a string? – BrenBarn Oct 17 '13 at 20:11
  • 1
    Preferably I would keep the value `1.2` instead of having to add quotes to every single value. – koo Oct 17 '13 at 20:15
  • What are `mpfr` and `mpq` and how are they relevant to this question when they are mentioned nowhere in the title, body or comments? – Charles Oct 18 '13 at 04:21
  • @Charles it is now `multiprecision` to make my question clearer. – koo Oct 18 '13 at 13:04
  • Have you seen the `parser` [module](http://docs.python.org/2/library/parser.html)? A bit awkward though. BTW, it sounds like you're looking for a concrete syntax tree, which, contrary to what BrenBarn said, does not necessarily involve creating literals of the appropriate types. – Matt Fenwick Oct 18 '13 at 13:29

2 Answers2

0

The most advanced tool to work with AST that I know of is the former astng project that backed up pylint and similar tools from logilab. Now it is called Astroid and available from https://bitbucket.org/logilab/astroid/ If it not won't help, then probably nothing will do.

anatoly techtonik
  • 19,847
  • 9
  • 124
  • 140
0

LibCST is a Python Concrete Syntax tree parser and toolkit which can be used to solve your problem. It provides a syntax tree looks like ast and preserve floating format as string. https://github.com/Instagram/LibCST/

https://libcst.readthedocs.io/en/latest/index.html

Here are some examples:

In [1]: import libcst as cst

In [2]: cst.parse_expression("1.2")
Out[2]:
Float(
    value='1.2',
    lpar=[],
    rpar=[],
)

In [3]: cst.parse_expression("1.2").value
Out[3]: '1.2'

In [4]: cst.parse_expression("5e-2").value
Out[4]: '5e-2'
Lai Jimmy
  • 256
  • 3
  • 4