Intel's Ronler Acres Plant

Silicon Forest
If the type is too small, Ctrl+ is your friend

Saturday, March 7, 2009

The Curse Of UNIX

Regular Expressions are the work of the devil, nevermind what some Perl apologists say:
WARNING: This article contains more punctuation that many non-programmers like. That's because regexps and finite automata are concise ways of expressing powerful concepts. They are not for the faint of heart. Don't blame Perl if you don't like regular expressions. And don't blame Thompson or anyone else. If you don't like them, don't use them. But don't despise those of us who do.
Perl is it's own language, and unlike other human languages cannot be translated into anything mere humans can read. I suspect it originated with aliens who are trying to subvert our civilization to prepare for their conquest of our planet:
How can I convert my perl scripts directly to C or compile them into binary form?

The short answer is: ``No, you can't compile perl into C. Period.''

However, having said that, it is believed that it would be possible to write a perl to C translator, although it is a PhD thesis waiting to happen. Anyone need a good challenging thesis?

According to some people (alien lackeys, no doubt), Perl is a programming language. Perhaps if you are born and bred to it, you might think so. If you didn't grow up in the UNIX (or Linux) environment, you (and me) are more likely to describe it as gibberish.

So why am I making a fuss about this anyway? Perhaps I like beating me head against a wall. Or maybe I have an NSA complex and think I can crack anything, however inscrutable it appears.

I went to a PLUG meeting the other night down at PSU. Later on I was looking on their web site for a forum where I could post a message. I did not find a forum, but I did find something called IRC, which is what elite programmers used to communicate before there was the internet. You need a special program to use this communications channel. So I downloaded it and took a look at what we've got, and half the program is writen in Perl. So I set about trying to understand what one small file does. It took me a while, but I finally was able to translate it to C. I don't know that it is 100% accurate, there are still a bunch of environmental issues to consider, but I think I captured the essence. So, yes, you could earn yourself a doctorate if you could come up with a program that would translate this stuff to English or C or any other reasonable language.

Here is the Perl program findsyntax.pl:

#!/usr/bin/perl -w

while(<>) {
if(m!/\*.SYNTAX\:! || $tt) {
s/^\s+/ /;
if (/^ [A-Z]+/) {
print "\n";
s/^ //;
}
if (m!\*/!) {
$tt=0;
} else {
$tt=1;
chomp;
}
print;
}
}

And here is my translation to C:

void chomp(char* p1)
{
// Remove all trailing end-of-line characters from a string.

char* p2;

p2 = &p1[strlen(p1) - 1];
while ((p2 > p1) && (*p2=='\n'))
*p2-- = 0;
}

#define isblank(cx) ((cx==' ') || (cx=='\t'))

void findsyntax(void) // uses global variables s & tt
{
while (strlen(s))
{
if (strstr(s, "/*.SYNTAX:") || tt)
{
if (isspace(*s)) // Condense leading white space
{ // to one space.
while (isblank(*s++));
s--; // Point to last blank
s[0] = ' '; // Ensure that it is a space
if (isupper(*s[1]) // Is first non-blank character
{ // a letter?
putEOL();
s++; // move past space to letter
}
tt = !strstr(s, "/*");
if (tt)
chomp(s);
}
printf(s);
}
}
}

I suppose my translation is not much more intelligable, at least to the non-programmer.

No comments: