PLY is an implementation of lex and yacc parsing tools for Python.
About PLY
PLY is a parser generator tool that uses reflection to read token definitions and production rules written in pure Python. You can, for example, define tokens with a simple string attribution or with methods containing a regular expression in its docstring.
To install PLY on your machine for python2/3, follow the steps outlined below:
- Download the source code from here.
- Unzip the downloaded zip file
- Navigate into the unzipped
ply-3.10
folder - Run the following command in your terminal:
python setup.py install
If you completed all the above, you should now be able to use the PLY module. You can test it out by opening a python interpreter and typing import ply.lex
.
Note: Do not use pip
to install PLY, it will install a broken distribution on your machine.
Examples of token definitions with code to interpret value:
def t_BOOLEAN(token):
r'(?:true|false)'
token.value = token.value == "true"
return token
def t_NUMBER(token):
r'[0-9]+'
token.value = int(token.value)
return token
Project page: http://www.dabeaz.com/ply/
Main docs: http://www.dabeaz.com/ply/ply.html