Reputation: 1
I'm coding a compiler for an non-existing language ( it's an example language for a university project). In the section reserved for function definitions, the compiler should be able to correctly compile such definitions:
function a() {
//some code...
b();
//other code...
}
//other function definitions
function b() {
printf("hello\n");
}
The compiler should parse definition for function "suspending" existence checking for function b usage inside function a . At the end of all function definitions, the compiler would check that all function used have been previously defined. Could anyone suggest a smart way to do this?
Upvotes: 0
Views: 165
Reputation: 241731
Unless your grammar has serious ambiguity issues, you should not need to know that b
is a function in order to correctly parse b()
. So on general principles, I'd say the answer is simple: do nothing special and the parse should work fine.
Once you've built your AST, you'll want to do some semantic analysis, which will include verifying that every name used as a function actually is a function. You will probably have other semantic constraints, too. A simple tree walk of the AST should be able to tell you what you need to know.
Upvotes: 2