Original Post
I have a simple recursive descent parser which parses and interprets most expressions okay. This includes manipulation of variables, which are stored in a global symbol table (ie. an std::map of name -> value pairs). What I need to do is extend this so that I can support (a) arrays/collections of variables, and (b) composite variables, like structures/classes in C/C++. The general plan is to treat array indexing and member resolution in the same way, so that obj.abc is equivalent to obj["abc"]. From memory I believe javascript and some other languages work this way, and it should simplify the code. I am also hoping that I can effectively get namespaces as a side-effect of this. Being very rusty at this sort of thing, my first grammar attempt looks like this:
variable --> varName IndexExpr | varName
IndexExpr --> dotIndexExpr | bracketIndexExpr
dotIndexExpr --> '.' variable
bracketIndexExpr --> '[' expression ']'
Assume expression and varName are defined already and are reasonable. For instance, expression includes constants and variables. I can see the above doesn't allow for var1[20].var2, but my idea to fix it would require a lot more rules, which I'm sure I don't actually need. Any suggestions? I'm sure that I just can't see the wood for the trees here. Also, any hints on how to implement this generally, especially with regard to the 'every object has its own symbol table' concept (or viewed conversely, 'the global namespace is just an unnamed object' concept), and handling this at arbitrary recursion depths. Any insights from anyone who's done this, especially in the context of an interpreter rather than a compiler, would be welcome.
