0
我嘗試提取一行的內容並在該行中的輸入被從野牛中拒絕時打印它們。 我嘗試重現這些建議:http://archive.oreilly.com/pub/a/linux/excerpts/9780596155971/error-reporting-recovery.html但是當輸入被拒絕時,將打印下一行而不是被拒絕的行,而行的編號已正確打印。提取行內容失敗flex/bison
撓曲:
%{
#include <stdio.h>
#include "parser.tab.h"
int line_number = 0;
char linebuf[500];
%}
...
%%
\n.* { ++line_number; strncpy(linebuf, yytext+1, sizeof(linebuf)); /* save the next line */
yyless(1); /* give back all but the \n to rescan */
}
%%
野牛:
%{
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include "parser.tab.h"
extern int yylex(void);
extern int line_number;
extern char line_contents[500];
void yyerror(char const *s);
%}
...
%%
int main(){
if(yyparse() == 0)
printf("Accepted\n");
else
printf("Syntax error in line %d: %s\n" line_number, linebuf);
...
對從野牛拒絕上述從包含語法錯誤的一個printd下一行的辦法輸入。
input:
result = function //(semicolon expected)
else
輸出:
Syntax error in line 1: else
相信詞法規則\n.*
或yytext+1
驅動器輸出到下一行,但其詞法規則是正確的?