Построить множество follow(1) на основе заданной ll-грамматики - Lisp

Узнай цену своей работы

Формулировка задачи:

Есть относительно понятная реализация здесь http://inst.eecs.berkeley.edu/~cs164/software/source/firstfoll.cl Код не запускается в силисп. Помогите пожалуйста разобраться, где его запустить и как вывести follow множество

Решение задачи: «Построить множество follow(1) на основе заданной ll-грамматики»

textual
Листинг программы
  1. ;; computing First and Follow sets
  2.  
  3. ;;Here is a sample grammar
  4.  
  5. ;; We need a way to represent a grammar in Lisp. We could do it in a
  6. ;; variety of different ways, for example setting up a data structure
  7. ;; of some kind of collection for the rules (list, hashtable, array),
  8. ;; some kind of indexing or selection so we can step through the
  9. ;; rules, some kind of constructors so we can construct the grammar
  10. ;; and its rules in the first place, some selectors on rule parts so
  11. ;; we can get the left-hand and right-hand sides, a selector so that
  12. ;; we can step through components in the right-hand side, etc.  It
  13. ;; turns out that if we use lisp lists as shown below, we can make
  14. ;; very neat use of built-in lisp programs to manipulate
  15. ;; grammars... like car, cdr, mapc, list, union. Programs to print
  16. ;; grammars are also free; and formatted output is also pretty
  17. ;; standard.  If we constructed a grammar data-type (and in some
  18. ;; languages we could mention we would have to do so), it would take
  19. ;; us a page of code, and all of our programs would have to use these
  20. ;; structures.  It would make the programs relatively opaque: you
  21. ;; would have to be fully educated in what we had done to make sense
  22. ;; of them.
  23.  
  24. ;; If this appears to you to constitute an argument against abstract
  25. ;; data types, you are right.  It is not a mathematical, legal, or ethical
  26. ;; argument. In fact it is an argument based on TASTE, and reasonable
  27. ;; people can disagree with me about what I've done.  I might even
  28. ;; change my mind about some of the issues, but at the moment I think
  29. ;; I'm right.  ( :) .
  30.  
  31. ;;
  32.  
  33. ;; Here is what we do to define a grammar.  The 2nd element in each
  34. ;; rule is the symbol "->".  This is just for visual appearance.  We
  35. ;; do not allow any of the extended notations like | or *.  If we did,
  36. ;; we could write an extended-grammar to vanilla-grammar translator.
  37.  
  38. (defparameter  ; grammar 3.12
  39.     g
  40.     '((Z -> d)
  41.       (Z -> X Y Z)
  42.       (Y -> )
  43.       (Y -> c)
  44.       (X -> Y)
  45.       (X -> a)))
  46.  
  47. (defparameter lect7
  48.     '((E -> T X)
  49.       (T -> \( E \) )
  50.       (T -> int Y)
  51.       (X -> + T)
  52.       (X -> )
  53.       (Y -> * T)
  54.       (Y -> )))
  55.      
  56.  
  57.  
  58. ;; selecting stuff from a rule
  59.  
  60. (defun rhs(rule)(cddr rule))        ;right hand side
  61. (defun lhs(rule)(car rule))     ;left hand side
  62.  
  63. ;; Return a list of the non-terminal symbols of g by making a list of
  64. ;; all the symbols on the lhs. Remove duplicates from that list.
  65.  
  66. (defun non-terminals(g) (remove-duplicates (mapcar #'lhs g)))
  67.  
  68. ;; Return a list of the terminal symbols of the grammar g by computing
  69. ;; the union of all the symbols on the rhs of all the rules of g.
  70. ;; Eliminate the non-terminal symbols.
  71.  
  72. (defun terminals(g)
  73.   (set-difference (reduce #'union (mapcar #'rhs g))
  74.           (non-terminals g) ))
  75.  
  76. ;; a diversion to discuss MACROS.
  77.  
  78. ;; We repeatedly need to do (setf foo (union foo z)). No big deal,
  79. ;; except that foo is sometimes a big expression, and we don't really
  80. ;; want to write it out twice. We might make a mistake and write it
  81. ;; differently the second time. Let's extend lisp with a macro,
  82. ;; addinto, so that we can write (addinto foo z). Like foo+=z in C
  83. ;; except we are using union instead of +.  We want to take a place
  84. ;; and stuff and produce a new expression that looks like (setf place
  85. ;; (union place stuff)).  But of course we must do sometime to make
  86. ;; place and stuff parameters.
  87. ;;
  88. ;; (defun addinto(p s)(setf p (union p s)))
  89. ;;  -- will not work. (addinto x y) sets p to (union x y).
  90.  
  91. ;; (defun addinto (p s) (list 'setf p (list 'union p s)))
  92. ;; --- will not work.  (addinto x y) returns the list (setf x (union x y))
  93.  
  94. ;; this second definition is closer, because if we
  95. ;; evaluate the returned list, we win. In fact
  96. ;; (defmacro addinto (p s) (list 'setf p (list 'union p s)))
  97. ;; does what we want.
  98.  
  99. ;; The experienced lisp macro-writer is more likely to
  100. ;; use the "backquote" syntax, for the IDENTICAL result:
  101.  
  102. (defmacro addinto(place stuff)
  103.   `(setf ,place (union ,place ,stuff)))
  104.  
  105. ;; note that backquote is just like quote except that items with a
  106. ;; comma in front of them are replaced by their values.
  107. ;; There are other tricks, including ,@ which works like this:
  108.  
  109. ;; (setf x '(a b c))
  110. ;; `(x ,x)   ==> (x (a b c))
  111. ;; `(x ,@ x) ==> (x a b c)    ;,@ works by appending
  112.  
  113. ;; Back to MACROS.
  114.  
  115. ;; In fact, what we want to say is written in the textbook as
  116. ;; something like Follow[x] := Follow[x] U First[y].  where Follow might
  117. ;; be First or some other set associated with the symbol x.
  118.  
  119. ;; We have chosen to represent First etc. as hashtables, which means
  120. ;; that to get First[X] we could write (gethash X Follow).  But maybe
  121. ;; we will weigh in on the side of abstraction this time, and vote
  122. ;; against explicitly writing gethash all over. Why?
  123. ;; That commits us to always using the hashtable mechanism.  Maybe we
  124. ;; will come up with another idea for the table datatype that
  125. ;; associates a symbol like X with several properties like First[X]
  126. ;; and Follow[X], and we would like to substitute our implementation
  127. ;; for this one, simply.
  128.  
  129. ;; Here's a useful Macro trick, then, that abstracts out the gethash:
  130.  
  131. ;; Like First[x] in textbook, where x is a symbol, we allow (First x)
  132.  
  133. (defmacro First (x)
  134.   `(gethash ,x First))
  135.  
  136. ;; Like Follow[x] in textbook, where x is a symbol.
  137.  
  138. (defmacro Follow(x)
  139.   `(gethash ,x Follow))
  140.  
  141. ;; We can now write (Follow x) instead of (gethash x Follow).
  142.  
  143. ;; Lisp note: More significantly, using a macro allows us to write
  144. ;; (setf (Follow x) ...), which would not be possible if we merely
  145. ;; defined functions: (defun Follow(x)(gethash x Follow)).  
  146. ;; Also, we expand this macro in the lexical environment of
  147. ;; the program. Thus the names First and Follow are the local
  148. ;; names, used in the program where the macros are used.
  149.  
  150. ;; Combining these macros allows us to write things like
  151. ;;(addinto (Follow x) (First y))
  152.  
  153. ;;; end of MACRO discussion

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

9   голосов , оценка 4.333 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы