samedi 2 juillet 2016

parsing identifiers except keywords

I am struggeling writing a identifier parser, which parses a alphanum string which is not a keyword. the keywords are all in a table:

struct keywords_t : x3::symbols<x3::unused_type> {
    keywords_t() {
        add("for", x3::unused)
                ("in", x3::unused)
                ("while", x3::unused);
    }
} const keywords;

and the parser for a identifier should be this:

auto const identifier_def =       
            x3::lexeme[
                (x3::alpha | '_') >> *(x3::alnum | '_')
            ];

now i try to combine these so an identifier parser fails on parsing a keyword. I tried it like this:

auto const identifier_def =       
                x3::lexeme[
                    (x3::alpha | '_') >> *(x3::alnum | '_')
                ]-keywords;

and this:

auto const identifier_def =       
                x3::lexeme[
                    (x3::alpha | '_') >> *(x3::alnum | '_') - keywords
                ];

it works on most inputs but if a string starts with an keywords like like int, whilefoo, forbar the parser fails to parse this strings. how can i get this parser correct?

Aucun commentaire:

Enregistrer un commentaire