Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which is the order that a compiler compiles

Well, i want to know which is the order that a compiler "read" the code. For example:

Suppose I have the following code snippet:

int N, M;

N = M = 0;

In this case, the compiler would separete a part of memory (int, 4 bytes) for N and M, and then, at the second line (where comes my doubt), of two things, one:

The compiler "read" N equals to M and equals both to zero.

OR

The compiler "reads" the zero, put it in memory of M, then get the value of M, that is zero, and put it on memory of N.

In other words, it is from right to left, or from left to right ?

I don't know if became clear my doubt, but in a test that i made:

int i=0; /*I declared the variable i, and assign zero value to it*/

printf("%d", i++); /*Prints 0*/

printf("%d", i); /*Prints 1*/

I understand the above code, at the second line, the compiler seems(from what i undestood)"read" from left to right, assigning to the type %d the i value, and after print, the variable i is incremented, because at the third line it is printed as 1.

The code snippet below, reverses the position of the ++:

int i=0; /*I declared i variable to zero*/

printf("%d", ++i); /*Prints 1*/

printf("%d", i); /*Prints 1*/

In this case, at the second line, (from what i understood) the compiler "reads" from left to right,and when the compiler reads what will be printed (that stay after the comma, what is the name of this space?), first "reads" the ++ and increments the variable below that is i in this case, and then assign to %d to be printed.

In order, which is the order that a compiler "reads" ? I had some teachers that told me the compiler "read" from right to left, from the semicolon(;), but the compiler actually has an order? And if something that i told above are wrong, please, correct me.(I don't speak english very well)

Thanks!

like image 746
ViniciusArruda Avatar asked Mar 21 '23 05:03

ViniciusArruda


2 Answers

I understand the above code, at the second line, the compiler seems(from what i undestood)"read" from left to right, assigning to the type %d the i value, and after print, the variable i is incremented, because at the third line it is printed as 1.

That is not the order things are being done in.

When you call i++, i is incremented by 1. The value before the increment happened is then returned.

So your: printf("%d", i++)

is actually doing:

int i = somevalue;
int temp = i;
i = i + 1;
printf("%d", temp);

I is NOT incremented after the printing. It was actually before printing.

and when you do: printf("%d", ++i)

it does:

int i = somevalue;
i = i + 1;
printf("%d", i);

The difference is the temp variable. Your teacher is right in this case that it is going from right to left from the semicolon because:

It has to process the value of I first. Then it can print it. In reality, if you break it all up into a separate line per operation or instruction, it's actually going from top to bottom as shown above.

like image 191
Brandon Avatar answered Mar 27 '23 10:03

Brandon


Only the lexical anaylizer reads the source code left to right. The grammar parser builds AST's and reads them in a variety of ways, depending on the particular node it finds.

For expressions, the AST holding the expression may be read in post-order, to generate a postfixed expression, more suitable for a stack based evaluator (easiest to implement).

For assignments (which are indeed expressions), the AST reads and generates code first the RHS, then for the LHS, and then generates the write-to-memory instruction.

For a function call, the AST may be parsed from the last node containing an argument expression to the first one (if using the C calling convention to perform the call).

like image 43
mcleod_ideafix Avatar answered Mar 27 '23 10:03

mcleod_ideafix