4

I have a really simple question. You know the formal grammar for Python on this link:

https://docs.python.org/3.4/reference/grammar.html

It says this is the grammar as it's given to the parser generator.

Which generator does Python use? Also, does it output Python code?

vaultah
  • 44,105
  • 12
  • 114
  • 143
Katie2423
  • 121
  • 7
  • 1
    Assuming you're asking about standard CPython, it's a custom parser, according to [this answer](http://stackoverflow.com/a/10748707/4014959). You can see the source [here](http://svn.python.org/projects/python/trunk/Modules/parsermodule.c). – PM 2Ring Nov 13 '15 at 15:14
  • Sorry I'm a noob. What's the difference between CPython and others? Is it just a python interpreter written in C? – Katie2423 Nov 13 '15 at 15:17
  • 1
    CPython is the standard Python implementation: it's what people usually mean when they talk about Python without further qualification. Yes, it's written in C, and some of the standard modules are also written in C, the rest are written in Python. There are other Python implementations written in a variety of languages, eg there's Jython, which is a Python implementation written in Java. – PM 2Ring Nov 13 '15 at 15:21
  • May I ask any popular parser generator that outputs python code? – Katie2423 Nov 13 '15 at 15:25
  • 1
    The standard library contains the [ast module](https://docs.python.org/3/library/ast.html), which can be used to parse Python code and produce an Abstract Syntax Tree. Some more elaborate documentation of this module can be found [here](https://greentreesnakes.readthedocs.org/en/latest/index.html) – titusjan Nov 13 '15 at 15:50

1 Answers1

1

If you want to parse Python code using Python you can use the https://docs.python.org/3/library/ast.html -- Abstract Syntax Tree module.

sorin
  • 161,544
  • 178
  • 535
  • 806