1 PLY (Python Lex-Yacc) Version 2.3 (February 18, 2007)
3 David M. Beazley (dave@dabeaz.com)
5 Copyright (C) 2001-2007 David M. Beazley
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 See the file COPYING for a complete copy of the LGPL.
26 PLY is a 100% Python implementation of the common parsing tools lex
27 and yacc. Although several other parsing tools are available for
28 Python, there are several reasons why you might want to consider PLY:
30 - The tools are very closely modeled after traditional lex/yacc.
31 If you know how to use these tools in C, you will find PLY
34 - PLY provides *very* extensive error reporting and diagnostic
35 information to assist in parser construction. The original
36 implementation was developed for instructional purposes. As
37 a result, the system tries to identify the most common types
38 of errors made by novice users.
40 - PLY provides full support for empty productions, error recovery,
41 precedence specifiers, and moderately ambiguous grammars.
43 - Parsing is based on LR-parsing which is fast, memory efficient,
44 better suited to large grammars, and which has a number of nice
45 properties when dealing with syntax errors and other parsing problems.
46 Currently, PLY builds its parsing tables using the SLR algorithm which
47 is slightly weaker than LALR(1) used in traditional yacc.
49 - PLY uses Python introspection features to build lexers and parsers.
50 This greatly simplifies the task of parser construction since it reduces
51 the number of files and eliminates the need to run a separate lex/yacc
52 tool before running your program.
54 - PLY can be used to build parsers for "real" programming languages.
55 Although it is not ultra-fast due to its Python implementation,
56 PLY can be used to parse grammars consisting of several hundred
57 rules (as might be found for a language like C). The lexer and LR
58 parser are also reasonably efficient when parsing typically
61 The original version of PLY was developed for an Introduction to
62 Compilers course where students used it to build a compiler for a
63 simple Pascal-like language. Their compiler had to include lexical
64 analysis, parsing, type checking, type inference, and generation of
65 assembly code for the SPARC processor. Because of this, the current
66 implementation has been extensively tested and debugged. In addition,
67 most of the API and error checking steps have been adapted to address
68 common usability problems.
73 PLY consists of two files : lex.py and yacc.py. These are contained
74 within the 'ply' directory which may also be used as a Python package.
75 To use PLY, simply copy the 'ply' directory to your project and import
76 lex and yacc from the associated 'ply' package. For example:
79 import ply.yacc as yacc
81 Alternatively, you can copy just the files lex.py and yacc.py
82 individually and use them as modules. For example:
87 The file setup.py can be used to install ply using distutils.
89 The file doc/ply.html contains complete documentation on how to use
92 The example directory contains several different examples including a
93 PLY specification for ANSI C as given in K&R 2nd Ed.
95 A simple example is found at the end of this document
99 PLY requires the use of Python 2.1 or greater. However, you should
100 use the latest Python release if possible. It should work on just
101 about any platform. PLY has been tested with both CPython and Jython.
102 However, it does not seem to work with IronPython.
106 More information about PLY can be obtained on the PLY webpage at:
108 http://www.dabeaz.com/ply
110 For a detailed overview of parsing theory, consult the excellent
111 book "Compilers : Principles, Techniques, and Tools" by Aho, Sethi, and
112 Ullman. The topics found in "Lex & Yacc" by Levine, Mason, and Brown
115 A Google group for PLY can be found at
117 http://groups.google.com/group/ply-hack
121 A special thanks is in order for all of the students in CS326 who
122 suffered through about 25 different versions of these tools :-).
124 The CHANGES file acknowledges those who have contributed patches.
126 Elias Ioup did the first implementation of LALR(1) parsing in PLY-1.x.
127 Andrew Waters and Markus Schoepflin were instrumental in reporting bugs
128 and testing a revised LALR(1) implementation for PLY-2.0.
130 Special Note for PLY-2.x
131 ========================
132 PLY-2.0 is the first in a series of PLY releases that will be adding a
133 variety of significant new features. The first release in this series
134 (Ply-2.0) should be 100% compatible with all previous Ply-1.x releases
135 except for the fact that Ply-2.0 features a correct implementation of
136 LALR(1) table generation.
138 If you have suggestions for improving PLY in future 2.x releases, please
144 Here is a simple example showing a PLY implementation of a calculator
147 # -----------------------------------------------------------------------------
150 # A simple calculator with variables.
151 # -----------------------------------------------------------------------------
155 'PLUS','MINUS','TIMES','DIVIDE','EQUALS',
168 t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'
173 t.value = int(t.value)
175 print "Integer value too large", t.value
184 t.lexer.lineno += t.value.count("\n")
187 print "Illegal character '%s'" % t.value[0]
191 import ply.lex as lex
194 # Precedence rules for the arithmetic operators
196 ('left','PLUS','MINUS'),
197 ('left','TIMES','DIVIDE'),
201 # dictionary of names (for storing variables)
204 def p_statement_assign(p):
205 'statement : NAME EQUALS expression'
208 def p_statement_expr(p):
209 'statement : expression'
212 def p_expression_binop(p):
213 '''expression : expression PLUS expression
214 | expression MINUS expression
215 | expression TIMES expression
216 | expression DIVIDE expression'''
217 if p[2] == '+' : p[0] = p[1] + p[3]
218 elif p[2] == '-': p[0] = p[1] - p[3]
219 elif p[2] == '*': p[0] = p[1] * p[3]
220 elif p[2] == '/': p[0] = p[1] / p[3]
222 def p_expression_uminus(p):
223 'expression : MINUS expression %prec UMINUS'
226 def p_expression_group(p):
227 'expression : LPAREN expression RPAREN'
230 def p_expression_number(p):
231 'expression : NUMBER'
234 def p_expression_name(p):
239 print "Undefined name '%s'" % p[1]
243 print "Syntax error at '%s'" % p.value
245 import ply.yacc as yacc
250 s = raw_input('calc > ')
256 Bug Reports and Patches
257 =======================
258 Because of the extremely specialized and advanced nature of PLY, I
259 rarely spend much time working on it unless I receive very specific
260 bug-reports and/or patches to fix problems. I also try to incorporate
261 submitted feature requests and enhancements into each new version. To
262 contact me about bugs and/or new features, please send email to
265 In addition there is a Google group for discussing PLY related issues at
267 http://groups.google.com/group/ply-hack