为什么我的 hylang 程序解析失败?
Why does my hylang program fail at parsing?
我写了一个解析日志的小 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
应该使用 "w" 和 "r" 而不是 'w' 和 'r'
制作字符串需要使用双引号。
在 lisps 中,单引号用于与制作字符串完全不同的东西 - "quoting" 下一种形式。解析 lisp 代码时,像 'a
这样的表达式会被转换为 (quote a)
。同样,'(hello)
变为 (quote (hello))
。请注意只有一个 ' 标记,因为它总是包含下一个表达式。 quote 形式允许您 "turn off" 评估单个表达式。查找 lisp 引用 - 它非常有用,并且对于该语言的一些更强大的功能很重要。
我写了一个解析日志的小 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
应该使用 "w" 和 "r" 而不是 'w' 和 'r'
制作字符串需要使用双引号。
在 lisps 中,单引号用于与制作字符串完全不同的东西 - "quoting" 下一种形式。解析 lisp 代码时,像 'a
这样的表达式会被转换为 (quote a)
。同样,'(hello)
变为 (quote (hello))
。请注意只有一个 ' 标记,因为它总是包含下一个表达式。 quote 形式允许您 "turn off" 评估单个表达式。查找 lisp 引用 - 它非常有用,并且对于该语言的一些更强大的功能很重要。