linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Glynn Clements <glynn@gclements.plus.com>
To: fabio@crearium.com
Cc: linux-c-programming@vger.kernel.org
Subject: Re: Pattern matching programming
Date: Fri, 20 May 2005 21:36:33 +0100	[thread overview]
Message-ID: <17038.19025.312174.566505@gargle.gargle.HOWL> (raw)
In-Reply-To: <33128.200.91.100.219.1116437772.squirrel@www.crearium.com>


fabio@crearium.com wrote:

> I am trying to code a small C program that basically takes a long text
> file with data that comes from a mysql server.
> 
> But I realize It is better to use regular expression. This is an examples
> of the text:
> 
> =1 <p> blah </p> <div foo>{$foobar}</div>blah.... <p>linux rulez</p>
> misc characters.... =2 blah blah <p> linux rulez again</p>.... <p>foo</p?blah

Don't try to parse anything as complex as HTML/SGML/XML using an
ad-hoc set of rules; use an existing library.

More generally, don't try to perform any non-trivial parsing without
familiarising yourself with the core theoretical concepts behind
formal grammars. A reasonable starting point is:

	http://en.wikipedia.org/wiki/Formal_grammar

If you can't locate an existing library for the language in question,
and you are using C or C++, the appropriate solution is almost
invariably to use lex (or flex) and yacc (or bison) to generate the
parser.

Using an ad-hoc approach (i.e. hand-coding a parser using the
functions in <regex.h> or, worse still, <string.h> or hand-coded
equivalents) is a recipe for producing a parser that is at worst
incorrect (i.e. will produce the wrong result in some cases) and at
best inefficient (it isn't hard to end up with a parser which is a
hundred times slower than an optimal lex-generated one).

-- 
Glynn Clements <glynn@gclements.plus.com>

      parent reply	other threads:[~2005-05-20 20:36 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-05-18 17:36 Pattern matching programming fabio
2005-05-18 20:09 ` Fabrizio Sestito
2005-05-20  1:55   ` Hareesh Nagarajan
2005-05-20 20:36 ` Glynn Clements [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=17038.19025.312174.566505@gargle.gargle.HOWL \
    --to=glynn@gclements.plus.com \
    --cc=fabio@crearium.com \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).