Sign in to follow this  

scope of the variables

Recommended Posts

Pooya65    127
what can i do for defining scope of variable? (creating a new symbol table for each function and block or ...)

I'm use lex and yacc and i wrote a very basic interpreter. syntax is like as C

Share this post

Link to post
Share on other sites
L. Spiro    25615
Keep a stack of scopes. Each time you enter a new scope, push a scope onto the stack. New variables are always added to the top scope.
This enables you to do as in C++ where a variable can be redeclared in a new scope and the highest-scoped version of that variable is what is used (and this typically only leads to bugs, but you can support it if you want).

When you leave a scope, pop the top scope off the stack and all the variables declared in it are gone. Simple.

L. Spiro

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this