9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Joel Salomon <JoelCSalomon@Gmail.com>
To: 9fans@cse.psu.edu
Subject: Composition of regexps (Was re: [9fans] regular expressions in plan9 different from the ones in unix?)
Date: Fri, 23 Feb 2007 01:27:56 -0500	[thread overview]
Message-ID: <224a39ebca9aaa0370eb804cd59e6aac@plan9.jp> (raw)
In-Reply-To: <ee9e417a0702221548l2caf1a85m276c7b34e2e55b65@mail.gmail.com>

On 2/22/07, Russ Cox <rsc@swtch.com> wrote:
> The Plan 9 regexp library matches the old Unix egrep command.
> Any regexp you'd try under Plan 9 should work with new egreps,
> though not vice versa -- new egreps tend to have newfangled
> additions like [:upper:] and \w and {4,6} for repetition.

This came up as I was implementing my C lexer for the compilers class
I'm taking.  How hard would it be to allow access to regcomp(2)'s
internals, so I could build up a regexp part-by part a la lex?

For example, to recognize C99 hexadecimal floating-point constants, I
wrote a second program that builds up the regexp piece-by-piece using
smprint(2), then compiling the whole thing:

	char	*decdig = "([0-9])",
		*hexdig = "([0-9A-Fa-f])",
		*sign = "([+\\-])",
		*dot = "(\\.)",
		*dseq, *dexp, *dfrac, *decflt,
		*hseq, *bexp, *hfrac, *hexflt;
	dseq = smprint("(%s+)", decdig);
	dexp = smprint("([Ee]%s?%s)", sign, dseq);
	dfrac = smprint("((%s?%s%s)|(%s%s))", dseq, dot, dseq, dseq, dot);
	decflt = smprint("(%s%s?)|(%s%s)", dfrac, dexp, dseq, dexp);
	regcomp(decflt);	// make sure it compiles
	print("decfloat: %s\n", decflt);
	
	hseq = smprint("(%s+)", hexdig);
	bexp = smprint("([Pp]%s?%s)", sign, dseq);
	hfrac = smprint("((%s?%s%s)|(%s%s))", hseq, dot, hseq, hseq, dot);
	hexflt = smprint("0[Xx](%s|%s)%s", hfrac, hseq, bexp);
	regcomp(hexflt);	// make sure it compiles
	print("hexfloat: %s\n", hexflt);

I know that regcomp builds up the Reprog by combining subprograms with
catenation and alternation &c., but I’d be loath to try tinkering
there directly without a much better understanding of the algorithm.
I’ve glanced through the documents at swtch.com/?????  and the regcomp
source code, just haven’t had the time for an in-depth study.

Would such a project be a worthwhile spent of time?  (Might it develop
into the asteroid to kill the dinosaur waiting for it?)

--Joel



  reply	other threads:[~2007-02-23  6:27 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-22 22:16 [9fans] regular expressions in plan9 different from the ones in unix? (at least linux) Folkert van Heusden
2007-02-22 23:17 ` Alberto Cortés
2007-02-22 23:21 ` William Josephson
2007-02-22 23:48   ` Russ Cox
2007-02-23  6:27     ` Joel Salomon [this message]
2007-02-23  6:54       ` Composition of regexps (Was re: [9fans] regular expressions in plan9 different from the ones in unix?) William K. Josephson
2007-02-23 13:34         ` Joel C. Salomon
2007-02-23 17:33       ` Russ Cox
2007-02-23 11:19     ` [9fans] regular expressions in plan9 different from the ones in unix? (at least linux) Gorka Guardiola
2007-02-23 12:12       ` erik quanstrom
2007-02-23 12:17         ` Gorka Guardiola
2007-02-23 13:02           ` erik quanstrom

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=224a39ebca9aaa0370eb804cd59e6aac@plan9.jp \
    --to=joelcsalomon@gmail.com \
    --cc=9fans@cse.psu.edu \
    /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).