对于我自己的小解析器框架,我试图定义(类似)以下函数:
template <class T>
// with operator>>( std::istream&, T& )
void tryParse( std::istream& is, T& tgt )
{
is >> tgt /* , *BUT* store every character that is consumed by this operation
in some string. If afterwards, is.fail() (which should indicate a parsing
error for now), put all the characters read back into the 'is' stream so that
we can try a different parser. */
}
Run Code Online (Sandbox Code Playgroud)
然后我可以这样写:(也许不是最好的例子)
/* grammar: MyData = <IntTriple> | <DoublePair>
DoublePair = <double> <double>
IntTriple …Run Code Online (Sandbox Code Playgroud)