Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7811867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:19:17+00:00 2026-06-02T04:19:17+00:00

I wrote an xml parser that parses ASCII files, but I need now to

  • 0

I wrote an xml parser that parses ASCII files, but I need now to be able to read UTF-8 encoded files. I have the following regex in lex but they don’t match UTF-8. I am not sure what I am 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 is:

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 up with i didn't see anything, my parser works for ASCII files, that is when I copy paste the XML UTF-8 file in a empty document.

Any help would be appreciated.

EDIT:

Here is a trimmed .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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:19:19+00:00Added an answer on June 2, 2026 at 4:19 am

    Okay, this is your problem:

    utf_8       [\x0000-\xffff]*
    bom         [\xEF\xBB\xBF]
    

    There are two problems here. First, Flex doesn’t actually understand Unicode. It works on bytes. So you need a regex macro that matches any valid UTF-8 byte sequence. http://keithdevens.com/weblog/archive/2004/Jun/29/UTF-8.regex has a regular expression that does that, which is not terribly hard to convert to Flex syntax (see below). Second, the square brackets in your BOM macro are making it match any single byte with value EF, BB, or BF, not the three-byte sequence EB BB BF which is what you want.

    (Incidentally, UTF-8 files are not supposed to have byte order marks, although many do anyway.)

    Here is a complete Flex input file that does more or less what you appear to have been trying to do:

    %{
    #include <stdio.h>
    %}
    
    bom     \xEF\xBB\xBF
    white   [ \t]
    
    u2a     [\xC2-\xDF][\x80-\xBF]
    u2b     \xE0[\xA0-\xBF][\x80-\xBF]
    u3a     [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}
    u3b     \xED[\x80-\x9F][\x80-\xBF]
    u4a     \xF0[\x90-\xBF][\x80-\xBF]{2}
    u4b     [\xF1-\xF3][\x80-\xBF]{3}
    u4c     \xF4[\x80-\x8F][\x80-\xBF]{2}
    
    utf_8   {u2a}|{u2b}|{u3a}|{u3b}|{u4a}|{u4b}|{u4c}
    
    %%
    
    {white}     { putchar(' ');  }
    \n          { putchar('\n'); }
    {bom}       { putchar('B');  }
    {utf_8}     { putchar('u');  }
    [\x21-\x7e] { putchar('.');  }
    .           { putchar('^');  }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with a Wikipedia XML dump that is encoded in UTF-8. Right
I have a python script that parses an XML file that contains part information
I wrote a xml parser with StAx that I use to parse XML streams
I'm using boost::property_tree to read and write XML configuration files in my application. But
I try to write warpper that parses xml files using xsl style sheet and
I wrote a XML type definition, but it is not correct. Where my mistake?
Ok, I've read a couple books on XML and wrote programs to spit it
I wrote a handler (javax.xml.rpc.handler.Handler) for a SOAP web service that inspects header data.
A while ago I wrote an app that relied on XML coming out of
I wrote a custom xml parser and its locking up on special characters. So

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.