开发者

Need help with lexer runtime_error

I'm trying to write a C qi parser for a modified C language. When I un-comment the t_in_op and below section I get a run_time error stating

Syntax Error: Hit BEGIN when '+' at index 1 (or something along those lines, don't remember exactly). What is the problem with this lexer?

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/lex_lexertl.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>

namespace lex = boost::spirit::lex;
namespace qi = boost::spirit::qi;
namespace phoenix = boost::phoenix;

#include <boost/spirit/home/support/detail/lexer/runtime_error.hpp>

template <typename Lexer>
struct iLexer : lex::lexer<Lexer>
{
    iLexer()
    {
        // define tokens and associate them with the lexer
        identifier      = "[a-zA-Z_][a-zA-Z_0-9]*";
        intNum          = "([0-9]+)|(0x[0-9a-fA-F]+)|(0b[01]+)";
        floatNum        = "(([0-9]+|(([0-9]+\\.[0-9]*[fF]?)|(\\.[0-9]+)))([eE][-+]?[0-9]+)?[fF]?)";
        hexFloatNum     = "(0x[01](\\.[0-9a-fA-F]*)?p[-+]?[0-9]+[fF]?)";
        swizzle         = "([01]+[w-z]+)+|([01]+[rgba]+)+|([01]+[uv]+)+";
        t_inc_op        = "++";
        t_dec_op        = "--";

   开发者_JS百科     // white space
        ws              = "[ \t\n]+";

        this->self = ws [lex::_pass = lex::pass_flags::pass_ignore];

        this->self += identifier
            | floatNum
            | hexFloatNum
            | intNum
            | swizzle
            | t_inc_op
            | t_dec_op
            ;

        // these are passed on to parser as they are defined ('{' is passed as '{')
        this->self += lex::token_def<>('(') | ')' | '{' | '}' | '=' | '<' | '>' | '&' | '|' | ';';
    }

    lex::token_def<> ws;
    lex::token_def<> identifier, intNum, floatNum, hexFloatNum, swizzle;
    lex::token_def<> t_inc_op, t_dec_op;
};

template <typename Iterator>
struct iGrammar : qi::grammar<Iterator>
{
    template <typename TokenDef>
    iGrammar(TokenDef const& tok)
        // construct the base with the start symbol of our grammar
      : iGrammar::base_type(translation_unit)
    {
        translation_unit
            = *external_declaration
            ;
        BOOST_SPIRIT_DEBUG_NODE(translation_unit);

        external_declaration
            = function_defnition
            | declaration
            ;
        BOOST_SPIRIT_DEBUG_NODE(external_declaration);

        function_defnition
            = (tok.identifier >> '(' >> ')')
            ;
        BOOST_SPIRIT_DEBUG_NODE(function_defnition);

        declaration
            = ( tok.identifier >> '=' >> tok.intNum >> ';')
            ;
        BOOST_SPIRIT_DEBUG_NODE(declaration);
    }

    qi::rule<Iterator> translation_unit;
    qi::rule<Iterator> external_declaration;
    qi::rule<Iterator> function_defnition;
    qi::rule<Iterator> declaration;
};

int main(int argc, char* argv[])
{
    // iterator type used to expose the underlying input stream
    typedef std::string::iterator base_iterator_type;

    // lexer type
    typedef lex::lexertl::actor_lexer<
        lex::lexertl::token<
            base_iterator_type, boost::mpl::vector2<double, int> 
        > > lexer_type;

    // iterator type exposed by the lexer 
    typedef iLexer<lexer_type>::iterator_type iterator_type;

    // now we use the types defined above to create the lexer and grammar
    // object instances needed to invoke the parsing process
    iLexer<lexer_type> tokenizer;            // Our lexer
    iGrammar<iterator_type> g (tokenizer);   // Our parser 

    std::string str (read_from_file("simple.ic"));
    base_iterator_type first = str.begin();

    try {
        bool r = lex::tokenize_and_parse(first, str.end(), tokenizer, g);
        if (r) {
        std::cout << "-------------------------\n";
        std::cout << "Parsing succeeded\n";
        std::cout << "-------------------------\n";
        }
        else {
            std::string rest(first, str.end());
            std::cout << "-------------------------\n";
            std::cout << "Parsing failed\n";
            std::cout << "stopped at: \"" << rest << "\"\n";
            std::cout << "-------------------------\n";
        }
    }
    catch(const boost::lexer::runtime_error &re)
    {
        std::cerr << re.what() << std::endl;
    }

    std::cout << "Bye... :-) \n\n";
    return 0;
}

simple.ic int i32 = 1;


The '+' character has a special meaning in regular expressions. Therefore, you need to escape it in order to match a plain '+':

t_inc_op = "\\+\\+";

The only exception where you don't need to escape special regular expresion characters in Spirit.Lex is when you define the tokens from a single character, like

this->self += char_('+');

in which case the library does the escaping for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜