I wrote an xml parser that parses ASCII files, but now I need to be able to read UTF-8 encoded files. I have the following regex in lex , but they do not conform to UTF-8. I'm not sure what I'm doing wrong:
utf_8 [\x00-\xff]*
bom [\xEF\xBB\xBF]
then
bom { fprintf( stderr, "OMG I SAW A BOM"); return BOM;}
utf_8 { fprintf( stderr, "OMG I SAW A UTF CHAR", yytext[0] ); return UTF_8;}
I also have the following grammar rules:
program
: UTF8 '<' '?'ID attribute_list '?''>'
root ...
where UTF8:
Utf8
: BOM {printf("i saw a bom\n");}
| UTF_8 {printf("i saw a utf\n");}
| {printf("i didn't see anything.'\n");}
;
It always comes with i didn't see anything, my parser works for ASCII files, that is, when I copy the UTF-8 XML file to an empty document.
Any help would be greatly appreciated.
EDIT:
Here is the edited .l file for reference:
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "y.tab.h"
int lines = 1;
%}
utf_8 [\x0000-\xffff]*
bom [\xEF\xBB\xBF]
whitespace [ \t]
ev (.|{bom})
ev1 (.|{utf_8})
%%
{whitespace} { fprintf( stderr, "%s", yytext );}
\n { fprintf( stderr, "%s%d ", yytext, lines++ );}
. { fprintf( stderr, "{TOKEN:%c}", yytext[0] ); return yytext[0];}
bom { fprintf( stderr, "OMG I SAW A BOM"); return BOM;}
utf_8 { fprintf( stderr, "OMG I SAW A UTF CHAR", yytext[0] ); return UTF_8;}
%%
void error( char *message )
{
fprintf( stderr, "Error: %s\n", message );
exit(1);
}