5

我正在使用 Spirit 2.4,我想解析这样的结构:

文本{text_field};

关键是在 text_field 中是一个带有符号“{”、“}”和“\”的转义字符串。我想为此使用 qi 创建一个解析器。我一直在尝试这个:

using boost::spirit::standard::char_;
using boost::spirit::standard::string;
using qi::lexeme;
using qi::lit;

qi::rule< IteratorT, std::string(), ascii::space_type > text;
qi::rule< IteratorT, std::string(), ascii::space_type > content;
qi::rule< IteratorT, std::string(), ascii::space_type > escChar;


text %= 
  lit( "Text" ) >> '{' >>
    content >>
  "};"
  ;

content %= lexeme[ +( +(char_ - ( lit( '\\' ) | '}' ) )  >> escChar ) ];

escChar %= string( "\\\\" ) 
  | string( "\\{" ) 
  | string( "\\}" );

但甚至不编译。任何的想法?

4

1 回答 1

8

你的语法可以写成:

qi::rule< IteratorT, std::string(), ascii::space_type > text; 
qi::rule< IteratorT, std::string() > content;   
qi::rule< IteratorT, char() > escChar;   

text = "Text{" >> content >> "};";  
content = +(~char_('}') | escChar); 
escChar = '\\' >> char_("\\{}");

IE

  • 文本Text{后面是内容,然后是}

  • content至少是字符(但不是})或 escChar 的一个实例

  • escChar是单个转义的\\, {, 或}

请注意,escChar规则现在返回单个字符并丢弃转义的\\. 我不确定这是否是您需要的。此外,我删除了内容escChar规则的船长,这允许省略lexeme[](没有船长的规则就像一个隐含的词位)。

于 2010-10-27T01:46:50.090 回答