【问题标题】:BNF to handle escape sequenceBNF 处理转义序列
【发布时间】:2010-06-04 00:37:47
【问题描述】:

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

【问题讨论】:

    标签: parsing escaping grammar bnf


    【解决方案1】:

    定义str为:

    str =  ( strset strescape ) *;
    

    strescape = { \\ } {\" } ;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      相关资源
      最近更新 更多