index.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>PyBison - Python-based Parsing at the Speed of C</title>
  5. </head>
  6. <body>
  7. <center>
  8. <script type="text/javascript"><!--
  9. google_ad_client = "pub-0243656465094951";
  10. google_ad_width = 728;
  11. google_ad_height = 90;
  12. google_ad_format = "728x90_as";
  13. google_ad_channel ="";
  14. google_color_border = "DDB7BA";
  15. google_color_bg = "FFF5F6";
  16. google_color_link = "0000CC";
  17. google_color_url = "008000";
  18. google_color_text = "6F6F6F";
  19. //--></script>
  20. <script type="text/javascript"
  21. src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
  22. </script>
  23. </center>
  24. <center>Return to <a href="http://www.freenet.org.nz/python">David's Python Resources</a></center
  25. <h1>PyBison - Python-based Parsing at the Speed of C</h1>
  26. <hr>
  27. <h2>Getting PyBison</h2>
  28. <blockquote>
  29. License: GPL
  30. <small><i>(but we will consider applications for licenses for
  31. commercial non-open-source deployment)</i></small>
  32. <ul>
  33. <li>Download PyBison -
  34. <a href="http://www.freenet.org.nz/python/pybison/pybison-0.1.8.tar.gz">pybison-0.1.8.tar.gz</a>
  35. </li>
  36. <li>See an <a href="calc.py">Example that implements a simple calculator</a></li>
  37. <li>Read the <a href="walkthrough.html">PyBison Walkthrough Tutorial</a></li>
  38. <li>Peruse the <a href="api/index.html">PyBison API Reference</a></li>
  39. <li>View the <a href="CHANGELOG.txt">Change Log</a></li>
  40. </ul>
  41. </blockquote>
  42. <hr>
  43. <h2>Introduction</h2>
  44. <blockquote>
  45. PyBison is a Python binding to the Bison (yacc) and Flex (lex) parser-generator
  46. utilities.<br>
  47. <br>
  48. It allows parsers to be quickly and easily developed as Python class
  49. declarations, and for these parsers to take advantage of the fast and
  50. powerful C-based Bison/Flex.<br>
  51. <br>
  52. Users write a subclass of a basic Parser object, containing a set of methods
  53. and attributes specifying the grammar and lexical analysis rules, and taking
  54. callbacks for providing parser input, and receiving parser target events.<br>
  55. <br>
  56. Presently, PyBison is only working on Linux (and possibly *BSD-based) systems.
  57. However, in time, (or if someone volunteers to help out with probably 2 hours'
  58. coding for a small shim layer) it's very possible PyBison will work on Windows
  59. as well.<br>
  60. <br>
  61. </blockquote>
  62. <hr>
  63. <h2>Features</h2>
  64. <blockquote>
  65. <ul>
  66. <li>Runs at near the speed of C-based parsers, due to direct hooks into bison-generated C code</li>
  67. <li>Full LALR(1) grammar support</li>
  68. <li>Includes a utility to convert your legacy grammar (.y) and scanner (.l) scripts into
  69. python modules compatible with PyBison</li>
  70. <li>Easy to understand - the walkthrough and the examples will have you writing your
  71. own parsers in minutes</li>
  72. <li>Comfortable and intuitive callback mechanisms</li>
  73. <li>Can export parse tree to XML with a simple method call
  74. <b><i style="color:#800000">(New!)</i></b></li>
  75. <li>Can reconstitute a parse tree from XML <b><i style="color:#800000">(New!)</i></b></li>
  76. <li>Examples include working parsers for the languages:
  77. <ul>
  78. <li>ANSI C</li>
  79. <li>Java (1.4.2)</li>
  80. </ul>
  81. </li>
  82. </ul>
  83. </blockquote>
  84. <h2>Comparison to Other Python Parsers</h2>
  85. <blockquote>
  86. This comparison is probably very biased, since it's written by the author
  87. of PyBison. However, it should help you to decide whether PyBison is for you.<br>
  88. <br>
  89. All the other Python-based parser-construction toolkits I've seen work
  90. in pure Python. While this offers conveniences such as not requiring binary compilation,
  91. and eliminating dependencies on third-party libraries and other software, it can
  92. incur a savage performance penalty.<br>
  93. <br>
  94. I've seen some Python parser frameworks which use an idiosyncratic syntax, which I
  95. couldn't (or wouldn't) comfortably relate to, especially since I have a background
  96. of developing large yacc-based packages. In particular, I wanted to build my
  97. parser in genuine Python source files, rather than embedding Python code into
  98. a different script language.<br>
  99. <br>
  100. On the other hand, I found the PLY parser framework to be much more comfortably
  101. Pythonic, in that targets are mapped to class methods. However, I ran into a
  102. couple of problems with PLY, namely:
  103. <ul>
  104. <li>speed - PLY could be very slow with generating the parse tables, and
  105. with parsing its input</li>
  106. <li>capacity - due to its use of named-match regular expressions (and the
  107. underlying Python limitation), the lexical analysis is limited to a
  108. vocabulary of 100 unique token types - insufficient for large modern
  109. languages.</li>
  110. <li>model - PLY is limited to SLR parsing, whereas Bison does full LALR(1)</li>
  111. <li>streaming - PLY's lexer requires the full input string to be made
  112. available in one chunk, making it unsuitable for parsing streams of
  113. unpredictable length, whereas Bison and Flex can work from a continuous
  114. stream.
  115. </ul>
  116. With PyBison, I've opted for a system which:
  117. <ul>
  118. <li>Extracts grammar and lexical analysis information from user-written
  119. parser classes</li>
  120. <li>Generates bison and flex sources, and converts these into C sources</li>
  121. <li>Compiles these into a shared loadable library, which is re-usable with
  122. subsequent parsing runs (and which gets automatically rebuilt if (by
  123. virtue of hashing tests) any of the grammar, precedence, tokens or
  124. lexing rules change</li>
  125. <li>Uses Pyrex to interface with this library, calling its yyparse() routine
  126. and taking callbacks for input requests and target fulfilment events</li>
  127. </ul>
  128. The result is a parser toolkit with a Python front end and Python's luxurious
  129. comfort, ease of use, but with (most of) the speed and power of traditional
  130. bison/yacc-based parsers.
  131. </blockquote>
  132. </body>
  133. </html>