/contrib/lispbuilder-20151218-git/lispbuilder-lexer/index.html

https://gitlab.com/dto/ecl-android-games-src · HTML · 91 lines · 73 code · 18 blank · 0 comment · 0 complexity · ea25c344bd0a93091af3dfad57a3b5ef MD5 · raw file

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>LEXER Package</title>
  5. </head>
  6. <body>
  7. <p><em>Text copied from
  8. <a href="http://www.geocities.com/mparker762/clawk">http://www.geocities.com/mparker762/clawk</a></em></p>
  9. <h2>LEXER package</h2>
  10. <p>The LEXER package implements a lexical-analyzer-generator
  11. called <em>DEFLEXER</em>, which is built on top of both
  12. REGEX and CLAWK. Many of the optimizations in the recent
  13. rewrite of the regex engine went into optimizing the sorts
  14. of patterns generated by DEFLEX.</p>
  15. <p>The default lexer doesn't implement full greediness. If you have a rule for ints followed by a rule for floats, the int rule will match on the part before the decimal before the float rule gets a change to look at it. You can fix this by specifying <code>:flex-compatible</code> as the first rule. This gives all patterns a chance to examine the text and takes the one that matches the longest string (first pattern wins in case of a tie). The down side of this option is that it slows down the analyser. If you can solve the issue by reordering your rules that's the way to do it.</p>
  16. <p>I'm currently writing an AWK->CLAWK translator using this
  17. as the lexer, and it's working fine. As far as I can tell,
  18. the DEFLEXER-generated lexing functions should be fast
  19. enough for production use.</p>
  20. <p>Currently, the LEX/FLEX/BISON feature of switching
  21. productions on and off using state variables is not
  22. supported, but it's a pretty simple feature to add. If
  23. you're using LEXER and discover you need this feature,
  24. let me know.</p>
  25. <p>It also doesn't yet support prefix and postfix context
  26. patterns. This isn't quite so trivial to add, but it's
  27. planned for a future release of regex, so LEXER will be
  28. getting it someday.</p>
  29. <p>Anyway, Here's a simple <em>DEFLEXER</em> example:</p>
  30. <code><pre>
  31. (deflexer test-lexer
  32. ("[0-9]+([.][0-9]+([Ee][0-9]+)?)"
  33. (return (values 'flt (num %0))))
  34. ("[0-9]+"
  35. (return (values 'int (int %0))))
  36. ("[:alpha:][:alnum:]*"
  37. (return (values 'name %0)))
  38. ("[:space:]+") )
  39. > (setq *lex* (test-lexer "1.0 12 fred 10.23e45"))
  40. &lt;closure&gt;
  41. > (funcall *lex*)
  42. FLT
  43. 1.0
  44. > (funcall *lex*)
  45. INT
  46. 12
  47. > (funcall *lex*)
  48. NAME
  49. "fred"
  50. > (funcall *lex*)
  51. FLT
  52. 1.0229999999999997E46
  53. > (funcall *lex*)
  54. NIL
  55. NIL
  56. </pre></code>
  57. <p>
  58. You can also write this lexer using the <code>:flex-compatible</code> option, in which case you can write the <em>int</em> and <em>flt</em> rules in any order.
  59. </p>
  60. <code><pre>
  61. (deflexer test-lexer
  62. :flex-compatible
  63. ("[0-9]+"
  64. (return (values 'int (int %0))))
  65. ("[0-9]+([.][0-9]+([Ee][0-9]+)?)"
  66. (return (values 'flt (num %0))))
  67. ("[:space:]+")
  68. )
  69. </pre></code>
  70. <hr>
  71. <address><a href="mailto:mparker762@hotmail.com">Michael Parker</a></address>
  72. </body>
  73. </html>