BNF处理转义序列

时间:2010-06-04 00:37:48

标签: parsing escaping grammar bnf

我使用这个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"

1 个答案:

答案 0 :(得分:1)

将str定义为:

str =  ( strset strescape ) *;

strescape = { \\ } {\" } ;