From: David L. B. <dl...@um...> - 2015-02-09 21:49:19
|
Hi, I've taken the "Advanced Calculator" example from the Flex/Bison Oreilly book and made some minor modifications so that setting the debug does not cause a syntax error and I've added C style '()' and '{}' to the IF THEN ELSE and WHILE DO statements. When compiled the calculator works exactly as I expect it to. If instead of calling yyparse() from main I call a function I've defined in the third section of the scanner.l file called "call_readline()" void call_readline() { YY_BUFFER_STATE bp; char *f = readline(">>> "); add_history(f); bp = yy_scan_string(f); free(f); yy_switch_to_buffer(bp); yyparse(); /* eat the input */ yy_delete_buffer(bp); } which I took from a posting by John Levine, the parser returns "error: syntax error" message. I have added several print statements to the scanner and to the parser. Here is a typical session: >>> 1 + 1 TOKEN: 1 PARSE: NUMBER TOKEN: + TOKEN: 1 PARSE: NUMBER PARSE: exp + exp 1: error: syntax error >>> 2 * 2 TOKEN: 2 PARSE: NUMBER TOKEN: * TOKEN: 2 PARSE: NUMBER PARSE: exp * exp 1: error: syntax error >>> ^C I have no idea how to debug this. If I call yyparse() from main the scanner and parser both work. If I change the input from stdin to readline the scanner seems to work but, what is being forwarded to the parser seems to be different for identical input. Can someone provide guidance? Cheers, David |