ANTLR BNF 语法符号中 epsilon 的等价物是什么?
在利用 ANTLR 3.3 期间,我正在更改当前语法以支持不带括号的输入。这是我的语法的第一个版本:
grammar PropLogic;
NOT : '!' ;
OR : '+' ;
AND : '.' ;
IMPLIES : '->' ;
SYMBOLS : ('a'..'z') | '~' ;
OP : '(' ;
CP : ')' ;
prog : formula EOF ;
formula : NOT formula
| OP formula( AND formula CP | OR formula CP | IMPLIES formula CP)
| SYMBOLS ;
WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ { $channel = HIDDEN; } ;
然后我以这种方式更改它以支持适当的功能:
grammar PropLogic;
NOT : '!' ;
OR : '+' ;
AND : '.' ;
IMPLIES : '->' ;
SYMBOL : ('a'..'z') | '~' ;
OP : '(' ;
CP : ')' ;
EM : '' ;
prog : formula EOF ;
formula : OP formula( AND formula CP | OR formula CP | IMPLIES formula CP)
| ( NOT formula | SYMBOL )( AND formula | OR formula | IMPLIES formula | EM ) ;
WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ { $channel = HIDDEN; } ;
但是我遇到了以下错误:
error<100>: syntax error: invalid char literal: ''
error<100>: syntax error: invalid char literal: ''
有人知道我如何克服这个错误吗?
During taking advantage of ANTLR 3.3, I'm changing the current grammar to support inputs without parenthesis too. Here's the first version of my grammar :
grammar PropLogic;
NOT : '!' ;
OR : '+' ;
AND : '.' ;
IMPLIES : '->' ;
SYMBOLS : ('a'..'z') | '~' ;
OP : '(' ;
CP : ')' ;
prog : formula EOF ;
formula : NOT formula
| OP formula( AND formula CP | OR formula CP | IMPLIES formula CP)
| SYMBOLS ;
WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ { $channel = HIDDEN; } ;
Then I changed it this way to support the appropriate features :
grammar PropLogic;
NOT : '!' ;
OR : '+' ;
AND : '.' ;
IMPLIES : '->' ;
SYMBOL : ('a'..'z') | '~' ;
OP : '(' ;
CP : ')' ;
EM : '' ;
prog : formula EOF ;
formula : OP formula( AND formula CP | OR formula CP | IMPLIES formula CP)
| ( NOT formula | SYMBOL )( AND formula | OR formula | IMPLIES formula | EM ) ;
WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ { $channel = HIDDEN; } ;
But I've been faced with following error :
error<100>: syntax error: invalid char literal: ''
error<100>: syntax error: invalid char literal: ''
Does anybody know that how can I overcome this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
EM
标记:无效:您无法匹配词法分析器规则中的空字符串。
要匹配 epsilon (什么也没有),你应该这样做:
当然,注释
/* epsilon */
可以安全地删除。请注意,当您按照当前语法执行此操作时,ANTLR 会抱怨可以使用多个替代项来匹配规则。这是因为你的语法不明确。
Your
EM
token:is invalid: you can't match an empty string in lexer rules.
To match epsilon (nothing), you should do:
Of course, the comment
/* epsilon */
can safely be removed.Note that when you do it like that in your current grammar, ANTLR will complain that there can be rules matched using multiple alternatives. This is because your grammar is ambiguous.
我不是 ANTLR 专家,但您可以尝试:
如果您想要运算符的传统优先级,这不会成功,但这是另一个问题。
编辑:OP提高了赌注;他也想要优先权。我会在半路上遇见他,因为那不是一部分
原来的问题。我已经为使 IMPLIES 的语法添加了优先级
优先级低于其他运算符,并将其留给 OP 来弄清楚如何完成其余的操作。
OP还问,“如何将 (!p 或 q ) 转换为 p -> q”。我认为他应该
作为一个单独的问题提出了这个问题。不过,我已经来了。
他需要做的是在树上行走,寻找他没有的模式
喜欢,然后将树更改为他所做的树,然后漂亮地打印答案。
使用 ANTLR 可以完成这一切,这是部分原因
它很受欢迎。
实际上,按程序遍历树并检查节点
类型,并且拼接旧节点并拼接新节点是可行的,但是皇家PitA。
特别是如果您想对大量转换执行此操作。
更有效的方法是使用
程序转换系统,允许表达表面语法模式以进行匹配和替换。程序转换系统当然包括解析机器和更强大的系统,让您(实际上坚持)您定义
前面的语法就像你的 ANTLR 一样。
我们的DMS Software Reengineering Toolkit就是这样一个程序转换工具,并且具有适当定义的命题语法,
以下DMS转换规则将执行OP的附加请求:
“...”是“域符号”,例如来自proplogic域的表面语法,“\”是元转义符,
因此“\p”和“\q”代表proplogic语法中的任意项。请注意,应用时规则必须达到“跨”优先级,因为“NOT \p OR \q”不是公式,而“\p IMPLIES \q”是; DMS 负责处理所有这些(“公式 -> 公式”表示法是 DMS 知道要做什么的方式)。该规则进行树到树重写。生成的树可以通过 DMS 进行漂亮打印。
您可以看到非常相似的完整示例,例如 传统代数和重写规则的语法简化代数方程。
I'm not an ANTLR expert, but you might try:
If you want traditional precedence of operators this won't do the trick, but that's another issue.
EDIT: OP raised the ante; he wants precedence too. I'll meet him halfway, since it wasn't part
of the orginal question. I've added precedence to the grammar that makes IMPLIES
the lower precedence than other operators, and leave it to OP to figure out how to do the rest.
OP additionally asked, "how to convert (!p or q ) into p -> q". I think he should
have asked this as a separate question. However, I'm already here.
What he needs to do is walk the tree, looking for the pattern he doesn't
like, and change the tree into one he does, and then prettyprint the answer.
It is possible to do all this with ANTLR, which is part of the reason
it is popular.
As a practical matter, procedurally walking the tree and checking the node
types, and splicing out old nodes and splicing in new is doable, but a royal PitA.
Especially if you want to do this for lots of transformations.
A more effective way to do this is to use a
program transformation system, which allows surface syntax patterns to be expressed for matching and replacement. Program transformation systems of course include parsing machinery and more powerful ones let you (and indeed insist) that you define
a grammar up front much as you for ANTLR.
Our DMS Software Reengineering Toolkit is such a program transformation tool, and with a suitably defined grammar for propositions,
the following DMS transformation rule would carry out OP's additional request:
The " ... " is "domain notation", e.g, surface syntax from the proplogic domain, the "\" are meta-escapes,
so "\p" and "\q" represent any arbitrary term from the proplogic grammar. Notice the rule has to reach "across" precedence levels when being applied, as "NOT \p OR \q" isn't a formula and "\p IMPLIES \q" is; DMS takes care of all this (the "formula -> formula" notation is how DMS knows what to do). This rule does a tree-to-tree rewrite. The resulting tree can be prettyprinted by DMS.
You can see a complete example of something very similar, e.g., a grammar for conventional algebra and rewrite rule to simplify algebraic equations.