Boost :: spirit :: qi keywords and identifiers

I saw several posts related to the nuances of using keywords / identifiers in qi grammars, but I can’t understand how the approach demonstrated in the enhancement examples should work ...

Keyword Declaration:

qi::symbols<char> keywords; 

An example of a set of keywords:

 keywords.add ("foo") ("bar") ; 

ID rule declaration:

 qi::rule<std::string::const_iterator, std::string(), ascii::space_type> identifier; 

Here, as a rule, the identifier is defined in the qi calc and compiler examples:

 identifier = !keywords >> qi::raw[ qi::lexeme[ ( qi::alpha | '_' ) >> *( qi::alnum | '_' ) ] ]; 

I may not read the qi syntax correctly, but it seems to me that this will not accept any literal that matches or starts with a keyword. Rejecting full keyword match is the desired behavior. But I want to accept "food" as an identifier, although it starts with the keyword "foo". This seems like a pretty standard use case, but the problem is finding documentation that really fixes this.

Can someone suggest an identifier rule that rejects only exact keyword matches?

Thanks!

+5
source share
1 answer

In fact, this issue deserves some votes. This should be much more implemented and probably should be considered in the lessons of the Spirit, as it is often overlooked (obviously the compiler samples are fine)


I may not read the qi syntax correctly, but it seems to me that this will not accept any literal that matches or starts with a keyword.

It is right. In case you notice one of my own answers (a pretty good chance), I try to do it as quickly and dirty as possible to fix grammars in which there were no proper gamers in the first place.

But yes, requiring separate keywords / identifiers, another job is required. I could find a link to the answer where it was done correctly (it’s not difficult, it’s just tiring).

Meanwhile, take a look at the very relevant

If you're building a really solid grammar of a general-purpose language, this is something you should consider using Spirit Lexer. On the other hand, in my humble opinion, the Spirit seeks rapid development and small one-time grammars, which are briefly implemented using the eDSL expression expression template. I believe that in many aspects it is very opposite to when it matters.

+3
source

Source: https://habr.com/ru/post/1208967/


All Articles