我试图为C的子集编写一个解析器。
在这种简单的(进一步简化的)语法上,树顶的行为很难分析。
grammar Shyc
rule functionDef
type space identifier '(' ')' bloc
end
rule type
'int'
end
rule bloc
'{' '}'
end
rule identifier
[a-zA-Z] [a-zA-Z_]*
end
rule space
[\s]+
end
end我的测试用例是"int main(){}“
来自树梢的错误消息是:
error at line 1, column 9
failure reason : Expected [a-zA-Z_] at line 1, column 9 (byte 9) after
compiler.rb:25:in `parse': Parse error (RuntimeError)
from compiler.rb:73:in `<main>'enter 因此,问题在于标识符规则.
树顶版本: 1.5.3和Ruby2.1.1
知道吗?
发布于 2014-07-12 20:57:12
问题是,我的测试用例在一个单独的文件中,最后是一个补充的行尾\n,这里测试的语法没有指定如何使用它。
这是解决问题的代码。正如Treetop邮件列表中所讨论的那样,这个错误是奇怪的,而且在某种程度上是误导性的,但是通常很难自动发送清楚的消息。
grammar Shyc
rule functionDef
type space identifier '(' ')' bloc space?
end
rule type
'int'
end
rule bloc
'{' '}'
end
rule identifier
[a-zA-Z] [a-zA-Z_]*
end
rule space
[\s\n]+
end结束
https://stackoverflow.com/questions/24677762
复制相似问题