CHANGES 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. Version 3.9
  2. ---------------------
  3. 08/30/16: beazley
  4. Exposed the parser state number as the parser.state attribute
  5. in productions and error functions. For example:
  6. def p_somerule(p):
  7. '''
  8. rule : A B C
  9. '''
  10. print('State:', p.parser.state)
  11. May address issue #65 (publish current state in error callback).
  12. 08/30/16: beazley
  13. Fixed Issue #88. Python3 compatibility with ply/cpp.
  14. 08/30/16: beazley
  15. Fixed Issue #93. Ply can crash if SyntaxError is raised inside
  16. a production. Not actually sure if the original implementation
  17. worked as documented at all. Yacc has been modified to follow
  18. the spec as outlined in the CHANGES noted for 11/27/07 below.
  19. 08/30/16: beazley
  20. Fixed Issue #97. Failure with code validation when the original
  21. source files aren't present. Validation step now ignores
  22. the missing file.
  23. 08/30/16: beazley
  24. Minor fixes to version numbers.
  25. Version 3.8
  26. ---------------------
  27. 10/02/15: beazley
  28. Fixed issues related to Python 3.5. Patch contributed by Barry Warsaw.
  29. Version 3.7
  30. ---------------------
  31. 08/25/15: beazley
  32. Fixed problems when reading table files from pickled data.
  33. 05/07/15: beazley
  34. Fixed regression in handling of table modules if specified as module
  35. objects. See https://github.com/dabeaz/ply/issues/63
  36. Version 3.6
  37. ---------------------
  38. 04/25/15: beazley
  39. If PLY is unable to create the 'parser.out' or 'parsetab.py' files due
  40. to permission issues, it now just issues a warning message and
  41. continues to operate. This could happen if a module using PLY
  42. is installed in a funny way where tables have to be regenerated, but
  43. for whatever reason, the user doesn't have write permission on
  44. the directory where PLY wants to put them.
  45. 04/24/15: beazley
  46. Fixed some issues related to use of packages and table file
  47. modules. Just to emphasize, PLY now generates its special
  48. files such as 'parsetab.py' and 'lextab.py' in the *SAME*
  49. directory as the source file that uses lex() and yacc().
  50. If for some reason, you want to change the name of the table
  51. module, use the tabmodule and lextab options:
  52. lexer = lex.lex(lextab='spamlextab')
  53. parser = yacc.yacc(tabmodule='spamparsetab')
  54. If you specify a simple name as shown, the module will still be
  55. created in the same directory as the file invoking lex() or yacc().
  56. If you want the table files to be placed into a different package,
  57. then give a fully qualified package name. For example:
  58. lexer = lex.lex(lextab='pkgname.files.lextab')
  59. parser = yacc.yacc(tabmodule='pkgname.files.parsetab')
  60. For this to work, 'pkgname.files' must already exist as a valid
  61. Python package (i.e., the directories must already exist and be
  62. set up with the proper __init__.py files, etc.).
  63. Version 3.5
  64. ---------------------
  65. 04/21/15: beazley
  66. Added support for defaulted_states in the parser. A
  67. defaulted_state is a state where the only legal action is a
  68. reduction of a single grammar rule across all valid input
  69. tokens. For such states, the rule is reduced and the
  70. reading of the next lookahead token is delayed until it is
  71. actually needed at a later point in time.
  72. This delay in consuming the next lookahead token is a
  73. potentially important feature in advanced parsing
  74. applications that require tight interaction between the
  75. lexer and the parser. For example, a grammar rule change
  76. modify the lexer state upon reduction and have such changes
  77. take effect before the next input token is read.
  78. *** POTENTIAL INCOMPATIBILITY ***
  79. One potential danger of defaulted_states is that syntax
  80. errors might be deferred to a a later point of processing
  81. than where they were detected in past versions of PLY.
  82. Thus, it's possible that your error handling could change
  83. slightly on the same inputs. defaulted_states do not change
  84. the overall parsing of the input (i.e., the same grammar is
  85. accepted).
  86. If for some reason, you need to disable defaulted states,
  87. you can do this:
  88. parser = yacc.yacc()
  89. parser.defaulted_states = {}
  90. 04/21/15: beazley
  91. Fixed debug logging in the parser. It wasn't properly reporting goto states
  92. on grammar rule reductions.
  93. 04/20/15: beazley
  94. Added actions to be defined to character literals (Issue #32). For example:
  95. literals = [ '{', '}' ]
  96. def t_lbrace(t):
  97. r'\{'
  98. # Some action
  99. t.type = '{'
  100. return t
  101. def t_rbrace(t):
  102. r'\}'
  103. # Some action
  104. t.type = '}'
  105. return t
  106. 04/19/15: beazley
  107. Import of the 'parsetab.py' file is now constrained to only consider the
  108. directory specified by the outputdir argument to yacc(). If not supplied,
  109. the import will only consider the directory in which the grammar is defined.
  110. This should greatly reduce problems with the wrong parsetab.py file being
  111. imported by mistake. For example, if it's found somewhere else on the path
  112. by accident.
  113. *** POTENTIAL INCOMPATIBILITY *** It's possible that this might break some
  114. packaging/deployment setup if PLY was instructed to place its parsetab.py
  115. in a different location. You'll have to specify a proper outputdir= argument
  116. to yacc() to fix this if needed.
  117. 04/19/15: beazley
  118. Changed default output directory to be the same as that in which the
  119. yacc grammar is defined. If your grammar is in a file 'calc.py',
  120. then the parsetab.py and parser.out files should be generated in the
  121. same directory as that file. The destination directory can be changed
  122. using the outputdir= argument to yacc().
  123. 04/19/15: beazley
  124. Changed the parsetab.py file signature slightly so that the parsetab won't
  125. regenerate if created on a different major version of Python (ie., a
  126. parsetab created on Python 2 will work with Python 3).
  127. 04/16/15: beazley
  128. Fixed Issue #44 call_errorfunc() should return the result of errorfunc()
  129. 04/16/15: beazley
  130. Support for versions of Python <2.7 is officially dropped. PLY may work, but
  131. the unit tests requires Python 2.7 or newer.
  132. 04/16/15: beazley
  133. Fixed bug related to calling yacc(start=...). PLY wasn't regenerating the
  134. table file correctly for this case.
  135. 04/16/15: beazley
  136. Added skipped tests for PyPy and Java. Related to use of Python's -O option.
  137. 05/29/13: beazley
  138. Added filter to make unit tests pass under 'python -3'.
  139. Reported by Neil Muller.
  140. 05/29/13: beazley
  141. Fixed CPP_INTEGER regex in ply/cpp.py (Issue 21).
  142. Reported by @vbraun.
  143. 05/29/13: beazley
  144. Fixed yacc validation bugs when from __future__ import unicode_literals
  145. is being used. Reported by Kenn Knowles.
  146. 05/29/13: beazley
  147. Added support for Travis-CI. Contributed by Kenn Knowles.
  148. 05/29/13: beazley
  149. Added a .gitignore file. Suggested by Kenn Knowles.
  150. 05/29/13: beazley
  151. Fixed validation problems for source files that include a
  152. different source code encoding specifier. Fix relies on
  153. the inspect module. Should work on Python 2.6 and newer.
  154. Not sure about older versions of Python.
  155. Contributed by Michael Droettboom
  156. 05/21/13: beazley
  157. Fixed unit tests for yacc to eliminate random failures due to dict hash value
  158. randomization in Python 3.3
  159. Reported by Arfrever
  160. 10/15/12: beazley
  161. Fixed comment whitespace processing bugs in ply/cpp.py.
  162. Reported by Alexei Pososin.
  163. 10/15/12: beazley
  164. Fixed token names in ply/ctokens.py to match rule names.
  165. Reported by Alexei Pososin.
  166. 04/26/12: beazley
  167. Changes to functions available in panic mode error recover. In previous versions
  168. of PLY, the following global functions were available for use in the p_error() rule:
  169. yacc.errok() # Reset error state
  170. yacc.token() # Get the next token
  171. yacc.restart() # Reset the parsing stack
  172. The use of global variables was problematic for code involving multiple parsers
  173. and frankly was a poor design overall. These functions have been moved to methods
  174. of the parser instance created by the yacc() function. You should write code like
  175. this:
  176. def p_error(p):
  177. ...
  178. parser.errok()
  179. parser = yacc.yacc()
  180. *** POTENTIAL INCOMPATIBILITY *** The original global functions now issue a
  181. DeprecationWarning.
  182. 04/19/12: beazley
  183. Fixed some problems with line and position tracking and the use of error
  184. symbols. If you have a grammar rule involving an error rule like this:
  185. def p_assignment_bad(p):
  186. '''assignment : location EQUALS error SEMI'''
  187. ...
  188. You can now do line and position tracking on the error token. For example:
  189. def p_assignment_bad(p):
  190. '''assignment : location EQUALS error SEMI'''
  191. start_line = p.lineno(3)
  192. start_pos = p.lexpos(3)
  193. If the trackng=True option is supplied to parse(), you can additionally get
  194. spans:
  195. def p_assignment_bad(p):
  196. '''assignment : location EQUALS error SEMI'''
  197. start_line, end_line = p.linespan(3)
  198. start_pos, end_pos = p.lexspan(3)
  199. Note that error handling is still a hairy thing in PLY. This won't work
  200. unless your lexer is providing accurate information. Please report bugs.
  201. Suggested by a bug reported by Davis Herring.
  202. 04/18/12: beazley
  203. Change to doc string handling in lex module. Regex patterns are now first
  204. pulled from a function's .regex attribute. If that doesn't exist, then
  205. .doc is checked as a fallback. The @TOKEN decorator now sets the .regex
  206. attribute of a function instead of its doc string.
  207. Changed suggested by Kristoffer Ellersgaard Koch.
  208. 04/18/12: beazley
  209. Fixed issue #1: Fixed _tabversion. It should use __tabversion__ instead of __version__
  210. Reported by Daniele Tricoli
  211. 04/18/12: beazley
  212. Fixed issue #8: Literals empty list causes IndexError
  213. Reported by Walter Nissen.
  214. 04/18/12: beazley
  215. Fixed issue #12: Typo in code snippet in documentation
  216. Reported by florianschanda.
  217. 04/18/12: beazley
  218. Fixed issue #10: Correctly escape t_XOREQUAL pattern.
  219. Reported by Andy Kittner.
  220. Version 3.4
  221. ---------------------
  222. 02/17/11: beazley
  223. Minor patch to make cpp.py compatible with Python 3. Note: This
  224. is an experimental file not currently used by the rest of PLY.
  225. 02/17/11: beazley
  226. Fixed setup.py trove classifiers to properly list PLY as
  227. Python 3 compatible.
  228. 01/02/11: beazley
  229. Migration of repository to github.
  230. Version 3.3
  231. -----------------------------
  232. 08/25/09: beazley
  233. Fixed issue 15 related to the set_lineno() method in yacc. Reported by
  234. mdsherry.
  235. 08/25/09: beazley
  236. Fixed a bug related to regular expression compilation flags not being
  237. properly stored in lextab.py files created by the lexer when running
  238. in optimize mode. Reported by Bruce Frederiksen.
  239. Version 3.2
  240. -----------------------------
  241. 03/24/09: beazley
  242. Added an extra check to not print duplicated warning messages
  243. about reduce/reduce conflicts.
  244. 03/24/09: beazley
  245. Switched PLY over to a BSD-license.
  246. 03/23/09: beazley
  247. Performance optimization. Discovered a few places to make
  248. speedups in LR table generation.
  249. 03/23/09: beazley
  250. New warning message. PLY now warns about rules never
  251. reduced due to reduce/reduce conflicts. Suggested by
  252. Bruce Frederiksen.
  253. 03/23/09: beazley
  254. Some clean-up of warning messages related to reduce/reduce errors.
  255. 03/23/09: beazley
  256. Added a new picklefile option to yacc() to write the parsing
  257. tables to a filename using the pickle module. Here is how
  258. it works:
  259. yacc(picklefile="parsetab.p")
  260. This option can be used if the normal parsetab.py file is
  261. extremely large. For example, on jython, it is impossible
  262. to read parsing tables if the parsetab.py exceeds a certain
  263. threshold.
  264. The filename supplied to the picklefile option is opened
  265. relative to the current working directory of the Python
  266. interpreter. If you need to refer to the file elsewhere,
  267. you will need to supply an absolute or relative path.
  268. For maximum portability, the pickle file is written
  269. using protocol 0.
  270. 03/13/09: beazley
  271. Fixed a bug in parser.out generation where the rule numbers
  272. where off by one.
  273. 03/13/09: beazley
  274. Fixed a string formatting bug with one of the error messages.
  275. Reported by Richard Reitmeyer
  276. Version 3.1
  277. -----------------------------
  278. 02/28/09: beazley
  279. Fixed broken start argument to yacc(). PLY-3.0 broke this
  280. feature by accident.
  281. 02/28/09: beazley
  282. Fixed debugging output. yacc() no longer reports shift/reduce
  283. or reduce/reduce conflicts if debugging is turned off. This
  284. restores similar behavior in PLY-2.5. Reported by Andrew Waters.
  285. Version 3.0
  286. -----------------------------
  287. 02/03/09: beazley
  288. Fixed missing lexer attribute on certain tokens when
  289. invoking the parser p_error() function. Reported by
  290. Bart Whiteley.
  291. 02/02/09: beazley
  292. The lex() command now does all error-reporting and diagonistics
  293. using the logging module interface. Pass in a Logger object
  294. using the errorlog parameter to specify a different logger.
  295. 02/02/09: beazley
  296. Refactored ply.lex to use a more object-oriented and organized
  297. approach to collecting lexer information.
  298. 02/01/09: beazley
  299. Removed the nowarn option from lex(). All output is controlled
  300. by passing in a logger object. Just pass in a logger with a high
  301. level setting to suppress output. This argument was never
  302. documented to begin with so hopefully no one was relying upon it.
  303. 02/01/09: beazley
  304. Discovered and removed a dead if-statement in the lexer. This
  305. resulted in a 6-7% speedup in lexing when I tested it.
  306. 01/13/09: beazley
  307. Minor change to the procedure for signalling a syntax error in a
  308. production rule. A normal SyntaxError exception should be raised
  309. instead of yacc.SyntaxError.
  310. 01/13/09: beazley
  311. Added a new method p.set_lineno(n,lineno) that can be used to set the
  312. line number of symbol n in grammar rules. This simplifies manual
  313. tracking of line numbers.
  314. 01/11/09: beazley
  315. Vastly improved debugging support for yacc.parse(). Instead of passing
  316. debug as an integer, you can supply a Logging object (see the logging
  317. module). Messages will be generated at the ERROR, INFO, and DEBUG
  318. logging levels, each level providing progressively more information.
  319. The debugging trace also shows states, grammar rule, values passed
  320. into grammar rules, and the result of each reduction.
  321. 01/09/09: beazley
  322. The yacc() command now does all error-reporting and diagnostics using
  323. the interface of the logging module. Use the errorlog parameter to
  324. specify a logging object for error messages. Use the debuglog parameter
  325. to specify a logging object for the 'parser.out' output.
  326. 01/09/09: beazley
  327. *HUGE* refactoring of the the ply.yacc() implementation. The high-level
  328. user interface is backwards compatible, but the internals are completely
  329. reorganized into classes. No more global variables. The internals
  330. are also more extensible. For example, you can use the classes to
  331. construct a LALR(1) parser in an entirely different manner than
  332. what is currently the case. Documentation is forthcoming.
  333. 01/07/09: beazley
  334. Various cleanup and refactoring of yacc internals.
  335. 01/06/09: beazley
  336. Fixed a bug with precedence assignment. yacc was assigning the precedence
  337. each rule based on the left-most token, when in fact, it should have been
  338. using the right-most token. Reported by Bruce Frederiksen.
  339. 11/27/08: beazley
  340. Numerous changes to support Python 3.0 including removal of deprecated
  341. statements (e.g., has_key) and the additional of compatibility code
  342. to emulate features from Python 2 that have been removed, but which
  343. are needed. Fixed the unit testing suite to work with Python 3.0.
  344. The code should be backwards compatible with Python 2.
  345. 11/26/08: beazley
  346. Loosened the rules on what kind of objects can be passed in as the
  347. "module" parameter to lex() and yacc(). Previously, you could only use
  348. a module or an instance. Now, PLY just uses dir() to get a list of
  349. symbols on whatever the object is without regard for its type.
  350. 11/26/08: beazley
  351. Changed all except: statements to be compatible with Python2.x/3.x syntax.
  352. 11/26/08: beazley
  353. Changed all raise Exception, value statements to raise Exception(value) for
  354. forward compatibility.
  355. 11/26/08: beazley
  356. Removed all print statements from lex and yacc, using sys.stdout and sys.stderr
  357. directly. Preparation for Python 3.0 support.
  358. 11/04/08: beazley
  359. Fixed a bug with referring to symbols on the the parsing stack using negative
  360. indices.
  361. 05/29/08: beazley
  362. Completely revamped the testing system to use the unittest module for everything.
  363. Added additional tests to cover new errors/warnings.
  364. Version 2.5
  365. -----------------------------
  366. 05/28/08: beazley
  367. Fixed a bug with writing lex-tables in optimized mode and start states.
  368. Reported by Kevin Henry.
  369. Version 2.4
  370. -----------------------------
  371. 05/04/08: beazley
  372. A version number is now embedded in the table file signature so that
  373. yacc can more gracefully accomodate changes to the output format
  374. in the future.
  375. 05/04/08: beazley
  376. Removed undocumented .pushback() method on grammar productions. I'm
  377. not sure this ever worked and can't recall ever using it. Might have
  378. been an abandoned idea that never really got fleshed out. This
  379. feature was never described or tested so removing it is hopefully
  380. harmless.
  381. 05/04/08: beazley
  382. Added extra error checking to yacc() to detect precedence rules defined
  383. for undefined terminal symbols. This allows yacc() to detect a potential
  384. problem that can be really tricky to debug if no warning message or error
  385. message is generated about it.
  386. 05/04/08: beazley
  387. lex() now has an outputdir that can specify the output directory for
  388. tables when running in optimize mode. For example:
  389. lexer = lex.lex(optimize=True, lextab="ltab", outputdir="foo/bar")
  390. The behavior of specifying a table module and output directory are
  391. more aligned with the behavior of yacc().
  392. 05/04/08: beazley
  393. [Issue 9]
  394. Fixed filename bug in when specifying the modulename in lex() and yacc().
  395. If you specified options such as the following:
  396. parser = yacc.yacc(tabmodule="foo.bar.parsetab",outputdir="foo/bar")
  397. yacc would create a file "foo.bar.parsetab.py" in the given directory.
  398. Now, it simply generates a file "parsetab.py" in that directory.
  399. Bug reported by cptbinho.
  400. 05/04/08: beazley
  401. Slight modification to lex() and yacc() to allow their table files
  402. to be loaded from a previously loaded module. This might make
  403. it easier to load the parsing tables from a complicated package
  404. structure. For example:
  405. import foo.bar.spam.parsetab as parsetab
  406. parser = yacc.yacc(tabmodule=parsetab)
  407. Note: lex and yacc will never regenerate the table file if used
  408. in the form---you will get a warning message instead.
  409. This idea suggested by Brian Clapper.
  410. 04/28/08: beazley
  411. Fixed a big with p_error() functions being picked up correctly
  412. when running in yacc(optimize=1) mode. Patch contributed by
  413. Bart Whiteley.
  414. 02/28/08: beazley
  415. Fixed a bug with 'nonassoc' precedence rules. Basically the
  416. non-precedence was being ignored and not producing the correct
  417. run-time behavior in the parser.
  418. 02/16/08: beazley
  419. Slight relaxation of what the input() method to a lexer will
  420. accept as a string. Instead of testing the input to see
  421. if the input is a string or unicode string, it checks to see
  422. if the input object looks like it contains string data.
  423. This change makes it possible to pass string-like objects
  424. in as input. For example, the object returned by mmap.
  425. import mmap, os
  426. data = mmap.mmap(os.open(filename,os.O_RDONLY),
  427. os.path.getsize(filename),
  428. access=mmap.ACCESS_READ)
  429. lexer.input(data)
  430. 11/29/07: beazley
  431. Modification of ply.lex to allow token functions to aliased.
  432. This is subtle, but it makes it easier to create libraries and
  433. to reuse token specifications. For example, suppose you defined
  434. a function like this:
  435. def number(t):
  436. r'\d+'
  437. t.value = int(t.value)
  438. return t
  439. This change would allow you to define a token rule as follows:
  440. t_NUMBER = number
  441. In this case, the token type will be set to 'NUMBER' and use
  442. the associated number() function to process tokens.
  443. 11/28/07: beazley
  444. Slight modification to lex and yacc to grab symbols from both
  445. the local and global dictionaries of the caller. This
  446. modification allows lexers and parsers to be defined using
  447. inner functions and closures.
  448. 11/28/07: beazley
  449. Performance optimization: The lexer.lexmatch and t.lexer
  450. attributes are no longer set for lexer tokens that are not
  451. defined by functions. The only normal use of these attributes
  452. would be in lexer rules that need to perform some kind of
  453. special processing. Thus, it doesn't make any sense to set
  454. them on every token.
  455. *** POTENTIAL INCOMPATIBILITY *** This might break code
  456. that is mucking around with internal lexer state in some
  457. sort of magical way.
  458. 11/27/07: beazley
  459. Added the ability to put the parser into error-handling mode
  460. from within a normal production. To do this, simply raise
  461. a yacc.SyntaxError exception like this:
  462. def p_some_production(p):
  463. 'some_production : prod1 prod2'
  464. ...
  465. raise yacc.SyntaxError # Signal an error
  466. A number of things happen after this occurs:
  467. - The last symbol shifted onto the symbol stack is discarded
  468. and parser state backed up to what it was before the
  469. the rule reduction.
  470. - The current lookahead symbol is saved and replaced by
  471. the 'error' symbol.
  472. - The parser enters error recovery mode where it tries
  473. to either reduce the 'error' rule or it starts
  474. discarding items off of the stack until the parser
  475. resets.
  476. When an error is manually set, the parser does *not* call
  477. the p_error() function (if any is defined).
  478. *** NEW FEATURE *** Suggested on the mailing list
  479. 11/27/07: beazley
  480. Fixed structure bug in examples/ansic. Reported by Dion Blazakis.
  481. 11/27/07: beazley
  482. Fixed a bug in the lexer related to start conditions and ignored
  483. token rules. If a rule was defined that changed state, but
  484. returned no token, the lexer could be left in an inconsistent
  485. state. Reported by
  486. 11/27/07: beazley
  487. Modified setup.py to support Python Eggs. Patch contributed by
  488. Simon Cross.
  489. 11/09/07: beazely
  490. Fixed a bug in error handling in yacc. If a syntax error occurred and the
  491. parser rolled the entire parse stack back, the parser would be left in in
  492. inconsistent state that would cause it to trigger incorrect actions on
  493. subsequent input. Reported by Ton Biegstraaten, Justin King, and others.
  494. 11/09/07: beazley
  495. Fixed a bug when passing empty input strings to yacc.parse(). This
  496. would result in an error message about "No input given". Reported
  497. by Andrew Dalke.
  498. Version 2.3
  499. -----------------------------
  500. 02/20/07: beazley
  501. Fixed a bug with character literals if the literal '.' appeared as the
  502. last symbol of a grammar rule. Reported by Ales Smrcka.
  503. 02/19/07: beazley
  504. Warning messages are now redirected to stderr instead of being printed
  505. to standard output.
  506. 02/19/07: beazley
  507. Added a warning message to lex.py if it detects a literal backslash
  508. character inside the t_ignore declaration. This is to help
  509. problems that might occur if someone accidentally defines t_ignore
  510. as a Python raw string. For example:
  511. t_ignore = r' \t'
  512. The idea for this is from an email I received from David Cimimi who
  513. reported bizarre behavior in lexing as a result of defining t_ignore
  514. as a raw string by accident.
  515. 02/18/07: beazley
  516. Performance improvements. Made some changes to the internal
  517. table organization and LR parser to improve parsing performance.
  518. 02/18/07: beazley
  519. Automatic tracking of line number and position information must now be
  520. enabled by a special flag to parse(). For example:
  521. yacc.parse(data,tracking=True)
  522. In many applications, it's just not that important to have the
  523. parser automatically track all line numbers. By making this an
  524. optional feature, it allows the parser to run significantly faster
  525. (more than a 20% speed increase in many cases). Note: positional
  526. information is always available for raw tokens---this change only
  527. applies to positional information associated with nonterminal
  528. grammar symbols.
  529. *** POTENTIAL INCOMPATIBILITY ***
  530. 02/18/07: beazley
  531. Yacc no longer supports extended slices of grammar productions.
  532. However, it does support regular slices. For example:
  533. def p_foo(p):
  534. '''foo: a b c d e'''
  535. p[0] = p[1:3]
  536. This change is a performance improvement to the parser--it streamlines
  537. normal access to the grammar values since slices are now handled in
  538. a __getslice__() method as opposed to __getitem__().
  539. 02/12/07: beazley
  540. Fixed a bug in the handling of token names when combined with
  541. start conditions. Bug reported by Todd O'Bryan.
  542. Version 2.2
  543. ------------------------------
  544. 11/01/06: beazley
  545. Added lexpos() and lexspan() methods to grammar symbols. These
  546. mirror the same functionality of lineno() and linespan(). For
  547. example:
  548. def p_expr(p):
  549. 'expr : expr PLUS expr'
  550. p.lexpos(1) # Lexing position of left-hand-expression
  551. p.lexpos(1) # Lexing position of PLUS
  552. start,end = p.lexspan(3) # Lexing range of right hand expression
  553. 11/01/06: beazley
  554. Minor change to error handling. The recommended way to skip characters
  555. in the input is to use t.lexer.skip() as shown here:
  556. def t_error(t):
  557. print "Illegal character '%s'" % t.value[0]
  558. t.lexer.skip(1)
  559. The old approach of just using t.skip(1) will still work, but won't
  560. be documented.
  561. 10/31/06: beazley
  562. Discarded tokens can now be specified as simple strings instead of
  563. functions. To do this, simply include the text "ignore_" in the
  564. token declaration. For example:
  565. t_ignore_cppcomment = r'//.*'
  566. Previously, this had to be done with a function. For example:
  567. def t_ignore_cppcomment(t):
  568. r'//.*'
  569. pass
  570. If start conditions/states are being used, state names should appear
  571. before the "ignore_" text.
  572. 10/19/06: beazley
  573. The Lex module now provides support for flex-style start conditions
  574. as described at http://www.gnu.org/software/flex/manual/html_chapter/flex_11.html.
  575. Please refer to this document to understand this change note. Refer to
  576. the PLY documentation for PLY-specific explanation of how this works.
  577. To use start conditions, you first need to declare a set of states in
  578. your lexer file:
  579. states = (
  580. ('foo','exclusive'),
  581. ('bar','inclusive')
  582. )
  583. This serves the same role as the %s and %x specifiers in flex.
  584. One a state has been declared, tokens for that state can be
  585. declared by defining rules of the form t_state_TOK. For example:
  586. t_PLUS = '\+' # Rule defined in INITIAL state
  587. t_foo_NUM = '\d+' # Rule defined in foo state
  588. t_bar_NUM = '\d+' # Rule defined in bar state
  589. t_foo_bar_NUM = '\d+' # Rule defined in both foo and bar
  590. t_ANY_NUM = '\d+' # Rule defined in all states
  591. In addition to defining tokens for each state, the t_ignore and t_error
  592. specifications can be customized for specific states. For example:
  593. t_foo_ignore = " " # Ignored characters for foo state
  594. def t_bar_error(t):
  595. # Handle errors in bar state
  596. With token rules, the following methods can be used to change states
  597. def t_TOKNAME(t):
  598. t.lexer.begin('foo') # Begin state 'foo'
  599. t.lexer.push_state('foo') # Begin state 'foo', push old state
  600. # onto a stack
  601. t.lexer.pop_state() # Restore previous state
  602. t.lexer.current_state() # Returns name of current state
  603. These methods mirror the BEGIN(), yy_push_state(), yy_pop_state(), and
  604. yy_top_state() functions in flex.
  605. The use of start states can be used as one way to write sub-lexers.
  606. For example, the lexer or parser might instruct the lexer to start
  607. generating a different set of tokens depending on the context.
  608. example/yply/ylex.py shows the use of start states to grab C/C++
  609. code fragments out of traditional yacc specification files.
  610. *** NEW FEATURE *** Suggested by Daniel Larraz with whom I also
  611. discussed various aspects of the design.
  612. 10/19/06: beazley
  613. Minor change to the way in which yacc.py was reporting shift/reduce
  614. conflicts. Although the underlying LALR(1) algorithm was correct,
  615. PLY was under-reporting the number of conflicts compared to yacc/bison
  616. when precedence rules were in effect. This change should make PLY
  617. report the same number of conflicts as yacc.
  618. 10/19/06: beazley
  619. Modified yacc so that grammar rules could also include the '-'
  620. character. For example:
  621. def p_expr_list(p):
  622. 'expression-list : expression-list expression'
  623. Suggested by Oldrich Jedlicka.
  624. 10/18/06: beazley
  625. Attribute lexer.lexmatch added so that token rules can access the re
  626. match object that was generated. For example:
  627. def t_FOO(t):
  628. r'some regex'
  629. m = t.lexer.lexmatch
  630. # Do something with m
  631. This may be useful if you want to access named groups specified within
  632. the regex for a specific token. Suggested by Oldrich Jedlicka.
  633. 10/16/06: beazley
  634. Changed the error message that results if an illegal character
  635. is encountered and no default error function is defined in lex.
  636. The exception is now more informative about the actual cause of
  637. the error.
  638. Version 2.1
  639. ------------------------------
  640. 10/02/06: beazley
  641. The last Lexer object built by lex() can be found in lex.lexer.
  642. The last Parser object built by yacc() can be found in yacc.parser.
  643. 10/02/06: beazley
  644. New example added: examples/yply
  645. This example uses PLY to convert Unix-yacc specification files to
  646. PLY programs with the same grammar. This may be useful if you
  647. want to convert a grammar from bison/yacc to use with PLY.
  648. 10/02/06: beazley
  649. Added support for a start symbol to be specified in the yacc
  650. input file itself. Just do this:
  651. start = 'name'
  652. where 'name' matches some grammar rule. For example:
  653. def p_name(p):
  654. 'name : A B C'
  655. ...
  656. This mirrors the functionality of the yacc %start specifier.
  657. 09/30/06: beazley
  658. Some new examples added.:
  659. examples/GardenSnake : A simple indentation based language similar
  660. to Python. Shows how you might handle
  661. whitespace. Contributed by Andrew Dalke.
  662. examples/BASIC : An implementation of 1964 Dartmouth BASIC.
  663. Contributed by Dave against his better
  664. judgement.
  665. 09/28/06: beazley
  666. Minor patch to allow named groups to be used in lex regular
  667. expression rules. For example:
  668. t_QSTRING = r'''(?P<quote>['"]).*?(?P=quote)'''
  669. Patch submitted by Adam Ring.
  670. 09/28/06: beazley
  671. LALR(1) is now the default parsing method. To use SLR, use
  672. yacc.yacc(method="SLR"). Note: there is no performance impact
  673. on parsing when using LALR(1) instead of SLR. However, constructing
  674. the parsing tables will take a little longer.
  675. 09/26/06: beazley
  676. Change to line number tracking. To modify line numbers, modify
  677. the line number of the lexer itself. For example:
  678. def t_NEWLINE(t):
  679. r'\n'
  680. t.lexer.lineno += 1
  681. This modification is both cleanup and a performance optimization.
  682. In past versions, lex was monitoring every token for changes in
  683. the line number. This extra processing is unnecessary for a vast
  684. majority of tokens. Thus, this new approach cleans it up a bit.
  685. *** POTENTIAL INCOMPATIBILITY ***
  686. You will need to change code in your lexer that updates the line
  687. number. For example, "t.lineno += 1" becomes "t.lexer.lineno += 1"
  688. 09/26/06: beazley
  689. Added the lexing position to tokens as an attribute lexpos. This
  690. is the raw index into the input text at which a token appears.
  691. This information can be used to compute column numbers and other
  692. details (e.g., scan backwards from lexpos to the first newline
  693. to get a column position).
  694. 09/25/06: beazley
  695. Changed the name of the __copy__() method on the Lexer class
  696. to clone(). This is used to clone a Lexer object (e.g., if
  697. you're running different lexers at the same time).
  698. 09/21/06: beazley
  699. Limitations related to the use of the re module have been eliminated.
  700. Several users reported problems with regular expressions exceeding
  701. more than 100 named groups. To solve this, lex.py is now capable
  702. of automatically splitting its master regular regular expression into
  703. smaller expressions as needed. This should, in theory, make it
  704. possible to specify an arbitrarily large number of tokens.
  705. 09/21/06: beazley
  706. Improved error checking in lex.py. Rules that match the empty string
  707. are now rejected (otherwise they cause the lexer to enter an infinite
  708. loop). An extra check for rules containing '#' has also been added.
  709. Since lex compiles regular expressions in verbose mode, '#' is interpreted
  710. as a regex comment, it is critical to use '\#' instead.
  711. 09/18/06: beazley
  712. Added a @TOKEN decorator function to lex.py that can be used to
  713. define token rules where the documentation string might be computed
  714. in some way.
  715. digit = r'([0-9])'
  716. nondigit = r'([_A-Za-z])'
  717. identifier = r'(' + nondigit + r'(' + digit + r'|' + nondigit + r')*)'
  718. from ply.lex import TOKEN
  719. @TOKEN(identifier)
  720. def t_ID(t):
  721. # Do whatever
  722. The @TOKEN decorator merely sets the documentation string of the
  723. associated token function as needed for lex to work.
  724. Note: An alternative solution is the following:
  725. def t_ID(t):
  726. # Do whatever
  727. t_ID.__doc__ = identifier
  728. Note: Decorators require the use of Python 2.4 or later. If compatibility
  729. with old versions is needed, use the latter solution.
  730. The need for this feature was suggested by Cem Karan.
  731. 09/14/06: beazley
  732. Support for single-character literal tokens has been added to yacc.
  733. These literals must be enclosed in quotes. For example:
  734. def p_expr(p):
  735. "expr : expr '+' expr"
  736. ...
  737. def p_expr(p):
  738. 'expr : expr "-" expr'
  739. ...
  740. In addition to this, it is necessary to tell the lexer module about
  741. literal characters. This is done by defining the variable 'literals'
  742. as a list of characters. This should be defined in the module that
  743. invokes the lex.lex() function. For example:
  744. literals = ['+','-','*','/','(',')','=']
  745. or simply
  746. literals = '+=*/()='
  747. It is important to note that literals can only be a single character.
  748. When the lexer fails to match a token using its normal regular expression
  749. rules, it will check the current character against the literal list.
  750. If found, it will be returned with a token type set to match the literal
  751. character. Otherwise, an illegal character will be signalled.
  752. 09/14/06: beazley
  753. Modified PLY to install itself as a proper Python package called 'ply'.
  754. This will make it a little more friendly to other modules. This
  755. changes the usage of PLY only slightly. Just do this to import the
  756. modules
  757. import ply.lex as lex
  758. import ply.yacc as yacc
  759. Alternatively, you can do this:
  760. from ply import *
  761. Which imports both the lex and yacc modules.
  762. Change suggested by Lee June.
  763. 09/13/06: beazley
  764. Changed the handling of negative indices when used in production rules.
  765. A negative production index now accesses already parsed symbols on the
  766. parsing stack. For example,
  767. def p_foo(p):
  768. "foo: A B C D"
  769. print p[1] # Value of 'A' symbol
  770. print p[2] # Value of 'B' symbol
  771. print p[-1] # Value of whatever symbol appears before A
  772. # on the parsing stack.
  773. p[0] = some_val # Sets the value of the 'foo' grammer symbol
  774. This behavior makes it easier to work with embedded actions within the
  775. parsing rules. For example, in C-yacc, it is possible to write code like
  776. this:
  777. bar: A { printf("seen an A = %d\n", $1); } B { do_stuff; }
  778. In this example, the printf() code executes immediately after A has been
  779. parsed. Within the embedded action code, $1 refers to the A symbol on
  780. the stack.
  781. To perform this equivalent action in PLY, you need to write a pair
  782. of rules like this:
  783. def p_bar(p):
  784. "bar : A seen_A B"
  785. do_stuff
  786. def p_seen_A(p):
  787. "seen_A :"
  788. print "seen an A =", p[-1]
  789. The second rule "seen_A" is merely a empty production which should be
  790. reduced as soon as A is parsed in the "bar" rule above. The use
  791. of the negative index p[-1] is used to access whatever symbol appeared
  792. before the seen_A symbol.
  793. This feature also makes it possible to support inherited attributes.
  794. For example:
  795. def p_decl(p):
  796. "decl : scope name"
  797. def p_scope(p):
  798. """scope : GLOBAL
  799. | LOCAL"""
  800. p[0] = p[1]
  801. def p_name(p):
  802. "name : ID"
  803. if p[-1] == "GLOBAL":
  804. # ...
  805. else if p[-1] == "LOCAL":
  806. #...
  807. In this case, the name rule is inheriting an attribute from the
  808. scope declaration that precedes it.
  809. *** POTENTIAL INCOMPATIBILITY ***
  810. If you are currently using negative indices within existing grammar rules,
  811. your code will break. This should be extremely rare if non-existent in
  812. most cases. The argument to various grammar rules is not usually not
  813. processed in the same way as a list of items.
  814. Version 2.0
  815. ------------------------------
  816. 09/07/06: beazley
  817. Major cleanup and refactoring of the LR table generation code. Both SLR
  818. and LALR(1) table generation is now performed by the same code base with
  819. only minor extensions for extra LALR(1) processing.
  820. 09/07/06: beazley
  821. Completely reimplemented the entire LALR(1) parsing engine to use the
  822. DeRemer and Pennello algorithm for calculating lookahead sets. This
  823. significantly improves the performance of generating LALR(1) tables
  824. and has the added feature of actually working correctly! If you
  825. experienced weird behavior with LALR(1) in prior releases, this should
  826. hopefully resolve all of those problems. Many thanks to
  827. Andrew Waters and Markus Schoepflin for submitting bug reports
  828. and helping me test out the revised LALR(1) support.
  829. Version 1.8
  830. ------------------------------
  831. 08/02/06: beazley
  832. Fixed a problem related to the handling of default actions in LALR(1)
  833. parsing. If you experienced subtle and/or bizarre behavior when trying
  834. to use the LALR(1) engine, this may correct those problems. Patch
  835. contributed by Russ Cox. Note: This patch has been superceded by
  836. revisions for LALR(1) parsing in Ply-2.0.
  837. 08/02/06: beazley
  838. Added support for slicing of productions in yacc.
  839. Patch contributed by Patrick Mezard.
  840. Version 1.7
  841. ------------------------------
  842. 03/02/06: beazley
  843. Fixed infinite recursion problem ReduceToTerminals() function that
  844. would sometimes come up in LALR(1) table generation. Reported by
  845. Markus Schoepflin.
  846. 03/01/06: beazley
  847. Added "reflags" argument to lex(). For example:
  848. lex.lex(reflags=re.UNICODE)
  849. This can be used to specify optional flags to the re.compile() function
  850. used inside the lexer. This may be necessary for special situations such
  851. as processing Unicode (e.g., if you want escapes like \w and \b to consult
  852. the Unicode character property database). The need for this suggested by
  853. Andreas Jung.
  854. 03/01/06: beazley
  855. Fixed a bug with an uninitialized variable on repeated instantiations of parser
  856. objects when the write_tables=0 argument was used. Reported by Michael Brown.
  857. 03/01/06: beazley
  858. Modified lex.py to accept Unicode strings both as the regular expressions for
  859. tokens and as input. Hopefully this is the only change needed for Unicode support.
  860. Patch contributed by Johan Dahl.
  861. 03/01/06: beazley
  862. Modified the class-based interface to work with new-style or old-style classes.
  863. Patch contributed by Michael Brown (although I tweaked it slightly so it would work
  864. with older versions of Python).
  865. Version 1.6
  866. ------------------------------
  867. 05/27/05: beazley
  868. Incorporated patch contributed by Christopher Stawarz to fix an extremely
  869. devious bug in LALR(1) parser generation. This patch should fix problems
  870. numerous people reported with LALR parsing.
  871. 05/27/05: beazley
  872. Fixed problem with lex.py copy constructor. Reported by Dave Aitel, Aaron Lav,
  873. and Thad Austin.
  874. 05/27/05: beazley
  875. Added outputdir option to yacc() to control output directory. Contributed
  876. by Christopher Stawarz.
  877. 05/27/05: beazley
  878. Added rununit.py test script to run tests using the Python unittest module.
  879. Contributed by Miki Tebeka.
  880. Version 1.5
  881. ------------------------------
  882. 05/26/04: beazley
  883. Major enhancement. LALR(1) parsing support is now working.
  884. This feature was implemented by Elias Ioup (ezioup@alumni.uchicago.edu)
  885. and optimized by David Beazley. To use LALR(1) parsing do
  886. the following:
  887. yacc.yacc(method="LALR")
  888. Computing LALR(1) parsing tables takes about twice as long as
  889. the default SLR method. However, LALR(1) allows you to handle
  890. more complex grammars. For example, the ANSI C grammar
  891. (in example/ansic) has 13 shift-reduce conflicts with SLR, but
  892. only has 1 shift-reduce conflict with LALR(1).
  893. 05/20/04: beazley
  894. Added a __len__ method to parser production lists. Can
  895. be used in parser rules like this:
  896. def p_somerule(p):
  897. """a : B C D
  898. | E F"
  899. if (len(p) == 3):
  900. # Must have been first rule
  901. elif (len(p) == 2):
  902. # Must be second rule
  903. Suggested by Joshua Gerth and others.
  904. Version 1.4
  905. ------------------------------
  906. 04/23/04: beazley
  907. Incorporated a variety of patches contributed by Eric Raymond.
  908. These include:
  909. 0. Cleans up some comments so they don't wrap on an 80-column display.
  910. 1. Directs compiler errors to stderr where they belong.
  911. 2. Implements and documents automatic line counting when \n is ignored.
  912. 3. Changes the way progress messages are dumped when debugging is on.
  913. The new format is both less verbose and conveys more information than
  914. the old, including shift and reduce actions.
  915. 04/23/04: beazley
  916. Added a Python setup.py file to simply installation. Contributed
  917. by Adam Kerrison.
  918. 04/23/04: beazley
  919. Added patches contributed by Adam Kerrison.
  920. - Some output is now only shown when debugging is enabled. This
  921. means that PLY will be completely silent when not in debugging mode.
  922. - An optional parameter "write_tables" can be passed to yacc() to
  923. control whether or not parsing tables are written. By default,
  924. it is true, but it can be turned off if you don't want the yacc
  925. table file. Note: disabling this will cause yacc() to regenerate
  926. the parsing table each time.
  927. 04/23/04: beazley
  928. Added patches contributed by David McNab. This patch addes two
  929. features:
  930. - The parser can be supplied as a class instead of a module.
  931. For an example of this, see the example/classcalc directory.
  932. - Debugging output can be directed to a filename of the user's
  933. choice. Use
  934. yacc(debugfile="somefile.out")
  935. Version 1.3
  936. ------------------------------
  937. 12/10/02: jmdyck
  938. Various minor adjustments to the code that Dave checked in today.
  939. Updated test/yacc_{inf,unused}.exp to reflect today's changes.
  940. 12/10/02: beazley
  941. Incorporated a variety of minor bug fixes to empty production
  942. handling and infinite recursion checking. Contributed by
  943. Michael Dyck.
  944. 12/10/02: beazley
  945. Removed bogus recover() method call in yacc.restart()
  946. Version 1.2
  947. ------------------------------
  948. 11/27/02: beazley
  949. Lexer and parser objects are now available as an attribute
  950. of tokens and slices respectively. For example:
  951. def t_NUMBER(t):
  952. r'\d+'
  953. print t.lexer
  954. def p_expr_plus(t):
  955. 'expr: expr PLUS expr'
  956. print t.lexer
  957. print t.parser
  958. This can be used for state management (if needed).
  959. 10/31/02: beazley
  960. Modified yacc.py to work with Python optimize mode. To make
  961. this work, you need to use
  962. yacc.yacc(optimize=1)
  963. Furthermore, you need to first run Python in normal mode
  964. to generate the necessary parsetab.py files. After that,
  965. you can use python -O or python -OO.
  966. Note: optimized mode turns off a lot of error checking.
  967. Only use when you are sure that your grammar is working.
  968. Make sure parsetab.py is up to date!
  969. 10/30/02: beazley
  970. Added cloning of Lexer objects. For example:
  971. import copy
  972. l = lex.lex()
  973. lc = copy.copy(l)
  974. l.input("Some text")
  975. lc.input("Some other text")
  976. ...
  977. This might be useful if the same "lexer" is meant to
  978. be used in different contexts---or if multiple lexers
  979. are running concurrently.
  980. 10/30/02: beazley
  981. Fixed subtle bug with first set computation and empty productions.
  982. Patch submitted by Michael Dyck.
  983. 10/30/02: beazley
  984. Fixed error messages to use "filename:line: message" instead
  985. of "filename:line. message". This makes error reporting more
  986. friendly to emacs. Patch submitted by François Pinard.
  987. 10/30/02: beazley
  988. Improvements to parser.out file. Terminals and nonterminals
  989. are sorted instead of being printed in random order.
  990. Patch submitted by François Pinard.
  991. 10/30/02: beazley
  992. Improvements to parser.out file output. Rules are now printed
  993. in a way that's easier to understand. Contributed by Russ Cox.
  994. 10/30/02: beazley
  995. Added 'nonassoc' associativity support. This can be used
  996. to disable the chaining of operators like a < b < c.
  997. To use, simply specify 'nonassoc' in the precedence table
  998. precedence = (
  999. ('nonassoc', 'LESSTHAN', 'GREATERTHAN'), # Nonassociative operators
  1000. ('left', 'PLUS', 'MINUS'),
  1001. ('left', 'TIMES', 'DIVIDE'),
  1002. ('right', 'UMINUS'), # Unary minus operator
  1003. )
  1004. Patch contributed by Russ Cox.
  1005. 10/30/02: beazley
  1006. Modified the lexer to provide optional support for Python -O and -OO
  1007. modes. To make this work, Python *first* needs to be run in
  1008. unoptimized mode. This reads the lexing information and creates a
  1009. file "lextab.py". Then, run lex like this:
  1010. # module foo.py
  1011. ...
  1012. ...
  1013. lex.lex(optimize=1)
  1014. Once the lextab file has been created, subsequent calls to
  1015. lex.lex() will read data from the lextab file instead of using
  1016. introspection. In optimized mode (-O, -OO) everything should
  1017. work normally despite the loss of doc strings.
  1018. To change the name of the file 'lextab.py' use the following:
  1019. lex.lex(lextab="footab")
  1020. (this creates a file footab.py)
  1021. Version 1.1 October 25, 2001
  1022. ------------------------------
  1023. 10/25/01: beazley
  1024. Modified the table generator to produce much more compact data.
  1025. This should greatly reduce the size of the parsetab.py[c] file.
  1026. Caveat: the tables still need to be constructed so a little more
  1027. work is done in parsetab on import.
  1028. 10/25/01: beazley
  1029. There may be a possible bug in the cycle detector that reports errors
  1030. about infinite recursion. I'm having a little trouble tracking it
  1031. down, but if you get this problem, you can disable the cycle
  1032. detector as follows:
  1033. yacc.yacc(check_recursion = 0)
  1034. 10/25/01: beazley
  1035. Fixed a bug in lex.py that sometimes caused illegal characters to be
  1036. reported incorrectly. Reported by Sverre Jørgensen.
  1037. 7/8/01 : beazley
  1038. Added a reference to the underlying lexer object when tokens are handled by
  1039. functions. The lexer is available as the 'lexer' attribute. This
  1040. was added to provide better lexing support for languages such as Fortran
  1041. where certain types of tokens can't be conveniently expressed as regular
  1042. expressions (and where the tokenizing function may want to perform a
  1043. little backtracking). Suggested by Pearu Peterson.
  1044. 6/20/01 : beazley
  1045. Modified yacc() function so that an optional starting symbol can be specified.
  1046. For example:
  1047. yacc.yacc(start="statement")
  1048. Normally yacc always treats the first production rule as the starting symbol.
  1049. However, if you are debugging your grammar it may be useful to specify
  1050. an alternative starting symbol. Idea suggested by Rich Salz.
  1051. Version 1.0 June 18, 2001
  1052. --------------------------
  1053. Initial public offering