yyless and pattern matching

"Michael B. Allen" <mballen@NOSPAM_erols.com>
23 Mar 2000 22:29:25 -0500

          From comp.compilers

Related articles
yyless and pattern matching mballen@NOSPAM_erols.com (Michael B. Allen) (2000-03-23)
Re: yyless and pattern matching cfc@world.std.com (Chris F Clark) (2000-03-25)
Re: yyless and pattern matching rkrayhawk@aol.com (2000-04-01)
| List of all articles for this month |

From: "Michael B. Allen" <mballen@NOSPAM_erols.com>
Newsgroups: comp.compilers
Date: 23 Mar 2000 22:29:25 -0500
Organization: Compilers Central
Keywords: lex, question


I've found a little peculiarity in flex I don't understand. Perhaps
you can explain this to me? I beleive I have isolated the issue in the
appended example.

In the example, comments('#' signs) are collected using yymore for
returning as one token. To detect the end of a series of comments the
lexer looks ahead for a char that is not a '#' at which point it calls
yyless to put that one char back. Now however if you try to match that
char with the '^' beginning of line op it fails. It's as though
because the character was matched previously, the regular expression
state required to match the char put back at the beginning of a line
is not valid anymore.

Here's the example lexer, input, and sample run to illustrate. I am
interested in how this problem is dealt with and moreover how flex works
exactly. Bare with me, I have no formal training and learned all of what
I know from a few hours with the O'Reilly book on lex and yacc.

Michael B. Allen
PS: Thanks for your help on my previous post Robert and John.


%x X

#\n {
        printf( "first_comment{%s}", yytext );
        BEGIN X;
^t {
        printf( "BEGAN WITH 't'!\n" );
<X>#\n {
        printf( "more_comment{%s}", yytext );
<X>[^#] {
        yyless( yyleng - 1 );
        printf( "no_longer_comment{%s}", yytext );





[miallen@angus parsers]$ test.parser < input
}t << 't' is at the beg of line but didn't match?
BEGAN WITH 't'! << now it did

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.