OLD | NEW |
(Empty) | |
| 1 PLY (Python Lex-Yacc) Version 3.1 |
| 2 |
| 3 David M. Beazley (dave@dabeaz.com) |
| 4 |
| 5 Copyright (C) 2001-2009 David M. Beazley |
| 6 |
| 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. |
| 11 |
| 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. |
| 16 |
| 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 |
| 20 |
| 21 See the file COPYING for a complete copy of the LGPL. |
| 22 |
| 23 Introduction |
| 24 ============ |
| 25 |
| 26 PLY is a 100% Python implementation of the common parsing tools lex |
| 27 and yacc. Here are a few highlights: |
| 28 |
| 29 - PLY is very closely modeled after traditional lex/yacc. |
| 30 If you know how to use these tools in C, you will find PLY |
| 31 to be similar. |
| 32 |
| 33 - PLY provides *very* extensive error reporting and diagnostic |
| 34 information to assist in parser construction. The original |
| 35 implementation was developed for instructional purposes. As |
| 36 a result, the system tries to identify the most common types |
| 37 of errors made by novice users. |
| 38 |
| 39 - PLY provides full support for empty productions, error recovery, |
| 40 precedence specifiers, and moderately ambiguous grammars. |
| 41 |
| 42 - Parsing is based on LR-parsing which is fast, memory efficient, |
| 43 better suited to large grammars, and which has a number of nice |
| 44 properties when dealing with syntax errors and other parsing problems. |
| 45 Currently, PLY builds its parsing tables using the LALR(1) |
| 46 algorithm used in yacc. |
| 47 |
| 48 - PLY uses Python introspection features to build lexers and parsers. |
| 49 This greatly simplifies the task of parser construction since it reduces |
| 50 the number of files and eliminates the need to run a separate lex/yacc |
| 51 tool before running your program. |
| 52 |
| 53 - PLY can be used to build parsers for "real" programming languages. |
| 54 Although it is not ultra-fast due to its Python implementation, |
| 55 PLY can be used to parse grammars consisting of several hundred |
| 56 rules (as might be found for a language like C). The lexer and LR |
| 57 parser are also reasonably efficient when parsing typically |
| 58 sized programs. People have used PLY to build parsers for |
| 59 C, C++, ADA, and other real programming languages. |
| 60 |
| 61 How to Use |
| 62 ========== |
| 63 |
| 64 PLY consists of two files : lex.py and yacc.py. These are contained |
| 65 within the 'ply' directory which may also be used as a Python package. |
| 66 To use PLY, simply copy the 'ply' directory to your project and import |
| 67 lex and yacc from the associated 'ply' package. For example: |
| 68 |
| 69 import ply.lex as lex |
| 70 import ply.yacc as yacc |
| 71 |
| 72 Alternatively, you can copy just the files lex.py and yacc.py |
| 73 individually and use them as modules. For example: |
| 74 |
| 75 import lex |
| 76 import yacc |
| 77 |
| 78 The file setup.py can be used to install ply using distutils. |
| 79 |
| 80 The file doc/ply.html contains complete documentation on how to use |
| 81 the system. |
| 82 |
| 83 The example directory contains several different examples including a |
| 84 PLY specification for ANSI C as given in K&R 2nd Ed. |
| 85 |
| 86 A simple example is found at the end of this document |
| 87 |
| 88 Requirements |
| 89 ============ |
| 90 PLY requires the use of Python 2.2 or greater. However, you should |
| 91 use the latest Python release if possible. It should work on just |
| 92 about any platform. PLY has been tested with both CPython and Jython. |
| 93 It also seems to work with IronPython. |
| 94 |
| 95 Resources |
| 96 ========= |
| 97 More information about PLY can be obtained on the PLY webpage at: |
| 98 |
| 99 http://www.dabeaz.com/ply |
| 100 |
| 101 For a detailed overview of parsing theory, consult the excellent |
| 102 book "Compilers : Principles, Techniques, and Tools" by Aho, Sethi, and |
| 103 Ullman. The topics found in "Lex & Yacc" by Levine, Mason, and Brown |
| 104 may also be useful. |
| 105 |
| 106 A Google group for PLY can be found at |
| 107 |
| 108 http://groups.google.com/group/ply-hack |
| 109 |
| 110 Acknowledgments |
| 111 =============== |
| 112 A special thanks is in order for all of the students in CS326 who |
| 113 suffered through about 25 different versions of these tools :-). |
| 114 |
| 115 The CHANGES file acknowledges those who have contributed patches. |
| 116 |
| 117 Elias Ioup did the first implementation of LALR(1) parsing in PLY-1.x. |
| 118 Andrew Waters and Markus Schoepflin were instrumental in reporting bugs |
| 119 and testing a revised LALR(1) implementation for PLY-2.0. |
| 120 |
| 121 Special Note for PLY-3.0 |
| 122 ======================== |
| 123 PLY-3.0 the first PLY release to support Python 3. However, backwards |
| 124 compatibility with Python 2.2 is still preserved. PLY provides dual |
| 125 Python 2/3 compatibility by restricting its implementation to a common |
| 126 subset of basic language features. You should not convert PLY using |
| 127 2to3--it is not necessary and may in fact break the implementation. |
| 128 |
| 129 Example |
| 130 ======= |
| 131 |
| 132 Here is a simple example showing a PLY implementation of a calculator |
| 133 with variables. |
| 134 |
| 135 # ----------------------------------------------------------------------------- |
| 136 # calc.py |
| 137 # |
| 138 # A simple calculator with variables. |
| 139 # ----------------------------------------------------------------------------- |
| 140 |
| 141 tokens = ( |
| 142 'NAME','NUMBER', |
| 143 'PLUS','MINUS','TIMES','DIVIDE','EQUALS', |
| 144 'LPAREN','RPAREN', |
| 145 ) |
| 146 |
| 147 # Tokens |
| 148 |
| 149 t_PLUS = r'\+' |
| 150 t_MINUS = r'-' |
| 151 t_TIMES = r'\*' |
| 152 t_DIVIDE = r'/' |
| 153 t_EQUALS = r'=' |
| 154 t_LPAREN = r'\(' |
| 155 t_RPAREN = r'\)' |
| 156 t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*' |
| 157 |
| 158 def t_NUMBER(t): |
| 159 r'\d+' |
| 160 t.value = int(t.value) |
| 161 return t |
| 162 |
| 163 # Ignored characters |
| 164 t_ignore = " \t" |
| 165 |
| 166 def t_newline(t): |
| 167 r'\n+' |
| 168 t.lexer.lineno += t.value.count("\n") |
| 169 |
| 170 def t_error(t): |
| 171 print "Illegal character '%s'" % t.value[0] |
| 172 t.lexer.skip(1) |
| 173 |
| 174 # Build the lexer |
| 175 import ply.lex as lex |
| 176 lex.lex() |
| 177 |
| 178 # Precedence rules for the arithmetic operators |
| 179 precedence = ( |
| 180 ('left','PLUS','MINUS'), |
| 181 ('left','TIMES','DIVIDE'), |
| 182 ('right','UMINUS'), |
| 183 ) |
| 184 |
| 185 # dictionary of names (for storing variables) |
| 186 names = { } |
| 187 |
| 188 def p_statement_assign(p): |
| 189 'statement : NAME EQUALS expression' |
| 190 names[p[1]] = p[3] |
| 191 |
| 192 def p_statement_expr(p): |
| 193 'statement : expression' |
| 194 print p[1] |
| 195 |
| 196 def p_expression_binop(p): |
| 197 '''expression : expression PLUS expression |
| 198 | expression MINUS expression |
| 199 | expression TIMES expression |
| 200 | expression DIVIDE expression''' |
| 201 if p[2] == '+' : p[0] = p[1] + p[3] |
| 202 elif p[2] == '-': p[0] = p[1] - p[3] |
| 203 elif p[2] == '*': p[0] = p[1] * p[3] |
| 204 elif p[2] == '/': p[0] = p[1] / p[3] |
| 205 |
| 206 def p_expression_uminus(p): |
| 207 'expression : MINUS expression %prec UMINUS' |
| 208 p[0] = -p[2] |
| 209 |
| 210 def p_expression_group(p): |
| 211 'expression : LPAREN expression RPAREN' |
| 212 p[0] = p[2] |
| 213 |
| 214 def p_expression_number(p): |
| 215 'expression : NUMBER' |
| 216 p[0] = p[1] |
| 217 |
| 218 def p_expression_name(p): |
| 219 'expression : NAME' |
| 220 try: |
| 221 p[0] = names[p[1]] |
| 222 except LookupError: |
| 223 print "Undefined name '%s'" % p[1] |
| 224 p[0] = 0 |
| 225 |
| 226 def p_error(p): |
| 227 print "Syntax error at '%s'" % p.value |
| 228 |
| 229 import ply.yacc as yacc |
| 230 yacc.yacc() |
| 231 |
| 232 while 1: |
| 233 try: |
| 234 s = raw_input('calc > ') |
| 235 except EOFError: |
| 236 break |
| 237 yacc.parse(s) |
| 238 |
| 239 |
| 240 Bug Reports and Patches |
| 241 ======================= |
| 242 Because of the extremely specialized and advanced nature of PLY, I |
| 243 rarely spend much time working on it unless I receive very specific |
| 244 bug-reports and/or patches to fix problems. I also try to incorporate |
| 245 submitted feature requests and enhancements into each new version. To |
| 246 contact me about bugs and/or new features, please send email to |
| 247 dave@dabeaz.com. |
| 248 |
| 249 In addition there is a Google group for discussing PLY related issues at |
| 250 |
| 251 http://groups.google.com/group/ply-hack |
| 252 |
| 253 -- Dave |
| 254 |
| 255 |
| 256 |
| 257 |
| 258 |
| 259 |
| 260 |
| 261 |
| 262 |
OLD | NEW |