Re: Precedence Rules for '$' and '^'

"Russ Cox" <rsc@swtch.com>
Fri, 14 Sep 2007 13:06:52 -0400

          From comp.compilers

Related articles
Precedence Rules for '$' and '^' jamin.hanson@googlemail.com (2007-09-12)
Re: Precedence Rules for '$' and '^' jo@durchholz.org (Joachim Durchholz) (2007-09-13)
Re: Precedence Rules for '$' and '^' jo@durchholz.org (Joachim Durchholz) (2007-09-13)
Re: Precedence Rules for '$' and '^' jamin.hanson@googlemail.com (2007-09-14)
Re: Precedence Rules for '$' and '^' rsc@swtch.com (Russ Cox) (2007-09-14)
Re: Precedence Rules for '$' and '^' jo@durchholz.org (Joachim Durchholz) (2007-09-15)
Re: Precedence Rules for '$' and '^' cfc@shell01.TheWorld.com (Chris F Clark) (2007-09-17)
Re: Precedence Rules for '$' and '^' jamin.hanson@googlemail.com (2007-09-17)
| List of all articles for this month |

From: "Russ Cox" <rsc@swtch.com>
Newsgroups: comp.compilers
Date: Fri, 14 Sep 2007 13:06:52 -0400
Organization: Compilers Central
References: 07-09-035
Keywords: lex

> If an earlier rule starts with '$', does that mean that a subsequent
> rule starting with '^' has the '^' ignored?


A related question, perhaps the one you were asking, is whether once $
has matched, ^ can still match the same position in the case of an
empty line/string.


For example, ^$ clearly matches the empty string, but does $^ match it
too? What about ^$^, $^$, and ^^$$?


The answers you expect depend on whether you think about ^ and $ as
matching positions in a line or imaginary characters at the beginning
and end of the line. In the position model, you'd expect that for an
empty string, both match position 0, so /$^/ and all the others would
work just fine. In the imaginary character model, "abc" is really
"(imaginary-^)abc(imaginary-$)" and the empty string is really
"(imaginary-^)(imaginary-$)". Then you'd expect only /^$/ to match,
of the examples given above.


Most implementations use the position model, but regexp parsers
sometimes get in the way when trying to find out. In Perl, for
example, '' =~ '$$' is true but '' =~ /$$/ is not, because in the
latter, $$ is some interpolated variable. In most greps, ^ and $ are
only recognized as special at the beginning and end of the pattern, so
^$$ is a regexp matching a line containing just a dollar sign, while
in egrep, ^ and $ are always special, so ^$$ is usually a regexp
matching an empty line.


Russ


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.