1,239,382,은 무엇 bigboss에게하는 것은
만 나는 의미 조치없이 할 것 :)했다 - 그것은 ... 훨씬 덜 장황 (도
Boost Spirit: "Semantic actions are evil"? 참조) 만들기 : 전부
vdef =
("constant" >> attr(true) | attr(false)) >>
"var" >> identifier >> '=' >> identifier_value >> ';' ;
. 기본값 (누락 된 constant
키워드)을 설명하기 위해 qi::attr
을 사용합니다. 여기
는 출력 전체 데모입니다 :
http://liveworkspace.org/code/c9e4bef100d2249eb4d4b88205f85c4b
출력 :
parse success: 'var myvariable = "has some value";'
data: false;myvariable;has some value;
parse success: 'constant var myvariable = "has some value";'
data: true;myvariable;has some value;
코드 :
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
namespace qi = boost::spirit::qi;
namespace karma = boost::spirit::karma;
namespace phx = boost::phoenix;
struct var_definition {
bool is_constant;
std::string name;
std::string value;
var_definition() : is_constant(false) {}
};
BOOST_FUSION_ADAPT_STRUCT(var_definition, (bool, is_constant)(std::string, name)(std::string, value))
void doParse(const std::string& input)
{
typedef std::string::const_iterator It;
qi::rule<It, std::string()> identifier, identifier_value;
qi::rule<It, var_definition(), qi::space_type> vdef;
{
using namespace qi;
identifier_value = '"' >> lexeme [ +~char_('"') ] > '"';
identifier = lexeme [ +graph ];
vdef =
("constant" >> attr(true) | attr(false)) >>
"var" >> identifier >> '=' >> identifier_value >> ';' ;
}
var_definition data;
It f(std::begin(input)), l(std::end(input));
bool ok = qi::phrase_parse(f,l,vdef,qi::space,data);
if (ok)
{
std::cout << "parse success: '" << input << "'\n";
std::cout << "data: " << karma::format_delimited(karma::auto_, ';', data) << "\n";
}
}
int main()
{
doParse("var myvariable = \"has some value\";");
doParse("constant var myvariable = \"has some value\";");
}
당신 ** 알고있다 **'const' 키워드있어 C++에서는 그렇습니까? –
물론 나는이 질문과 관련이 있는지 어떻게 알 수 있습니까? 어떻게 내 작은 언어로 상수를 구현할 수 있습니까? –
@userXXX 아, 그래서 당신은 당신의 언어에 대해 묻고 있습니다! 이 캐스케이프에서는 컴파일러에서'const' 값에 대한 할당을 금지하는 체크를 구현합니다. –