【发布时间】:2019-08-27 19:47:56
【问题描述】:
说,我有以下 EBNF:
document = content , { content } ;
content = hello world | answer | space ;
hello world = "hello" , space , "world" ;
answer = "42" ;
space = " " ;
这让我可以解析如下内容:
hello world 42
现在我想用块注释来扩展这个语法。我怎样才能正确地做到这一点?
如果我开始简单:
document = content , { content } ;
content = hello world | answer | space | comment;
hello world = "hello" , space , "world" ;
answer = "42" ;
space = " " ;
comment = "/*" , ?any character? , "*/" ;
我无法解析:
Hello /* I'm the taxman! */ World 42
如果我从上面的特殊情况进一步扩展语法,它会变得丑陋,但会解析。
document = content , { content } ;
content = hello world | answer | space | comment;
hello world = "hello" , { comment } , space , { comment } , "world" ;
answer = "42" ;
space = " " ;
comment = "/*" , ?any character? , "*/" ;
但我仍然无法解析如下内容:
Hel/*p! I need somebody. Help! Not just anybody... */lo World 42
我将如何使用 EBNF 语法来做到这一点?还是根本不可能?
【问题讨论】:
-
你用什么工具来测试你的语法?
-
@whydoubt 除了我的脑袋。
标签: parsing comments context-free-grammar ebnf