我使用这个 BNF 来解析我的脚本:

{identset} = {ASCII} - {"\{\}};     //<--all ascii charset except '\"' '{' and '}'
{strset}   = {ASCII} - {"};
ident      = {identset}*;
str        = {strset}*;
node     ::= ident "{" nodes "}" |  //<--entry point
             "\"" str "\"" |
             ident;
nodes    ::= node nodes |
             node;

它可以将以下文本正确解析为树状结构
doc {
    title { "some title goes here" }
    refcode { "SDS-1" }
    rev { "1.0" }
    revdate { "04062010" }
    body {
        "this is the body of the document
         all text should go here"
        chapter { "some inline section" }
        "text again"
    }
}

我的问题是, 如何处理字符串 文字中的转义序列:
"some text of \"quotation\" should escape"

最佳答案

定义 str 为:

str =  ( strset strescape ) *;


strescape = { \\ } {\" } ;

关于parsing - BNF 处理转义序列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2970775/

10-09 15:54