我编写了一个解析日志的小 hylang 程序。但是,当我尝试评估它时,出现以下错误:
File "", line 8, column 38
(setv rule (.next irule))))))) ^ LexException: Ran into a RPAREN where it wasn't expected.
有问题的函数(单独计算时也会出错)如下:
(defmain [&rest args]
(setv inname (get args 1))
(setv outname (get args 2))
(with [ifile (open inname 'r')]
(with [ofile (open outname 'w')]
(setv tl (Tableline))
(setv irule (iter (.get-rule tl)))
(setv rule (.next irule))
(for [line ifile]
(setv match (re.match (get rule 0) line))
(when match
(for [(, group-num attr-data) (enumerate (get rule 1))]
(setattr tl (get attr-data 1) (apply (get attr-data 0)
[(.group match (inc group-num))])))
(if (. tl ready) (write ofile (str tl)))
(setv rule (.next irule)))))))
据我所知,它是平衡的表达,所有括号都在各自的位置。为什么词法分析器会失败?
我的程序全文在这里:pastebin
最佳答案
您需要使用双引号来制作字符串。
在 lisps 中,单引号的用途与制作字符串完全不同——“引用”下一个形式。解析 lisp 代码时,像 'a
这样的表达式被转换为 (quote a)
。同样,'(hello)
变为 (quote (hello))
。请注意只有一个 ' 标记,因为它总是包含下一个表达式。引用形式允许您“关闭”单个表达式的评估。查找 lisp 引用 - 它非常有用,并且对于该语言的一些更强大的功能很重要。
关于lisp - 为什么我的 hylang 程序解析失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42811335/