在 (f)lex 中复制整个输入行(以获得更好的错误消息)?
Copying entire input line in (f)lex (for better error messages)?
作为使用 yacc(或 bison)和 lex(或 flex)的典型解析器的一部分,我想在词法分析器中复制整个输入行,这样,如果以后出现错误,程序可以打印出来整个有问题的行,并在有问题的标记下放置插入符号 ^
。
要复制该行,我目前正在做:
char *line; // holds copy of entire line
bool copied_line;
%%
^.+ {
if ( !copied_line ) {
free( line );
line = strdup( yytext );
copied_line = true;
}
REJECT;
}
/* ... other tokens ... */
\n { copied_line = false; return END; }
这可行,但是,从进入调试器开始,它的效率确实很低。似乎正在发生的事情是 REJECT
导致词法分析器一次后退一个字符,而不是仅仅跳转到下一个可能的匹配项。
是否有更好、更有效的方法来获得我想要的东西?
假设您的输入来自可搜索的流:
- 计算遇到的换行符的数量 N
- 如果出错,查找并输出行N + 1
即使输入来自不可搜索的流,您也可以将所有字符保存在临时存储中。
这个主题的变体是可能的,比如存储最后看到的换行符的偏移量,所以你可以直接搜索它。
根据 @Serge Ballesta 的提示使用 YY_INPUT
:
#define YY_INPUT( BUF, RESULT, MAX_SIZE ) \
(RESULT) = lexer_get_input( (BUF), (MAX_SIZE) )
static size_t column; // current 0-based column
static char *input_line;
static size_t lexer_get_input( char *buf, size_t buf_size ) {
size_t bytes_read = 0;
for ( ; bytes_read < buf_size; ++bytes_read ) {
int const c = getc( yyin );
if ( c == EOF ) {
if ( ferror( yyin ) )
/* complain and exit */;
break;
}
buf[ bytes_read ] = (char)c;
if ( c == '\n' )
break;
} // for
if ( column == 0 && bytes_read < buf_size ) {
static size_t input_line_capacity;
if ( input_line_capacity < bytes_read + 1/*null*/ ) {
input_line_capacity = bytes_read + 1/*null*/;
input_line = (char*)realloc( input_line, input_line_capacity );
}
strncpy( input_line, buf, bytes_read );
input_line_len = bytes_read;
input_line[ input_line_len ] = '[=10=]';
}
return bytes_read;
}
第一次调用时,column
将为 0,因此会将整行复制到 input_line
。在随后的调用中,不需要做任何特别的事情。最终,column
会在遇到换行符时重置为 0;然后 下一次 调用该函数时,它将再次复制该行。
这似乎行得通,而且效率更高。有人发现它有什么问题吗?
在 flex 中,您可以使用 YY_USER_ACTION
,如果将其定义为宏,将 运行 用于每个标记,就在 运行 标记操作之前。所以像:
#define YY_USER_ACTION append_to_buffer(yytext);
会将 yytext
附加到一个缓冲区,您以后可以在其中使用它。
这里是 YY_INPUT
使用 getline()
的可能定义。只要没有令牌同时包含换行符和后续字符,它就应该可以工作。 (令牌可以在末尾包含换行符。)具体来说,current_line
将包含当前令牌的最后一行。
词法扫描成功完成后,current_line
将被释放并重置剩余的全局变量,以便可以对另一个输入进行词法分析。如果词法扫描在到达输入末尾之前中断(例如,因为解析不成功),则应显式调用 reset_current_line()
以执行这些任务。
char* current_line = NULL;
size_t current_line_alloc = 0;
ssize_t current_line_sent = 0;
ssize_t current_line_len = 0;
void reset_current_line() {
free(current_line);
current_line = NULL;
current_line_alloc = current_line_sent = current_line_len = 0;
}
ssize_t refill_flex_buffer(char* buf, size_t max_size) {
ssize_t avail = current_line_len - current_line_sent;
if (!avail) {
current_line_sent = 0;
avail = getline(¤t_line, ¤t_line_alloc, stdin);
if (avail < 0) {
if (ferror(stdin)) { perror("Could not read input: "); }
avail = 0;
}
current_line_len = avail;
}
if (avail > max_size) avail = max_size;
memcpy(buf, current_line + current_line_sent, avail);
current_line_sent += avail;
if (!avail) reset_current_line();
return avail;
}
#define YY_INPUT(buf, result, max_size) \
result = refill_flex_buffer(buf, max_size);
虽然上面的代码不依赖于维护当前列位置,但是如果你想识别当前标记在当前行中的位置,这很重要。如果您不使用 yyless
或 yymore
:
,以下内容将有所帮助
size_t current_col = 0, current_col_end = 0;
/* Call this in any token whose last character is \n,
* but only after making use of column information.
*/
void reset_current_col() {
current_col = current_col_end = 0;
}
#define YY_USER_ACTION \
{ current_col = current_col_end; current_col_end += yyleng; }
如果您将此扫描器与具有前瞻性的解析器一起使用,则仅保留输入流的一行可能不够,因为前瞻标记可能位于错误标记的后续行中。在循环缓冲区中保留几行保留行将是一个简单的增强,但需要多少行并不明显。
作为使用 yacc(或 bison)和 lex(或 flex)的典型解析器的一部分,我想在词法分析器中复制整个输入行,这样,如果以后出现错误,程序可以打印出来整个有问题的行,并在有问题的标记下放置插入符号 ^
。
要复制该行,我目前正在做:
char *line; // holds copy of entire line
bool copied_line;
%%
^.+ {
if ( !copied_line ) {
free( line );
line = strdup( yytext );
copied_line = true;
}
REJECT;
}
/* ... other tokens ... */
\n { copied_line = false; return END; }
这可行,但是,从进入调试器开始,它的效率确实很低。似乎正在发生的事情是 REJECT
导致词法分析器一次后退一个字符,而不是仅仅跳转到下一个可能的匹配项。
是否有更好、更有效的方法来获得我想要的东西?
假设您的输入来自可搜索的流:
- 计算遇到的换行符的数量 N
- 如果出错,查找并输出行N + 1
即使输入来自不可搜索的流,您也可以将所有字符保存在临时存储中。
这个主题的变体是可能的,比如存储最后看到的换行符的偏移量,所以你可以直接搜索它。
根据 @Serge Ballesta 的提示使用 YY_INPUT
:
#define YY_INPUT( BUF, RESULT, MAX_SIZE ) \
(RESULT) = lexer_get_input( (BUF), (MAX_SIZE) )
static size_t column; // current 0-based column
static char *input_line;
static size_t lexer_get_input( char *buf, size_t buf_size ) {
size_t bytes_read = 0;
for ( ; bytes_read < buf_size; ++bytes_read ) {
int const c = getc( yyin );
if ( c == EOF ) {
if ( ferror( yyin ) )
/* complain and exit */;
break;
}
buf[ bytes_read ] = (char)c;
if ( c == '\n' )
break;
} // for
if ( column == 0 && bytes_read < buf_size ) {
static size_t input_line_capacity;
if ( input_line_capacity < bytes_read + 1/*null*/ ) {
input_line_capacity = bytes_read + 1/*null*/;
input_line = (char*)realloc( input_line, input_line_capacity );
}
strncpy( input_line, buf, bytes_read );
input_line_len = bytes_read;
input_line[ input_line_len ] = '[=10=]';
}
return bytes_read;
}
第一次调用时,column
将为 0,因此会将整行复制到 input_line
。在随后的调用中,不需要做任何特别的事情。最终,column
会在遇到换行符时重置为 0;然后 下一次 调用该函数时,它将再次复制该行。
这似乎行得通,而且效率更高。有人发现它有什么问题吗?
在 flex 中,您可以使用 YY_USER_ACTION
,如果将其定义为宏,将 运行 用于每个标记,就在 运行 标记操作之前。所以像:
#define YY_USER_ACTION append_to_buffer(yytext);
会将 yytext
附加到一个缓冲区,您以后可以在其中使用它。
这里是 YY_INPUT
使用 getline()
的可能定义。只要没有令牌同时包含换行符和后续字符,它就应该可以工作。 (令牌可以在末尾包含换行符。)具体来说,current_line
将包含当前令牌的最后一行。
词法扫描成功完成后,current_line
将被释放并重置剩余的全局变量,以便可以对另一个输入进行词法分析。如果词法扫描在到达输入末尾之前中断(例如,因为解析不成功),则应显式调用 reset_current_line()
以执行这些任务。
char* current_line = NULL;
size_t current_line_alloc = 0;
ssize_t current_line_sent = 0;
ssize_t current_line_len = 0;
void reset_current_line() {
free(current_line);
current_line = NULL;
current_line_alloc = current_line_sent = current_line_len = 0;
}
ssize_t refill_flex_buffer(char* buf, size_t max_size) {
ssize_t avail = current_line_len - current_line_sent;
if (!avail) {
current_line_sent = 0;
avail = getline(¤t_line, ¤t_line_alloc, stdin);
if (avail < 0) {
if (ferror(stdin)) { perror("Could not read input: "); }
avail = 0;
}
current_line_len = avail;
}
if (avail > max_size) avail = max_size;
memcpy(buf, current_line + current_line_sent, avail);
current_line_sent += avail;
if (!avail) reset_current_line();
return avail;
}
#define YY_INPUT(buf, result, max_size) \
result = refill_flex_buffer(buf, max_size);
虽然上面的代码不依赖于维护当前列位置,但是如果你想识别当前标记在当前行中的位置,这很重要。如果您不使用 yyless
或 yymore
:
size_t current_col = 0, current_col_end = 0;
/* Call this in any token whose last character is \n,
* but only after making use of column information.
*/
void reset_current_col() {
current_col = current_col_end = 0;
}
#define YY_USER_ACTION \
{ current_col = current_col_end; current_col_end += yyleng; }
如果您将此扫描器与具有前瞻性的解析器一起使用,则仅保留输入流的一行可能不够,因为前瞻标记可能位于错误标记的后续行中。在循环缓冲区中保留几行保留行将是一个简单的增强,但需要多少行并不明显。