看了action的讲解, 没搞清楚,求助。
下面的程序, 当输入[[]]时, 为什么输出on_open_array一次,而输出on_close_array两次。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
%%{
machine openclose;
write data;
}%%
void test(char *str)
{
int cs;
char *p = str, *pe = str + strlen(str);
bool found_open_obj = false;
bool found_open_array = false;
bool found_close_obj = false;
bool found_close_array = false;
%%{
action on_open_obj {
printf("on_open_obj\n");
found_open_obj = true;
}
action on_open_array {
printf("on_open_array\n");
found_open_array = true;
}
action on_close_obj {
printf("on_close_obj\n");
found_close_obj = true;
}
action on_close_array {
printf("on_close_array\n");
found_close_array = true;
}
main := (space*) ( '{'@on_open_obj | '['@on_open_array ) any* ( '}'@on_close_obj | ']'@on_close_array ) space* '\n';
# Initialize and execute.
write init;
write exec;
}%%
if ( cs < openclose_first_final )
{
fprintf( stderr, "there was an syntax error\n" );
}
else
{
if(found_open_obj && found_close_obj)
{
fprintf( stderr, "startwith open obj, endwith close obj\n" );
}
else if(found_open_array && found_close_array)
{
fprintf( stderr, "startwith open array, endwith close array\n" );
}
else
{
fprintf( stderr, "wrong format\n" );
}
}
};
int main(int argc, char *argv[])
{
char buf[1024];
while (fgets(buf, sizeof(buf), stdin) != 0) {
test(buf);
}
return 0;
}
--
FROM 117.100.99.*