Unix Programming - Newbie flex question.

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > October 2004 > Newbie flex question.





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Newbie flex question.
P. Kenter

2004-10-28, 5:51 pm

Hi all.

I playing with flex and bison a bit and have a, hopefully simple, question
about my flex code. If there is a more appropriate news group for this,
please let me know (I posted also in comp.compilers but that is moderated
and hasn't been updated for 3 days, don't know how long it normally takes
to get an answer there so forgive me for being impatient).

So, here is my scanner to scan pascal like comments. Everything enclosed by
a pair of braces should generated a "**Comment**\n" line, everything else a
"_nocomment_\n" line. Comments can not be nested.

%option noyywrap
%pointer

%{

#include <stdio.h>

%}

COMMENT_________"{"[^}]*"}"

%%

{COMMENT}_______________{_printf("**Comment**\n");_}
[^{COMMENT}]*___________{_printf("_nocomment_\n");_}


When I give the following input to the scanner the output is as follows:
% echo "Hi {how are} you } doing" | ./scanner
nocomment
**Comment**
nocomment
}_nocomment_


Why is for the the last "}" the standard rule invoked? I would expect that "
you } doing" is also not a comment and should only generated 1 nocomment
line. According to me the output should be:
nocomment
**Comment**
nocomment

Any help would be appreciated. I've tried looking for tutorials, but they
all come with the same examples like parsing expressions in calculators,
nothing like this. So a good (online) book/tutorial is welcome.

Pepijn.
Jens.Toerring@physik.fu-berlin.de

2004-10-28, 5:51 pm

P. Kenter <pepijn.kenter@nospam.dlr.nl> wrote:
> So, here is my scanner to scan pascal like comments. Everything enclosed by
> a pair of braces should generated a "**Comment**\n" line, everything else a
> "_nocomment_\n" line. Comments can not be nested.


> COMMENT_________"{"[^}]*"}"


> %%


> {COMMENT}_______________{_printf("**Comment**\n");_}
> [^{COMMENT}]*___________{_printf("_nocomment_\n");_}



> When I give the following input to the scanner the output is as follows:
> % echo "Hi {how are} you } doing" | ./scanner
> nocomment
> **Comment**
> nocomment
> }_nocomment_



> Why is for the the last "}" the standard rule invoked? I would expect that "
> you } doing" is also not a comment and should only generated 1 nocomment


The problem is that

[^{COMMENT}]*

means everything that's not a '{', a '}' or one of the letters in
"COMMENT", not what you seem to expect it to be, i.e. everything
that's not a comment. And since per default charcters with no rule
attached to them are send to stdout the '}' appears.

Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
P. Kenter

2004-10-28, 5:51 pm


>
> The problem is that
>
> [^{COMMENT}]*
>
> means everything that's not a '{', a '}' or one of the letters in
> "COMMENT", not what you seem to expect it to be, i.e. everything
> that's not a comment. And since per default charcters with no rule
> attached to them are send to stdout the '}' appears.
>


Ah, I see. Thanks.

So naturally my next question is: how can match everything that is not a
{COMMENT} ?

Pepijn.
Jens.Toerring@physik.fu-berlin.de

2004-10-28, 5:51 pm

P. Kenter <pepijn.kenter@nospam.dlr.nl> wrote:

[vbcol=seagreen]
> Ah, I see. Thanks.


> So naturally my next question is: how can match everything that is not a
> {COMMENT} ?


What about

[^{]+

i.e. everything that's not the start-of-comment delimiter?

Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com