나는 개인적으로 의미 론적 행동 (또는 피닉스) 이것을 가볍게 사용하지 않을 것이다. 이것은 제나라의 "성령"이 아닙니다.
이 내 걸릴 수 있습니다 :
rule<char const*, char()> r =
(omit [ int_(0) ] >> char_('a')) |
(omit [ int_(1) ] >> char_('b'))
;
보기? 훨씬 더 청결한. 또한 : 자동 속성 전파.
이
ok: a
fail
fail
ok: b
전체 샘플 코드 : 제나라를 통해
//#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace qi = boost::spirit::qi;
namespace phoenix = boost::phoenix;
template <typename P>
inline
void test_parser(char const* input, P const& p, bool full_match = true)
{
char const* f(input);
char const* l(f + strlen(f));
char result;
if (qi::parse(f, l, p, result) && (!full_match || (f == l)))
std::cout << "ok: " << result << std::endl;
else
std::cout << "fail" << std::endl;
}
int main()
{
int p;
using namespace qi;
rule<char const*, char()> r =
(omit [ int_(0) ] >> char_('a')) |
(omit [ int_(1) ] >> char_('b'))
;
BOOST_SPIRIT_DEBUG_NODE(r);
test_parser("0a", r); //should match
test_parser("0b", r); //should not match
test_parser("1a", r); //should not match
test_parser("1b", r); //should match
}
가능한 해결 방법 : 게으른 http://liveworkspace.org/code/2B4VCK$8 – user2008982
나 '는
가 출력 http://liveworkspace.org/code/1T9h5에 살고 참조 시맨틱 액션없이 답변을 추가했습니다. ([부스트 스피릿 : "시맨틱 액션은 악"입니까?] (http://stackoverflow.com/questions/8259440/boost-spirit-semantic-actions-are-evil/8259585#8259585)) – sehe
@sehe : 의미 론적 행동은 악합니다. 게시물의 좋은 부분은 실제 상황 일뿐입니다. 필수 :)하지만, IMHO는 파서의 컨텍스트 감도와 관련된 질문을 실제 구문 분석 문제로 만들기보다는 취해 보았습니다. 예제는 그 간단한 표현이었습니다. 내 2c. 그래도 우리 중 한 명이 받아 들여지면 좋을 것입니다 ... – FatalFlaw