Roberto Diaz wrote:
> Hello everybody.
> 
> I need your advice.. I need to access the line into which resides the
> last matched string. I am looking out in the documentation but I only
> see yylineno as a way to figure out this, I wonder if we have something
> better.
> 
> for example:
> line:
> my_token1 my_token2 my_token3 \n
> 
> lets suppose I have matched my_token3, Now I would like to store the
> whole line. Is there some easy way to accomplish this?
> 
> Thank you very much indeed
> 
> Rob.

Use scanner states.
I use something similar to the following:

%{

#include <string>

using namespace std;

string current_line;
bool first_entry = true;

#define YY_STACK_USED 1
#define YY_NO_TOP_STATE 1

%}

EOL     (\r?\n)

%x BUFFER_LINE

%%

        { // on first entry, set up the states
          if (first_entry) {
            BEGIN(INITIAL);
            yy_push_state(BUFFER_LINE);
            first_entry = false;
          }
        }

<BUFFER_LINE>^[^\r\n]*/{EOL} {
  current_line = yytext;
  // in my case, i fiddle with yytext and unput() only those parts
  // i want to keep (in particular, comments get stripped); yyless(0)
  // simply puts back the entire line for rescanning
  yyless(0);
  yy_pop_state();
  yy_set_bol(true); // mark as start of line
}

<*>{

  {EOL} {
    yy_push_state(BUFFER_LINE);
  }

  <<EOF>> {
    current_line.clear();
    // There's no yy_pop_all_states() function, so we have to access
    // internals here.
    while (yy_start_stack_ptr > 0)
      yy_pop_state();
  }

}


The state stack is used so that the BUFFER_LINE state does not interfere
with any other state that might be active.

Note that I also use a custom skeleton + awk script to hack C++ support
(without wrapping it all in a class) into flex's output, so the above
will probably need a bit of C-ification to work with vanilla flex.


_______________________________________________
Help-flex mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/help-flex

Reply via email to