help-flex
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to return the position in the input string where the token is fo


From: Joseph Kraenzle
Subject: Re: how to return the position in the input string where the token is found
Date: Mon, 07 Jul 2003 12:09:29 -0400

As mentioned in <http://www.gnu.org/manual/flex-2.5.4/html_node/flex_14.html>,

"The macro YY_USER_ACTION can be defined to provide an action which is always executed prior to the matched rule's action. For example, it could be #define'd to call a routine to convert yytext to lower-case. When YY_USER_ACTION is invoked, the variable yy_act gives the number of the matched rule (rules are numbered starting with 1). Suppose you want to profile how often each of your rules is matched. The following would do the trick:

#define YY_USER_ACTION ++ctr[yy_act]

where ctr is an array to hold the counts for the different rules. Note that the macro YY_NUM_RULES gives the total number of rules (including the default rule, even if you use `-s', so a correct declaration for ctr is:

int ctr[YY_NUM_RULES];"

I do a related tracking by making use of the YY_USER_ACTION macro. You can track the number of characters that have been read from the string by lex in the current line with a little thought about storing some global indices. And in the YY_USER_ACTION statement, knowing the length of the token in the string (accessible by using yyleng), you can subtract it from the current position of the lexer in the string, and find the location of the token (e.g. "+") in the string. This can be stored into a global in the YY_USER_ACTION macro and accessed in the action part of the lexer.

I am sure there are other ways in the input process to track and mark the operator in the expression, and then call a function in the action part to grab the globally stored value.

At 01:44 AM 7/4/03 -0700, you wrote:
Hi,

We are trying to develop a friendly IDE for our own
grammer. For this purpose we have created a Lexer
which is taking input from a string buffer.

The user enters the script in a rich edit control. The
lexer is called after each character.

Once a token is recognized we need to call a function
in the action part of the lexer, that would return the
position of the character in the string.

For example if input string is "x+y". Where "+" is the
token to be identified, the function needs to pass the
position of "+" in the string i.e. 2.

I am unable to find any means to return this
position.Please help regarding this

Thanks


__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com


_______________________________________________
address@hidden http://mail.gnu.org/mailman/listinfo/help-bison










reply via email to

[Prev in Thread] Current Thread [Next in Thread]