help-flex
[Top][All Lists]
Advanced

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

Re: CSV Parser - Empty values


From: Marco Draijer
Subject: Re: CSV Parser - Empty values
Date: Wed, 19 Feb 2003 09:21:29 +0100

I would use start states for this situation:

%s FOUND_COMMA
%s IN_STRING

<INITIAL>,          {BEGIN(FOUND_COMMA); did_yyless=0; return DELIMETER_TOK;}
<FOUND_COMMA>,      {if (did_yyless) {did_yyless=0; return DELIMITER_TOK;} else {yyless(0); did_yyless=1; return EMPTYDATA_TOK;} }

<FOUND_COMMA>.      {yyless(0); BEGIN(INITIAL);}
\"         {BEGIN(IN_STRING);}
<IN_STRING>[^"]*    {return DATATOK;}
<IN_STRING>\"       {BEGIN(INITIAL);}
\n         {return NEWLINE_TOK;}
<INITIAL,FOUND_COMMA>[a-zA-Z0-9]*  {return DATATOK;}

The only task of FOUND_COMMA is to return EMPTYDATA_TOK (only once!), or return to INITIAL start state.
As a bonus: use IN_STRING, so you can pass a complete string (including commas) to bison, and you don't have to handle strings there.

regards,
Marco


>>> "Ahsan A." <address@hidden> 02/19/03 08:49 AM >>>
Hi,

I'm new to flex & Bison, and I'm trying to write a csv
parser. My question is regarding the lexical scanner:

In a .csv file, if there is line:

one,two,,,three,four,"data,with comma"

I wish to return an EMPTYDATA_TOK if an empty value is
encountered (2 commas without any data in between)

the patterns are:

,          {return DELIMETER_TOK;}
\"         {return STRDELIMETER_TOK;}
\n         {return NEWLINE_TOK;}
[a-zA-Z0-9]*  {return DATATOK;}

What I wish to do is return an EMPTYTOK, when an empty
value (between two commas ) is matched.

Since there is no way to match a null value in regex,
is there any other way I could do this ? I tried
declaring two variables int del_start, del_end, and
checking for an empty value in between 2 commas, and
returning an EMPTYTOK, but in that case I need TWO
return statements in one action... which is not
possible...

The bison grammar is fairly simple...

Or would you recommend I write a scanner of my own for
this ? The object of writing this parser is to flex my
knowledge of GNU flex. :)

Regards,

Ahsan

__________________________________________________
Do you Yahoo!?
Yahoo! Shopping - Send Flowers for Valentine's Day
http://shopping.yahoo.com


_______________________________________________
Help-flex mailing list
address@hidden
http://mail.gnu.org/mailman/listinfo/help-flex

--
****************************************************************************
This message contains information that may be privileged or confidential and
is the property of the Cap Gemini Ernst & Young Group. It is only intended
for the person to whom it is addressed. If you are not the intended
recipient, you are not authorized to read, print, retain, copy disseminate,
distribute, or use this message or any part thereof. If you receive this
message in error, please notify the sender immediately and delete all copies
of this message.
****************************************************************************


reply via email to

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