提升灵气期望值
Boost Spirit Qi Expectation
本文关键字:期望值 更新时间:2023-10-16
我对Spirit Qi比较陌生,正在尝试解析类似汇编程序的语言。
例如,我想解析:
Func Ident{
Mov name, "hello"
Push 5
Exit
}
到目前为止,一切都很好。我可以正确地解析它。然而,错误处理程序有时会出现奇怪的错误位置。以以下故障代码为例:
Func Ident{
Mov name "hello" ; <-- comma is missing here
Push 5
Exit
}
以下是此解析中涉及的规则:
gr_function = lexeme["Func" >> !(alnum | '_')] // Ensure whole words
> gr_identifier
> "{"
> *( gr_instruction
|gr_label
|gr_vardecl
|gr_paramdecl)
> "}";
gr_instruction = gr_instruction_names
> gr_operands;
gr_operands = -(gr_operand % ',');
解析会注意到错误,但会抱怨Mov后面缺少"}"。我有一种感觉,这个问题在"Func"的定义中,但无法准确指出。我希望解析器抱怨缺少","如果它抱怨相应的错误,那也没关系,但它肯定应该指出一个缺失的逗号是罪魁祸首。
我尝试过一些变体,例如:
gr_operands = -(gr_operand
>> *(','
> gr_operand)
);
和其他人,但有其他奇怪的错误。
有人知道如何说"好吧,你可能有一条没有操作数的指令,但如果你找到了一条,并且在下一条之前没有逗号,就在逗号处失败"吗?
更新
感谢您到目前为止的回答。gr_operand定义如下:
gr_operand = ( gr_operand_intlit
|gr_operand_flplit
|gr_operand_strlit
|gr_operand_register
|gr_operand_identifier);
gr_operand_intlit = int_;
gr_operand_flplit = double_;
gr_operand_strlit = '"'
> strlitcont
> '"'
;
gr_operand_register = gr_register_names;
// TODO: Must also not accept the keywords from the statement grammar
gr_operand_identifier = !(gr_instruction_names | gr_register_names)
>> raw[
lexeme[(alpha | '_') >> *(alnum | '_')]
];
escchar.name("\"");
escchar = '' >> char_(""");
strlitcont.name("String literal content");
strlitcont = *( escchar | ~char_('"') );
您需要明确什么可以是操作数。我猜是这样的:
gr_operand = gr_identifier | gr_string;
gr_string = lexeme [ '"' >> *("""" | ~char_(""")) >> '"' ];
不相关,但您可能需要明确一点,即新行开始一条新语句(使用blank_type作为队长):
>> "{"
>> -(
gr_instruction
| gr_label
| gr_vardecl
| gr_paramdecl
) % eol
> "}";
现在,解析器将能够抱怨在解析失败时需要换行。
我用你在原帖中的草图制作了一个完整的工作样本。
在Coliru上查看直播:
#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
template <typename It, typename Skipper = qi::blank_type>
struct parser : qi::grammar<It, Skipper>
{
parser() : parser::base_type(start)
{
using namespace qi;
start = lexeme["Func" >> !(alnum | '_')] > function;
function = gr_identifier
>> "{"
>> -(
gr_instruction
//| gr_label
//| gr_vardecl
//| gr_paramdecl
) % eol
> "}";
gr_instruction_names.add("Mov", unused);
gr_instruction_names.add("Push", unused);
gr_instruction_names.add("Exit", unused);
gr_instruction = lexeme [ gr_instruction_names >> !(alnum|"_") ] > gr_operands;
gr_operands = -(gr_operand % ',');
gr_identifier = lexeme [ alpha >> *(alnum | '_') ];
gr_operand = gr_identifier | gr_string;
gr_string = lexeme [ '"' >> *("""" | ~char_(""")) >> '"' ];
BOOST_SPIRIT_DEBUG_NODES((start)(function)(gr_instruction)(gr_operands)(gr_identifier)(gr_operand)(gr_string));
}
private:
qi::symbols<char, qi::unused_type> gr_instruction_names;
qi::rule<It, Skipper> start, function, gr_instruction, gr_operands, gr_identifier, gr_operand, gr_string;
};
int main()
{
typedef boost::spirit::istream_iterator It;
std::cin.unsetf(std::ios::skipws);
It f(std::cin), l;
parser<It, qi::blank_type> p;
try
{
bool ok = qi::phrase_parse(f,l,p,qi::blank);
if (ok) std::cout << "parse successn";
else std::cerr << "parse failed: '" << std::string(f,l) << "'n";
if (f!=l) std::cerr << "trailing unparsed: '" << std::string(f,l) << "'n";
return ok;
} catch(const qi::expectation_failure<It>& e)
{
std::string frag(e.first, e.last);
std::cerr << e.what() << "'" << frag << "'n";
}
return false;
}