我是 ANTLR 的初学者,我正在通过一个例子来学习它。我使用 C 作为我的目标语言。该示例是取自此问题的 Scheme R5RS 语法文件,稍作修改(重命名语法名称并添加一些选项,语法规范不变)。
antlr 生成了词法分析器和解析器,我用一个测试编译它main()
,我只是做一些初始化并简单地调用解析器。当使用一段方案代码运行测试程序时,解析器检测到一些语法错误(这不应该发生!)
main
test.c 中的函数
#include <stdio.h>
#include "r5rsLexer.h"
#include "r5rsParser.h"
int main(int argc, char *argv[])
{
pANTLR3_UINT8 fname;
pANTLR3_INPUT_STREAM input;
pr5rsLexer lexer;
pANTLR3_COMMON_TOKEN_STREAM tstream;
pr5rsParser parser;
r5rsParser_parse_return parse_return;
if (argc != 2)
{
ANTLR3_FPRINTF(stderr, "usage: %s file\n", argv[0]);
exit(1);
}
fname = (pANTLR3_UINT8)argv[1];
input = antlr3FileStreamNew(fname, ANTLR3_ENC_8BIT);
if (!input)
{
ANTLR3_FPRINTF(stderr, "open file stream failed\n");
exit(1);
}
lexer = r5rsLexerNew(input);
if (!lexer)
{
ANTLR3_FPRINTF(stderr, "new lexer failed\n");
exit(1);
}
tstream =
antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT, TOKENSOURCE(lexer));
if (!tstream)
{
ANTLR3_FPRINTF(stderr, "open token stream failed\n");
exit(1);
}
parser = r5rsParserNew(tstream);
if (!parser)
{
ANTLR3_FPRINTF(stderr, "new parser failed\n");
exit(1);
}
parse_return = parser->parse(parser);
printf("succeed!\n");
return 0;
}
test.scm 中的方案代码:
(define-syntax should-be
(syntax-rules ()
((_ test-id value expression)
(let ((return-value expression))
(if (not (equal? return-value value))
(for-each (lambda (v) (display v))
`("Failure: " test-id ", expected '"
value "', got '" ,return-value "'." #\newline))
(for-each (lambda (v) (display v))
'("Passed: " test-id #\newline)))))))
(should-be 1.1 0
(let ((cont #f))
(letrec ((x (call-with-current-continuation (lambda (c) (set! cont c) 0)))
(y (call-with-current-continuation (lambda (c) (set! cont c) 0))))
(if cont
(let ((c cont))
(set! cont #f)
(set! x 1)
(set! y 1)
(c 0))
(+ x y)))))
终端输出:
$> ls
r5rs.g test.c test.scm
$> antlr3 r5rs.g
$> ls
r5rs.g r5rs.tokens r5rsLexer.c r5rsLexer.h r5rsParser.c r5rsParser.h test.c test.scm
$> gcc -o test test.c r5rsLexer.c r5rsParser.c -lantlr3c
$> ./test test.scm
test.scm(1) : error 4 : Unexpected token, at offset 0
near [Index: 1 (Start: 154513905-Stop: 154513917) ='define-syntax', type<5> Line:1
LinePos:0]
: unexpected input...
expected one of : <EOR>
test.scm(2) : error 4 : Unexpected token, at offset 3
near [Index: 8 (Start: 154513932-Stop: 154513943) ='syntax-rules', type<7> Line: 2
LinePos:3]
: unexpected input...
expected one of : <EOR>
test.scm(2) : error 4 : Unexpected token, at offset 17
near [Index: 11 (Start: 154513946-Stop: 154513946) =')', type<82> Line: 2 LinePos:17]
: unexpected input...
expected one of : <EOR>
test.scm(2) : error 4 : Unexpected token, at offset 17
near [Index: 11 (Start: 154513946-Stop: 154513946) =')', type<82> Line: 2 LinePos:17]
: unexpected input...
expected one of : <EOR>
我已经阅读了语法规范,它是正确的。我无法弄清楚问题出在哪里......有人可以帮忙吗?谢谢!
======================回复=========================
按照 and 的语法规则pattern
,template
我下到了下面的代码片段。我认为解析将template
与之匹配并失败,因为template
没有quasiquote
替代方案。
`("Failure: " test-id ", expected '" value "', got '" ,return-value "'." #\newline)
我相信 for 的语法规则template
正确地遵循了 R5RS 规范,并且该代码被其他 R5Rs 方案实现所接受(我在 scheme48 和 guile 中对其进行了测试)。这怎么可能发生?
我想我的分析一定有问题……