Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Greetings Kind Regards I am learning to utilize flex and bison but for now my question is re/ flex i.e. to wit in particular how to input a string via stdin to be recognized by the generated lexer. The flex input file is below and lexer generated via
win_flex --wincompat --noyywrap flex.y
The token rule is merely that for the C language identifier.
flex.y
%%
\b[_a-zA-Z][_a-zA-Z0-9]*\b		printf("hello, world\n");
%%
 main()
        {
        yylex();
        }
Upon compilation and execution of generated file I merely type in e.g. "abc" w/o quotes then <enter> and expect "hello, world\n" to be printed as it is the action associated w/ the rule, however it is not, only ECHO is called in the switch statement below i.e. case 2:
C++
		switch ( yy_act )
	{ /* beginning of action switch */
			case 0: /* must back up */
			/* undo the effects of YY_DO_BEFORE_ACTION */
			*yy_cp = (yy_hold_char);
			yy_cp = (yy_last_accepting_cpos);
			yy_current_state = (yy_last_accepting_state);
			goto yy_find_action;

case 1:
YY_RULE_SETUP
//#line 2 "flex.y"
printf("hello, world\n");
	YY_BREAK
case 2:
YY_RULE_SETUP
//#line 3 "flex.y"
ECHO;
	YY_BREAK
//#line 730 "lex.yy.c"
case YY_STATE_EOF(INITIAL):
	yyterminate();

	case YY_END_OF_BUFFER:
		{
		/* Amount of text matched not including the EOB char. */
		int yy_amount_of_matched_text = (int) (yy_cp - (yytext_ptr)) - 1;
...}
So I do not know if I am specigying input file incorrectly, generating incorrectly or entering via stdin incorrectly. Kindly Advise - Thank You

What I have tried:

Every conceivable string termination via keyboard i.e. to wit in particular <enter>, <ctrl>+c, <ctrl>+z, space character, and the usual internet search
Posted
Updated 12-May-22 21:17pm
v2

1 solution

It does not depend on the kind of input stream.
Simply flex doesn't support '\b' as word boundary, see, for instance: regex - Matching exact word in Flex for lexical analysis - Stack Overflow[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900