简体   繁体   English

持有对地图矢量的地图参考

[英]Holding a map reference to a vector of maps

My task is to construct a compiler which lexes, parses and analyses the resultant Abstract Syntax Tree to ensure type matching, avoid duplicate declaration etc. 我的任务是构造一个编译器,对最终的抽象语法树进行词法分析,分析和分析,以确保类型匹配,避免重复声明等。

It was instructed for us to construct a Symbol Table, which holds a map of variables and their types for each scope. 指示我们构造一个符号表,其中包含每个作用域的变量及其类型的映射。 I opted for a vector of maps. 我选择了地图矢量。 I preferred this over a stack since I can iterate over it when checking for variables at any scope. 我首选此方法而不是堆栈,因为在任何范围内检查变量时都可以对其进行迭代。

I constructed Push, Pop, Lookup and Insert operations for this structure as shown below and my idea was to hold a reference to the latest map in the vector and add variables to it. 我为此结构构造了Push,Pop,Lookup和Insert操作,如下所示,我的想法是在向量​​中保留对最新映射的引用,并向其中添加变量。 As a new scope is entered a push operation is carried out creating a new map in the vector within which to store the arrays. 输入新的作用域后,将执行推入操作,以在向量中创建一个新映射,以在其中存储阵列。

When a scope is exited a Pop operation is done to remove the map at the end of the vector and acquire the previous map which is now at the back of the vector. 当退出示波器时,将执行弹出操作,以删除矢量末端的地图,并获取之前位于矢量背面的先前的地图。

Via debugging I've noticed the vector is simply holding no map details and working by reference seems to be doing nothing to update the vector that's supposed to be holding this map. 通过调试,我注意到该向量只是不保存任何地图详细信息,并且通过引用进行的工作似乎无济于事以更新应该保存此地图的向量。 How do I correctly reference a map inside a vector and maintain this structure? 如何正确引用向量内部的地图并维护此结构?

Symbol Table: 符号表:

struct SymbolTable {
        // Stack defining scopes holding identifier / type details
        std::vector<std::map<std::string,std::string>> _scopeVector;

        // Tracks current working stack
        std::map<std::string,std::string> _currentMap;

        SymbolTable() = default;

        void Push() {
            std::map<std::string,std::string> *_tempMap;
            _tempMap = new std::map<std::string,std::string>();
            _scopeVector.push_back(*_tempMap);
            _currentMap = _scopeVector.back();
        }

        void Insert(std::string p_name, std::string p_type) {
            _currentMap.insert(std::make_pair(p_name,p_type));
        }

        // Returns type if found, empty if not
        std::string Lookup (std::string p_name) {
            for (int i = 0; i < _scopeVector.size(); i++) {
                if (_scopeVector[i].find(p_name) == _scopeVector[i].end()) {
                    // No match yet
                } else {
                    return _scopeVector[i].find(p_name)->first; // return var name
                }
            }
            std::cerr << "Type name " << p_name << " not found in all of stack" << std::endl;
            return "";
        }

        void Pop () {
            _scopeVector.pop_back();
            _currentMap = _scopeVector.back();
        }
    };

    SymbolTable *ST;

Class constructor which sets up the Symbol Table: 设置符号表的类构造函数:

SemanticAnalysisVisitor() {
    ST = new SymbolTable();
    ST->Push();
}

Debugger image of an empty vector but populated map 空向量但已填充映射的调试器图像

As for your problem (I think ) the statement 关于你的问题(我认为

_currentMap = _scopeVector.back();

copies the map from the vector. 从向量复制地图。

_currentMap will always be a separate and distinct map, totally unrelated from whatever is in the vector. _currentMap将始终是单独且不同的映射,与向量中的任何内容完全无关。

It seems you want to use references, but that's not possible in this case (unless you do a redesign). 似乎您想使用引用,但这在这种情况下是不可能的(除非您进行重新设计)。

You could solve this problem (and the memory leak I mentioned in a comment) by having a vector of (smart) pointers to the maps, and make _currentMap a (smart) pointer as well. 您可以通过使向量的(智能)指针指向向量,并使_currentMap成为(智能)指针,来解决此问题(以及我在评论中提到的内存泄漏)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM