从一行开始的字符位置 [英] Character Position from starting of a line

查看:158
本文介绍了从一行开始的字符位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在flex中,从行首开始获取字符位置的方法是什么? 我从文件的开头有关于位置的帖子,但是我希望从一行的开头开始.

In flex, what's the way to get character position from the starting of a line? I've got a post regarding position from start of a file but i want it from the start of a line.

还应处理以下情况:

/** this is 
a comment
*/int x,y;

输出:

此处位置"int" = 3

Here position of "int"=3

请给我一些实施的提示.

Please give me some hints to implement this.

推荐答案

我假设关于从文件开始位置的帖子"是

I presume that the "post regarding position from start of a file" is this one, or something similar. The following is based on that answer.

要跟踪当前列偏移量,只需在碰到换行符时添加代码以重置offset值.如果您遵循链接的答案中显示的模型,则可以使用YY_USER_ACTION宏来进行调整:

To track the current column offset, you only need to add the code to reset the offset value when you hit a newline character. If you follow the model presented in the linked answer, you can use a YY_USER_ACTION macro like this to do the adjustment:

#define YY_USER_ACTION                                       \
  yylloc.first_line = yylloc.last_line;                      \
  yylloc.first_column = yylloc.last_column;                  \
  if (yylloc.first_line == yylineno)                         \
     yylloc.last_column += yyleng;                           \
  else {                                                     \
     int col;                                                \
     for (col = 1; yytext[yyleng - col] != '\n'; ++col) {}   \
     yylloc.last_column = col;                               \
     yylloc.last_line = yylineno;                            \
  }

上面的代码假定当前令牌的起始行/列是前一个令牌的结束行/列,这意味着yylloc需要正确初始化.通常,您无需担心,因为只要知道您正在使用位置信息,bison就会自动声明并初始化yylloc(至{1,1,1,1}).

The above code assumes that the starting line/column of the current token is the end line/column of the previous token, which means that yylloc needs to be correctly initialized. Normally you don't need to worry about this, because bison will automatically declare and initialize yylloc (to {1,1,1,1}), as long as it knows that you are using location information.

宏第三行中的测试优化了令牌中没有换行符的常见情况,在这种情况下,自令牌开始以来,yylineno就不会改变.在else子句中,我们知道将在令牌中找到换行符,这意味着我们不必检查缓冲区下溢. (如果您调用input()或以其他方式自己操作yylineno,则需要修复for循环.)

The test in the third line of the macro optimizes the common case where there was no newline in the token, in which case yylineno will not have changed since the beginning of the token. In the else clause, we know that a newline will be found in the token, which means we don't have to check for buffer underflow. (If you call input() or otherwise manipulate yylineno yourself, then you'll need to fix the for loop.)

请注意,如果您使用yylessyymore或调用input,则该代码将无法正常工作.

Note that the code will not work properly if you use yyless or yymore, or if you call input.

使用yymoreyylloc将报告最后一个令牌段的范围,而不是整个令牌.要解决此问题,您需要先保存真实令牌.

With yymore, yylloc will report the range of the last token segment, rather than the entire token; to fix that, you'll need to save the real token beginning.

要使用yyless正确跟踪令牌范围,您可能需要在调用yyless之后重新扫描令牌(尽管如果令牌中没有换行符,可以避免重新扫描).

To correctly track the token range with yyless, you'll probably need to rescan the token after the call to yyless (although the rescan can be avoided if there is no newline in the token).

在调用input之后,您需要为每个读取的字符手动更新yylloc.last_column.不要调整yylineno; flex会正确处理.但是,如果yylineno已更改,则确实需要更新yylloc.last_line.

And after calling input, you'll need to manually update yylloc.last_column for each character read. Don't adjust yylineno; flex will deal with that correctly. But you do need to update yylloc.last_line if yylineno has changed.

这篇关于从一行开始的字符位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆