使用复合语法进行语法分析时出现Boost.spirit分段错误

时间:2017-06-30 13:26:56

标签: c++ parsing boost boost-spirit

尝试为.fnt文件编写解析器:

int main()
{
    const auto word = qi::lexeme [ + qi::alnum ];
    const auto literal = qi::lexeme[ '"' >> + ( qi::char_ - '"' ) >> '"' ];
    const auto value = literal | qi::double_;
    const auto pair = word >> '=' >> value;
    const auto line = word >> ( + pair ) >> qi::eol;
    const auto document = + line;

    std::ifstream in( "input.fnt" );
    in.unsetf( std::ios::skipws );
    boost::spirit::istream_iterator begin(in);
    boost::spirit::istream_iterator end;

    qi::parse( begin, end, document );
}

执行失败,在boost :: spirit的深层内出现 Segmentation fault 错误。但是当我将语法改为不是由中间语法(规则?)组成时 - 它就成功了。

Internal Boost::Spirit code segfaults when parsing a composite grammar处说表达式模板保留对临时值的内部引用,但所有语法在 qi :: parse()调用期间确实存在。 可能是什么问题呢?如何将复合语法分解为更有意义的部分?

P.S。将 const auto 更改为 #define 并删除 = 符号时 - 它会成功:)( qi :: parse()返回 false ,但至少返回)。

1 个答案:

答案 0 :(得分:1)

除了重复之外,您可以获得一些免费的建议:Assigning parsers to auto variables

让我们解析简单的FNT样本:

info face="Arial" size=32 bold=0 italic=0 charset="" unicode=1 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=1,1 outline=0
common lineHeight=32 base=26 scaleW=256 scaleH=256 pages=0 packed=0 alphaChnl=1 redChnl=0 greenChnl=0 blueChnl=0
chars count=0

注意:

  1. 使用船长来允许无关紧要的空白。在这种情况下,请使用qi::blank,因为它会使eol显着
  2. 通过"property=0,0,0"
  3. 允许多值(double % ','样式)
  4. 将BOOST_SPIRIT_DEBUG与规则一起使用
  5. 为隐式词位(Boost spirit skipper issues
  6. 声明无船长规则

    <强> Live On Coliru

    #define BOOST_SPIRIT_DEBUG
    #include <boost/spirit/include/qi.hpp>
    #include <fstream>
    
    namespace qi = boost::spirit::qi;
    
    int main() {
        using It = boost::spirit::istream_iterator;
        qi::rule<It> 
            word    = +qi::alnum,
            literal = '"' >> *(qi::char_ - '"') >> '"';
        qi::rule<It, std::string()> 
            value   = qi::raw[literal | qi::double_ % ','];
    
        BOOST_SPIRIT_DEBUG_NODES((word)(literal)(value))
    
        const auto pair     = qi::copy(word >> '=' >> value);
        const auto line     = qi::copy(word >> ( + pair ) >> qi::eol);
        const auto document = qi::copy(+ line);
    
        std::ifstream in("input.fnt");
        in.unsetf(std::ios::skipws);
        It begin(in), end;
    
        bool ok = qi::phrase_parse(begin, end, document, qi::blank);
        std::cout << std::boolalpha << ok << '\n';
    }
    

    打印:

    true
    

    使用调试信息:

    <word>
      <try>info face="Arial" si</try>
      <success> face="Arial" size=3</success>
      <attributes>[]</attributes>
    </word>
    <word>
      <try>face="Arial" size=32</try>
      <success>="Arial" size=32 bol</success>
      <attributes>[]</attributes>
    </word>
    <value>
      <try>"Arial" size=32 bold</try>
      <literal>
        <try>"Arial" size=32 bold</try>
        <success> size=32 bold=0 ital</success>
        <attributes>[]</attributes>
      </literal>
      <success> size=32 bold=0 ital</success>
      <attributes>[[", A, r, i, a, l, "]]</attributes>
    </value>
    <word>
      <try>size=32 bold=0 itali</try>
      <success>=32 bold=0 italic=0 </success>
      <attributes>[]</attributes>
    </word>
    <value>
      <try>32 bold=0 italic=0 c</try>
      <literal>
        <try>32 bold=0 italic=0 c</try>
        <fail/>
      </literal>
      <success> bold=0 italic=0 cha</success>
      <attributes>[[3, 2]]</attributes>
    </value>
    <word>
      <try>bold=0 italic=0 char</try>
      <success>=0 italic=0 charset=</success>
      <attributes>[]</attributes>
    </word>
    <value>
      <try>0 italic=0 charset="</try>
      <literal>
        <try>0 italic=0 charset="</try>
        <fail/>
      </literal>
      <success> italic=0 charset=""</success>
      <attributes>[[0]]</attributes>
    </value>
    <word>
      <try>italic=0 charset="" </try>
      <success>=0 charset="" unicod</success>
      <attributes>[]</attributes>
    </word>
    <value>
      <try>0 charset="" unicode</try>
      <literal>
        <try>0 charset="" unicode</try>
        <fail/>
      </literal>
      <success> charset="" unicode=</success>
      <attributes>[[0]]</attributes>
    </value>
    <word>
      <try>charset="" unicode=1</try>
      <success>="" unicode=1 stretc</success>
      <attributes>[]</attributes>
    </word>
    <value>
      <try>"" unicode=1 stretch</try>
      <literal>
        <try>"" unicode=1 stretch</try>
        <success> unicode=1 stretchH=</success>
        <attributes>[]</attributes>
      </literal>
      <success> unicode=1 stretchH=</success>
      <attributes>[[", "]]</attributes>
    </value>
    <word>
      <try>unicode=1 stretchH=1</try>
      <success>=1 stretchH=100 smoo</success>
      <attributes>[]</attributes>
    </word>
    <value>
      <try>1 stretchH=100 smoot</try>
      <literal>
        <try>1 stretchH=100 smoot</try>
        <fail/>
      </literal>
      <success> stretchH=100 smooth</success>
      <attributes>[[1]]</attributes>
    </value>
    <word>
      <try>stretchH=100 smooth=</try>
      <success>=100 smooth=1 aa=1 p</success>
      <attributes>[]</attributes>
    </word>
    <value>
      <try>100 smooth=1 aa=1 pa</try>
      <literal>
        <try>100 smooth=1 aa=1 pa</try>
        <fail/>
      </literal>
      <success> smooth=1 aa=1 paddi</success>
      <attributes>[[1, 0, 0]]</attributes>
    </value>
    <word>
      <try>smooth=1 aa=1 paddin</try>
      <success>=1 aa=1 padding=0,0,</success>
      <attributes>[]</attributes>
    </word>
    <value>
      <try>1 aa=1 padding=0,0,0</try>
      <literal>
        <try>1 aa=1 padding=0,0,0</try>
        <fail/>
      </literal>
      <success> aa=1 padding=0,0,0,</success>
      <attributes>[[1]]</attributes>
    </value>
    <word>
      <try>aa=1 padding=0,0,0,0</try>
      <success>=1 padding=0,0,0,0 s</success>
      <attributes>[]</attributes>
    </word>
    <value>
      <try>1 padding=0,0,0,0 sp</try>
      <literal>
        <try>1 padding=0,0,0,0 sp</try>
        <fail/>
      </literal>
      <success> padding=0,0,0,0 spa</success>
      <attributes>[[1]]</attributes>
    </value>
    <word>
      <try>padding=0,0,0,0 spac</try>
      <success>=0,0,0,0 spacing=1,1</success>
      <attributes>[]</attributes>
    </word>
    <value>
      <try>0,0,0,0 spacing=1,1 </try>
      <literal>
        <try>0,0,0,0 spacing=1,1 </try>
        <fail/>
      </literal>
      <success> spacing=1,1 outline</success>
      <attributes>[[0, ,, 0, ,, 0, ,, 0]]</attributes>
    </value>
    <word>
      <try>spacing=1,1 outline=</try>
      <success>=1,1 outline=0\ncommo</success>
      <attributes>[]</attributes>
    </word>
    <value>
      <try>1,1 outline=0\ncommon</try>
      <literal>
        <try>1,1 outline=0\ncommon</try>
        <fail/>
      </literal>
      <success> outline=0\ncommon li</success>
      <attributes>[[1, ,, 1]]</attributes>
    </value>
    <word>
      <try>outline=0\ncommon lin</try>
      <success>=0\ncommon lineHeight</success>
      <attributes>[]</attributes>
    </word>
    <value>
      <try>0\ncommon lineHeight=</try>
      <literal>
        <try>0\ncommon lineHeight=</try>
        <fail/>
      </literal>
      <success>\ncommon lineHeight=3</success>
      <attributes>[[0]]</attributes>
    </value>
    <word>
      <try>\ncommon lineHeight=3</try>
      <fail/>
    </word>
    <word>
      <try>common lineHeight=32</try>
      <success> lineHeight=32 base=</success>
      <attributes>[]</attributes>
    </word>
    <word>
      <try>lineHeight=32 base=2</try>
      <success>=32 base=26 scaleW=2</success>
      <attributes>[]</attributes>
    </word>
    <value>
      <try>32 base=26 scaleW=25</try>
      <literal>
        <try>32 base=26 scaleW=25</try>
        <fail/>
      </literal>
      <success> base=26 scaleW=256 </success>
      <attributes>[[3, 2]]</attributes>
    </value>
    <word>
      <try>base=26 scaleW=256 s</try>
      <success>=26 scaleW=256 scale</success>
      <attributes>[]</attributes>
    </word>
    <value>
      <try>26 scaleW=256 scaleH</try>
      <literal>
        <try>26 scaleW=256 scaleH</try>
        <fail/>
      </literal>
      <success> scaleW=256 scaleH=2</success>
      <attributes>[[2, 6]]</attributes>
    </value>
    <word>
      <try>scaleW=256 scaleH=25</try>
      <success>=256 scaleH=256 page</success>
      <attributes>[]</attributes>
    </word>
    <value>
      <try>256 scaleH=256 pages</try>
      <literal>
        <try>256 scaleH=256 pages</try>
        <fail/>
      </literal>
      <success> scaleH=256 pages=0 </success>
      <attributes>[[2, 5, 6]]</attributes>
    </value>
    <word>
      <try>scaleH=256 pages=0 p</try>
      <success>=256 pages=0 packed=</success>
      <attributes>[]</attributes>
    </word>
    <value>
      <try>256 pages=0 packed=0</try>
      <literal>
        <try>256 pages=0 packed=0</try>
        <fail/>
      </literal>
      <success> pages=0 packed=0 al</success>
      <attributes>[[2, 5, 6]]</attributes>
    </value>
    <word>
      <try>pages=0 packed=0 alp</try>
      <success>=0 packed=0 alphaChn</success>
      <attributes>[]</attributes>
    </word>
    <value>
      <try>0 packed=0 alphaChnl</try>
      <literal>
        <try>0 packed=0 alphaChnl</try>
        <fail/>
      </literal>
      <success> packed=0 alphaChnl=</success>
      <attributes>[[0]]</attributes>
    </value>
    <word>
      <try>packed=0 alphaChnl=1</try>
      <success>=0 alphaChnl=1 redCh</success>
      <attributes>[]</attributes>
    </word>
    <value>
      <try>0 alphaChnl=1 redChn</try>
      <literal>
        <try>0 alphaChnl=1 redChn</try>
        <fail/>
      </literal>
      <success> alphaChnl=1 redChnl</success>
      <attributes>[[0]]</attributes>
    </value>
    <word>
      <try>alphaChnl=1 redChnl=</try>
      <success>=1 redChnl=0 greenCh</success>
      <attributes>[]</attributes>
    </word>
    <value>
      <try>1 redChnl=0 greenChn</try>
      <literal>
        <try>1 redChnl=0 greenChn</try>
        <fail/>
      </literal>
      <success> redChnl=0 greenChnl</success>
      <attributes>[[1]]</attributes>
    </value>
    <word>
      <try>redChnl=0 greenChnl=</try>
      <success>=0 greenChnl=0 blueC</success>
      <attributes>[]</attributes>
    </word>
    <value>
      <try>0 greenChnl=0 blueCh</try>
      <literal>
        <try>0 greenChnl=0 blueCh</try>
        <fail/>
      </literal>
      <success> greenChnl=0 blueChn</success>
      <attributes>[[0]]</attributes>
    </value>
    <word>
      <try>greenChnl=0 blueChnl</try>
      <success>=0 blueChnl=0\nchars </success>
      <attributes>[]</attributes>
    </word>
    <value>
      <try>0 blueChnl=0\nchars c</try>
      <literal>
        <try>0 blueChnl=0\nchars c</try>
        <fail/>
      </literal>
      <success> blueChnl=0\nchars co</success>
      <attributes>[[0]]</attributes>
    </value>
    <word>
      <try>blueChnl=0\nchars cou</try>
      <success>=0\nchars count=0\n</success>
      <attributes>[]</attributes>
    </word>
    <value>
      <try>0\nchars count=0\n</try>
      <literal>
        <try>0\nchars count=0\n</try>
        <fail/>
      </literal>
      <success>\nchars count=0\n</success>
      <attributes>[[0]]</attributes>
    </value>
    <word>
      <try>\nchars count=0\n</try>
      <fail/>
    </word>
    <word>
      <try>chars count=0\n</try>
      <success> count=0\n</success>
      <attributes>[]</attributes>
    </word>
    <word>
      <try>count=0\n</try>
      <success>=0\n</success>
      <attributes>[]</attributes>
    </word>
    <value>
      <try>0\n</try>
      <literal>
        <try>0\n</try>
        <fail/>
      </literal>
      <success>\n</success>
      <attributes>[[0]]</attributes>
    </value>
    <word>
      <try>\n</try>
      <fail/>
    </word>
    <word>
      <try></try>
      <fail/>
    </word>