You need to take a 2-pass, or multi-pass approach:
Languages like Java require a multi-pass compiler since the definition of x would not be required to come before the use:
public class Example {
public static void main(String [] args) {
assert(x==0);
x++;
assert(x==1);
}
static int x=0;
}
There are various approaches, for example you could do the following:
The first pass could look for all variable declarations, the second for method declarations, etc. until the last pass uses all this information to compile the final code.