c – 用boost :: spirit解析python语法 – 问题

c – 用boost :: spirit解析python语法 – 问题,第1张

概述我正在尝试用boost :: spirit库编写一个 python解析器.这是代码: template <typename Iterator>class Parser : public qi::grammar<Iterator, space_type>{public: Parser() : Parser::base_type(small_stmt) { NEW 我正在尝试用boost :: spirit库编写一个 python解析器.这是代码:
template <typename Iterator>class Parser : public qi::grammar<Iterator,space_type>{public:    Parser() : Parser::base_type(small_stmt)    {        NEWliNE = lit("<NEWliNE>");        INDENT = lit("<INDENT>");        DEDENT = lit("<DEDENT>");        ENDMARKER = lit("<EOT>");        name = IDentifIEr.alias();        NUMBER = integer|longinteger|floatnumber|imagnumber;        STRING = stringliteral.alias();        IDentifIEr = (Alpha | '_') >> *(Alpha | digit | '_');        stringliteral = -stringprefix  >> (shortstring | longstring);        stringprefix = lit("r") | lit("u") | lit("ur") | lit("R") | lit("U") | lit("UR") | lit("Ur") | lit("uR") | lit("b") | lit("B") | lit("br") | lit("Br") | lit("bR") | lit("BR");        shortstring =  "'" >> *(shortstringitem - "'") >> "'" | "\"" >> *(shortstringitem - "\"") >> "\"";        longstring = "'''" >> *longstringitem >> "'''" | "\"\"\"" >> *longstringitem >> "\"\"\"";        shortstringitem = shortstringchar | escapeseq;        longstringitem = longstringchar | escapeseq;        shortstringchar = char_ - "\" - "\n";        longstringchar = char_ - "\";        escapeseq = '\' >> char_;        longinteger = integer >> (lit("l") | lit("L"));        integer = decimalinteger | octinteger | hexinteger | bininteger;        decimalinteger = nonzerodigit >> *digit | lit("0");        octinteger = lit("0") >> (lit("o") | lit("O")) >> +octdigit | lit("0") >> +octdigit;        hexinteger = lit("0") >> (lit("x") | lit("X")) >> +hexdigit;        bininteger = lit("0") >> (lit("b") | lit("B")) >> +bindigit;        nonzerodigit = char_('1','9');        octdigit = char_('0','7');        bindigit = lit("0") | lit("1");        hexdigit = digit | char_('a','f') | char_('A','F');        floatnumber = pointfloat | exponentfloat;        pointfloat = -intpart >> fraction | intpart >> ".";        exponentfloat = (intpart | pointfloat) >> exponent;        intpart = +digit;        fraction = "." >> +digit;        exponent = (lit("e") | lit("E")) >> -(lit("+") | lit("-")) >> +digit;        imagnumber = (floatnumber | intpart) >> (lit("j") | lit("J"));        single_input = NEWliNE|simple_stmt|compound_stmt >> NEWliNE;        file_input = *(NEWliNE|stmt) >> ENDMARKER;        eval_input = testList >> *NEWliNE >> ENDMARKER;        decorator = lit("@") >> dotted_name >> -( lit("(") >> -(argList) >> lit(")") ) >> NEWliNE;        decorators = +decorator;        decorated = decorators >> (classdef|funcdef);        funcdef = lit("def") >> name >> parameters >> lit(":") >> suite;        parameters = lit("(") >> -(varargsList) >> lit(")");        varargsList = (*(fpdef >> -(lit("=") >> test) >> lit(",")) >> (lit("*") >> name >> -(lit(",") >> lit("**") >> name)|lit("**") >> name)|fpdef >> -(lit("=") >> test) >> *(lit(",") >> fpdef >> -(lit("=") >> test)) >> -(lit(",")));        fpdef = name|lit("(") >> fpList >> lit(")");        fpList = fpdef >> *(lit(",") >> fpdef) >> -(lit(","));        stmt = simple_stmt|compound_stmt;        simple_stmt = small_stmt >> *(lit(";") >> small_stmt) >> -(lit(";")) >> NEWliNE;        small_stmt = (expr_stmt|print_stmt|del_stmt|pass_stmt|flow_stmt|import_stmt|global_stmt|exec_stmt|assert_stmt);        expr_stmt = testList >> (augassign >> (yIEld_expr|testList)|*(lit("=") >> (yIEld_expr|testList)));        augassign = (lit("+=")|lit("-=")|lit("*=")|lit("/=")|lit("%=")|lit("&=")|lit("|=")|lit("^=")|lit("<<=")|lit(">>=")|lit("**=")|lit("//="));        print_stmt = lit("print") >> ( -( test >> *(lit(",") >> test) >> -(lit(",")) )|lit(">>") >> test >> -( +(lit(",")) ) );        del_stmt = lit("del") >> exprList;        pass_stmt = lit("pass");        flow_stmt = break_stmt|continue_stmt|return_stmt|raise_stmt|yIEld_stmt;        break_stmt = lit("break");        continue_stmt = lit("continue");        return_stmt = lit("return") >> -(testList);        yIEld_stmt = yIEld_expr.alias();        raise_stmt = lit("raise") >> -(test >> -(lit(",") >> test >> -(lit(",") >> test)));        import_stmt = import_name|import_from;        import_name = lit("import") >> dotted_as_names;        import_from = (lit("from") >> (*lit(".") >> dotted_name|+lit(".")) >> lit("import") >> (lit("*")|lit("(") >> import_as_names >> lit(")")|import_as_names));        import_as_name = name >> -(lit("as") >> name);        dotted_as_name = dotted_name >> -(lit("as") >> name);        import_as_names = import_as_name >> *(lit(",") >> import_as_name) >> -(lit(","));        dotted_as_names = dotted_as_name >> *(lit(",") >> dotted_as_name);        dotted_name = name >> *(lit(".") >> name);        global_stmt = lit("global") >> name >> *(lit(",") >> name);        exec_stmt = lit("exec") >> expr >> -(lit("in") >> test >> -(lit(",") >> test));        assert_stmt = lit("assert") >> test >> -(lit(",") >> test);        compound_stmt = if_stmt|while_stmt|for_stmt|try_stmt|with_stmt|funcdef|classdef|decorated;        if_stmt = lit("if") >> test >> lit(":") >> suite >> *(lit("elif") >> test >> lit(":") >> suite) >> -(lit("else") >> lit(":") >> suite);        while_stmt = lit("while") >> test >> lit(":") >> suite >> -(lit("else") >> lit(":") >> suite);        for_stmt = lit("for") >> exprList >> lit("in") >> testList >> lit(":") >> suite >> -(lit("else") >> lit(":") >> suite);        try_stmt = (lit("try") >> lit(":") >> suite >> (+(except_clause >> lit(":") >> suite) >> -(lit("else") >> lit(":") >> suite) >> -(lit("finally") >> lit(":") >> suite)|lit("finally") >> lit(":") >> suite));        with_stmt = lit("with") >> with_item >> *(lit(",") >> with_item) >> lit(":") >> suite;        with_item = test >> -(lit("as") >> expr);        except_clause = lit("except") >> -(test >> -((lit("as")|lit(",")) >> test));        suite = simple_stmt|NEWliNE >> INDENT >> +stmt >> DEDENT;        testList_safe = old_test >> -(+(lit(",") >> old_test) >> -(lit(",")));        old_test = or_test|old_lambdef;        old_lambdef = lit("lambda") >> -(varargsList) >> lit(":") >> old_test;        test = or_test >> -(lit("if") >> or_test >> lit("else") >> test)|lambdef;        or_test = and_test >> *(lit("or") >> and_test);        and_test = not_test >> *(lit("and") >> not_test);        not_test = lit("not") >> not_test|comparison;        comparison = expr >> *(comp_op >> expr);        comp_op = lit("<")|lit(">")|lit("==")|lit(">=")|lit("<=")|lit("<>")|lit("!=")|lit("in")|lit("not in")|lit("is")|lit("is not");        expr = xor_expr >> *(lit("|") >> xor_expr);        xor_expr = and_expr >> *(lit("^") >> and_expr);        and_expr = shift_expr >> *(lit("&") >> shift_expr);        shift_expr = arith_expr >> *((lit("<<")|lit(">>")) >> arith_expr);        arith_expr = term >> *((lit("+")|lit("-")) >> term);        term = factor >> *((lit("*")|lit("/")|lit("%")|lit("//")) >> factor);        factor = (lit("+")|lit("-")|lit("~")) >> factor|power;        power = atom >> *trailer >> -(lit("**") >> factor);        atom = (lit("(") >> -(yIEld_expr|testList_comp) >> lit(")")|lit("-(") >> -(Listmaker) >> lit(")")|lit("{") >> -(dictorsetmaker) >> lit("}")|lit("`") >> testList1 >> lit("`")|name|NUMBER|+STRING);        Listmaker = test >> ( List_for|*(lit(",")) );        testList_comp = test >> ( comp_for|*(lit(",")) );        lambdef = lit("lambda") >> -(varargsList) >> lit(":") >> test;        trailer = lit("(") >> -(argList) >> lit(")")|lit("[") >> subscriptList >> lit("]")|lit(".") >> name;        subscriptList = subscript >> *(lit(",") >> subscript) >> -(lit(","));        subscript = lit(".") >> lit(".") >> lit(".")|test|-(test) >> lit(":") >> -(test) >> -(sliceop);        sliceop = lit(":") >> -(test);        exprList = expr >> *(lit(",") >> expr) >> -(lit(","));        testList = test >> *(lit(","));        dictorsetmaker = ( (test >> lit(":") >> test >> (comp_for|*(lit(",") >> test >> lit(":") >> test) >> -(lit(","))))|(test >> (comp_for|*(lit(",")))) );        classdef = lit("class") >> name >> -(lit("(") >> -(testList) >> lit(")")) >> lit(":") >> suite;        argList = *(argument >> lit(",")) >> (argument >> -(lit(","))|lit("*") >> test >> *(lit(",") >> argument) >> -(lit(",") >> lit("**") >> test)|lit("**") >> test);        argument = test >> -(comp_for)|test >> lit("=") >> test;        List_iter = List_for|List_if;        List_for = lit("for") >> exprList >> lit("in") >> testList_safe >> -(List_iter);        List_if = lit("if") >> old_test >> -(List_iter);        comp_iter = comp_for|comp_if;        comp_for = lit("for") >> exprList >> lit("in") >> or_test >> -(comp_iter);        comp_if = lit("if") >> old_test >> -(comp_iter);        testList1 = test >> *(lit(",") >> test);        enCoding_decl = name.alias();        yIEld_expr = lit("yIEld") >> -(testList);    }    // LEXEMS    qi::rule<Iterator,space_type> NEWliNE;    qi::rule<Iterator,space_type> INDENT;    qi::rule<Iterator,space_type> DEDENT;    qi::rule<Iterator,space_type> ENDMARKER;    qi::rule<Iterator,space_type> name;    qi::rule<Iterator,space_type> NUMBER;    qi::rule<Iterator,space_type> STRING;    // IDENTIFIER    qi::rule<Iterator,space_type> IDentifIEr;    // STRING literaL    qi::rule<Iterator,space_type> stringliteral;    qi::rule<Iterator,space_type> stringprefix;    qi::rule<Iterator,space_type> shortstring;    qi::rule<Iterator,space_type> longstring;    qi::rule<Iterator,space_type> shortstringitem;    qi::rule<Iterator,space_type> longstringitem;    qi::rule<Iterator,space_type> shortstringchar;    qi::rule<Iterator,space_type> longstringchar;    qi::rule<Iterator,space_type> escapeseq;    // INTEGER literaL    qi::rule<Iterator,space_type> longinteger;    qi::rule<Iterator,space_type> integer;    qi::rule<Iterator,space_type> decimalinteger;    qi::rule<Iterator,space_type> octinteger;    qi::rule<Iterator,space_type> hexinteger;    qi::rule<Iterator,space_type> bininteger;    qi::rule<Iterator,space_type> nonzerodigit;    qi::rule<Iterator,space_type> octdigit;    qi::rule<Iterator,space_type> bindigit;    qi::rule<Iterator,space_type> hexdigit;    // float literaL    qi::rule<Iterator,space_type> floatnumber;    qi::rule<Iterator,space_type> pointfloat;    qi::rule<Iterator,space_type> exponentfloat;    qi::rule<Iterator,space_type> intpart;    qi::rule<Iterator,space_type> fraction;    qi::rule<Iterator,space_type> exponent;    //IMAGINARY literaL    qi::rule<Iterator,space_type> imagnumber;    // PYTHON GRAMMAR    qi::rule<Iterator,space_type> single_input;    qi::rule<Iterator,space_type> file_input;    qi::rule<Iterator,space_type> eval_input;    qi::rule<Iterator,space_type> decorator;    qi::rule<Iterator,space_type> decorators;    qi::rule<Iterator,space_type> decorated;    qi::rule<Iterator,space_type> funcdef;    qi::rule<Iterator,space_type> parameters;    qi::rule<Iterator,space_type> varargsList;    qi::rule<Iterator,space_type> fpdef;    qi::rule<Iterator,space_type> fpList;    qi::rule<Iterator,space_type> stmt;    qi::rule<Iterator,space_type> simple_stmt;    qi::rule<Iterator,space_type> small_stmt;    qi::rule<Iterator,space_type> expr_stmt;    qi::rule<Iterator,space_type> augassign;    qi::rule<Iterator,space_type> print_stmt;    qi::rule<Iterator,space_type> del_stmt;    qi::rule<Iterator,space_type> pass_stmt;    qi::rule<Iterator,space_type> flow_stmt;    qi::rule<Iterator,space_type> break_stmt;    qi::rule<Iterator,space_type> continue_stmt;    qi::rule<Iterator,space_type> return_stmt;    qi::rule<Iterator,space_type> yIEld_stmt;    qi::rule<Iterator,space_type> raise_stmt;    qi::rule<Iterator,space_type> import_stmt;    qi::rule<Iterator,space_type> import_name;    qi::rule<Iterator,space_type> import_from;    qi::rule<Iterator,space_type> import_as_name;    qi::rule<Iterator,space_type> dotted_as_name;    qi::rule<Iterator,space_type> import_as_names;    qi::rule<Iterator,space_type> dotted_as_names;    qi::rule<Iterator,space_type> dotted_name;    qi::rule<Iterator,space_type> global_stmt;    qi::rule<Iterator,space_type> exec_stmt;    qi::rule<Iterator,space_type> assert_stmt;    qi::rule<Iterator,space_type> compound_stmt;    qi::rule<Iterator,space_type> if_stmt;    qi::rule<Iterator,space_type> while_stmt;    qi::rule<Iterator,space_type> for_stmt;    qi::rule<Iterator,space_type> try_stmt;    qi::rule<Iterator,space_type> with_stmt;    qi::rule<Iterator,space_type> with_item;    qi::rule<Iterator,space_type> except_clause;    qi::rule<Iterator,space_type> suite;    qi::rule<Iterator,space_type> testList_safe;    qi::rule<Iterator,space_type> old_test;    qi::rule<Iterator,space_type> old_lambdef;    qi::rule<Iterator,space_type> test;    qi::rule<Iterator,space_type> or_test;    qi::rule<Iterator,space_type> and_test;    qi::rule<Iterator,space_type> not_test;    qi::rule<Iterator,space_type> comparison;    qi::rule<Iterator,space_type> comp_op;    qi::rule<Iterator,space_type> expr;    qi::rule<Iterator,space_type> xor_expr;    qi::rule<Iterator,space_type> and_expr;    qi::rule<Iterator,space_type> shift_expr;    qi::rule<Iterator,space_type> arith_expr;    qi::rule<Iterator,space_type> term;    qi::rule<Iterator,space_type> factor;    qi::rule<Iterator,space_type> power;    qi::rule<Iterator,space_type> atom;    qi::rule<Iterator,space_type> Listmaker;    qi::rule<Iterator,space_type> testList_comp;    qi::rule<Iterator,space_type> lambdef;    qi::rule<Iterator,space_type> trailer;    qi::rule<Iterator,space_type> subscriptList;    qi::rule<Iterator,space_type> subscript;    qi::rule<Iterator,space_type> sliceop;    qi::rule<Iterator,space_type> exprList;    qi::rule<Iterator,space_type> testList;    qi::rule<Iterator,space_type> dictorsetmaker;    qi::rule<Iterator,space_type> classdef;    qi::rule<Iterator,space_type> argList;    qi::rule<Iterator,space_type> argument;    qi::rule<Iterator,space_type> List_iter;    qi::rule<Iterator,space_type> List_for;    qi::rule<Iterator,space_type> List_if;    qi::rule<Iterator,space_type> comp_iter;    qi::rule<Iterator,space_type> comp_for;    qi::rule<Iterator,space_type> comp_if;    qi::rule<Iterator,space_type> testList1;    qi::rule<Iterator,space_type> enCoding_decl;    qi::rule<Iterator,space_type> yIEld_expr;};

问题是,当我尝试解析简单文件时:

pass

经过一些lexer模块后的女巫是:

pass <NEWliNE> <EOT>

解析失败并在第一个字符处停止.
当我尝试使用pass_stmt规则解析此文件时,一切正常(除了我们仍然拥有并保留,但传递单词被“消耗”).
当我尝试用规则一级解析它时 – small_stmt – 解析器停在

> <EOT>

消费

pass <NEWliNE

一级 – simple_stmt给出与file_input相同的结果 – 解析器在第一个字符处停止.

在添加PYTHON GRAMMAR部分中定义的语法(从http://docs.python.org/reference/grammar.html获取)之前,一切正常.解析器识别标识符,文字,数字等.

有人知道这里有什么不对吗?

解决方法 我建议您按照 here所述启用调试.这将为您提供实际情况的见解.一般来说,我建议一步一步地构建语法,而不是试图在一个大的飞跃中实现一切.

您提供的上述代码对于非单一读者来说非常难以理解,因为它非常大且没有评论.编写语法非常类似于编写“普通”代码.封装是成功的关键.尝试构建覆盖自包含片段的较小语法,并根据需要组合这些子语法.有关最佳做法,请参阅here.

总结

以上是内存溢出为你收集整理的c – 用boost :: spirit解析python语法 – 问题全部内容,希望文章能够帮你解决c – 用boost :: spirit解析python语法 – 问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1241283.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存