Click to See Complete Forum and Search --> : lexical addressing sucks


sans-hubris
03-25-2002, 01:40 AM
I have to write a program in Scheme to replace variables with their lexical addresses and IT'S FREAKING HARD!!!. Later, I have to "un-lexical address" them and get the original variables back. And I like this professor too!!!

Bradmont5
03-25-2002, 02:04 AM
2 questions:

1: Why would you like a professor who gives you easy assignments?

2: What's lexical addressing?

sans-hubris
03-26-2002, 01:16 AM
Originally posted by Bradmont5:
<STRONG>2 questions:

1: Why would you like a professor who gives you easy assignments?

2: What's lexical addressing?</STRONG>
I just figured it all out, and it was easier than I thought it would be.

1: You're right, I wouldn't like a professor who gave us easy assignments, but I didn't expect this kind of difficulty.

Anyway, lexical addressing is a way of getting rid of variable names in code.

In order to demonstrate this so that everyone here can understand, I'll stick to C. (Thankfully, I did not have to parse C code, which has syntax that's extremely complex.)

As you may (or may not) know, you can define blocks of code with '{' and '}' and variables within that block of code can be seen only within that block of code. You may also know that you can define variables inside that block with the same name as variables outside that block, and all subsequent code that address that name will only see the variable within that block of code, not the one(s) outside it. E.g.:

{
int a, b=2, c=3;

a=b + c;

{
int a, z=5, y=6;

a=z * y * b;

printf("%d\n",a); //prints "60"

//So how the heck does the compiler know which
//'a' to use, and where 'b' is?
//Answer: lexical addresses are assigned to all variables.

}

printf("%d\n",a); //prints "5"
}

A lexical address might look something like this:
(v : d p)
where 'v' is the variable name (optional), 'd' is the depth (zero is the current depth), and 'p' is the position of the variable in that depth. So the above code might look like this after being run through a lexical analyzer:

{
int a, b=2, c=3;

(a : 0 0)=(b : 0 1) + (c : 0 2);

{
int a, z=5, y=6;

(a : 0 0)=(z : 0 1) * (y : 0 2) * (b : 1 1);

printf("%d\n",(a : 0 0));

}

printf("%d\n",(a : 0 0));
}

It should be noted that there are also free variable which are basically variables not defined within local scope. printf() would also be a free variable as it is not defined within that scope. So in the above code, 'b' is a free variable within the inner-most scope. There are also bound variables, which, as you may have guessed (or not), are variables defined within a certain scope.

So what would the original code below look like?

{
int a=1;
{
int b=2;
{
int c=3
{
int d;
(: 0 0)=(: 3 0) + (: 2 0) + (: 1 0);
}
}
}
}


[ 26 March 2002: Message edited by: sans-hubris ]