Writing a compiler frontend. When should I check if a variable was declared or not?

99 views Asked by At

I'm currently writing a compiler in C for my own programming language.

Now I'm working on the front-end. I've added functions recently, and now I have troubles with checking if a variable was declared or not.

Before adding functions, declaration of a variable in EBNF looked like this:

<Assign> ::= <VariableDeclarator>? <Lvalue> "=" <Rvalue>

(declaration in my language is available only with initialization).

But now, when I have functions, I have to declare all its parameters before its body.

This brings extra complexity to my lexer, which determines if a variable was declared or not.

I have a single name table for the whole program.

I know that there is an idea of having multiple name tables in a stack, but I don't know if it can help solve my problem.

I don't understand how to check if a variable in a function body was declared or not.

0

There are 0 answers