make_lts_rewrite.scm 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;; ;;;
  3. ;;; Language Technologies Institute ;;;
  4. ;;; Carnegie Mellon University ;;;
  5. ;;; Copyright (c) 2002 ;;;
  6. ;;; All Rights Reserved. ;;;
  7. ;;; ;;;
  8. ;;; Permission is hereby granted, free of charge, to use and distribute ;;;
  9. ;;; this software and its documentation without restriction, including ;;;
  10. ;;; without limitation the rights to use, copy, modify, merge, publish, ;;;
  11. ;;; distribute, sublicense, and/or sell copies of this work, and to ;;;
  12. ;;; permit persons to whom this work is furnished to do so, subject to ;;;
  13. ;;; the following conditions: ;;;
  14. ;;; 1. The code must retain the above copyright notice, this list of ;;;
  15. ;;; conditions and the following disclaimer. ;;;
  16. ;;; 2. Any modifications must be clearly marked as such. ;;;
  17. ;;; 3. Original authors' names are not deleted. ;;;
  18. ;;; 4. The authors' names are not used to endorse or promote products ;;;
  19. ;;; derived from this software without specific prior written ;;;
  20. ;;; permission. ;;;
  21. ;;; ;;;
  22. ;;; CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK ;;;
  23. ;;; DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ;;;
  24. ;;; ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT ;;;
  25. ;;; SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE ;;;
  26. ;;; FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES ;;;
  27. ;;; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN ;;;
  28. ;;; AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ;;;
  29. ;;; ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF ;;;
  30. ;;; THIS SOFTWARE. ;;;
  31. ;;; ;;;
  32. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  33. ;;; Author: Alan W Black (awb@cs.cmu.edu) ;;;
  34. ;;; Date: August 2002 ;;;
  35. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  36. ;;; ;;;
  37. ;;; Generate a C compilable lts rewrite rules. ;;;
  38. ;;; ;;;
  39. ;;; From CMU Flite ;;;
  40. ;;; ;;;
  41. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  42. (define (lts_norm_rule rule)
  43. (let (q w)
  44. (mapcar
  45. (lambda (l)
  46. (cond
  47. ((string-equal l "[")
  48. (set! q (list w))
  49. (set! w nil))
  50. ((string-equal l "]")
  51. (set! q (cons (reverse w) q))
  52. (set! w nil))
  53. ((string-equal l "=")
  54. (set! q (cons (reverse w) q))
  55. (set! w nil))
  56. (t
  57. (set! w (cons l w)))))
  58. rule)
  59. (set! xxx (list
  60. (car (cddr q)) ;; reversed left hand side of rules
  61. (car (cdr q)) ;; middle condition
  62. (flip_stars (car q)) ;; RHS with * reverse
  63. (reverse w))) ;; re-write output
  64. (format t "%l %l\n" rule xxx)
  65. xxx))
  66. (define (flip_stars q)
  67. ;; We want klene star to appear before the object
  68. (cond
  69. ((null q) q)
  70. ((and (cdr q)
  71. (string-equal (cadr q) "*"))
  72. (cons (cadr q)
  73. (cons (car q)
  74. (flip_stars (cddr q)))))
  75. (t
  76. (cons (car q) (flip_stars (cdr q))))))
  77. (define (ltsrewritestoC name fname odir)
  78. "(ltsrewritestoC name idir odir)"
  79. (let
  80. ((ofde (fopen (path-append odir (string-append name ".c")) "w"))
  81. (ofdh (fopen (path-append odir (string-append name ".h")) "w"))
  82. (rules (car (load fname t)))
  83. (ifd))
  84. (format ofde "/*******************************************************/\n")
  85. (format ofde "/** Autogenerated lts rewrite rules for %s */\n" name)
  86. (format ofde "/** from %s */\n" name)
  87. (format ofde "/*******************************************************/\n")
  88. (format ofde "\n")
  89. (format ofde "#include \"cst_string.h\"\n")
  90. (format ofde "#include \"cst_val.h\"\n")
  91. (format ofde "#include \"cst_lts_rewrites.h\"\n")
  92. (format ofdh "extern const cst_lts_rewrites %s;\n\n" name)
  93. (cellstovals
  94. (format nil "%s_lts_sets" name)
  95. (car (cdr (cdr rules)))
  96. ofde)
  97. (set! eoc_sets cells_count)
  98. (cellstovals
  99. (format nil "%s_lts_rules" name)
  100. (mapcar
  101. lts_norm_rule
  102. (car (cdr (cdr (cdr rules)))))
  103. ofde)
  104. (if (equal? eoc_sets 0)
  105. (format ofde "#define %s_lts_sets 0\n" name)
  106. (format ofde "#define %s_lts_sets &%s_lts_sets_%04d\n"
  107. name name eoc_sets))
  108. (format ofde "#define %s_lts_rules &%s_lts_rules_%04d\n"
  109. name name cells_count)
  110. (format ofde "\n")
  111. (format ofde "const cst_lts_rewrites %s = {\n" name)
  112. (format ofde " \"%s\",\n" name)
  113. (format ofde " %s_lts_sets,\n" name)
  114. (format ofde " %s_lts_rules,\n" name)
  115. (format ofde "};\n")
  116. (format ofde "\n")
  117. (fclose ofde)
  118. (fclose ofdh)
  119. ))
  120. (provide 'make_lts_rewrite)