C++ FSM 设计和所有权

发布于 2024-09-08 11:38:33 字数 1144 浏览 9 评论 0原文

我想为此语法实现 FSM/“下推自动机”解析器: 解析器范围和条件已经被“词法分析”到有限状态机解析器

我有以下内容:

class State
{
public:
    virtual State* event( const string &token );
    State* deleteDaughter();
private:
    A* m_parent;
    A* m_daughter;
}
class SomeState : public State
{
public:
    State* event( const std::string &token );
}

随着 Bevent() 执行(在许多 if-elseif 之后)return m_parent->deleteDaughter()。我知道这很可疑(并且会崩溃),但我需要方法从女儿 State 返回父 State 并确保女儿 State > 没有泄露。

我的事件循环如下所示:

while( somestringstream >> token )
    state = state->event();

在您责骂设计和最后一段代码之前,我尝试从 这里,看起来还不错。为了清楚和简洁起见,我将决定部分移交给各州本身。

我知道有很多关于这个主题的书籍,但我不是计算机科学家/程序员,我想自己学习做到这一点(当然,在 SO 所有友好人员的帮助下)。如果概念不清楚,请提问。谢谢!

I would like to implement a FSM/"pushdown automaton" parser for this syntax: parser with scopes and conditionals which has already been "lexed" into Finite State Machine parser

I have the following:

class State
{
public:
    virtual State* event( const string &token );
    State* deleteDaughter();
private:
    A* m_parent;
    A* m_daughter;
}
class SomeState : public State
{
public:
    State* event( const std::string &token );
}

With B's event() doing (after many if-elseif's) return m_parent->deleteDaughter(). I know this is fishy (and it crashes), but I need way to return the parent State from the daughter State and make sure the daughter State isn't leaked.

My event loop looks like this:

while( somestringstream >> token )
    state = state->event();

Before you scold the design and last piece of code, I tried extending a much too simple example from here, which seems pretty ok. I am moving the decision part to the states themselves, for clarity and brevity.

I understand there's loads of books on this subject, but I'm no computer scientist/programmer and I want to learn to do this myself (of course, with the help of all the friendly people at SO). If the concept isn't clear, please ask. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迟月 2024-09-15 11:38:33

仍然可以随意发表您对此的看法,但我已经弄清楚如何优雅地处理所有事情:

首先:我的事件循环将保留指向最后创建的 State* 的指针。

第二:每个 State 都有一个指向父 State 的指针,在构造函数中初始化,默认为 0(如果用于除第一个 State*< /代码>);这保证了任何国家都不会超出范围。

第三: State* endOfState() 函数正是这样做的(我对此特别自豪。

State* State::endOfState()
{
    State* parent = m_parent; // keep member pointer after suicide
    delete this;
    return parent;
}

当从子类的 event() 中调用它时,它将正确删除自身,并返回父指针(

如果仍然包含泄漏,请通知我如果解决方案不清楚,请询问:)

PS:为了公平起见,灵感被盗了。 http://www.codeguru.com/forum/showthread.php?t= 179284

Feel free to still post your take on this, but I have figured out how to handle everything gracefully:

First: my event loop will keep a pointer to the last State* created.

Second: Each State has a pointer to the parent State, initialized in the constructor, defaulting to 0 (memory leak if used for anything but the first State*); this guarantees that no State will go out of scope.

Third: State* endOfState() function which does exactly this (and I'm particularly proud of this.

State* State::endOfState()
{
    State* parent = m_parent; // keep member pointer after suicide
    delete this;
    return parent;
}

When this is called from within a subclass's event(), it will properly delete itself, and return the parent pointer (going one up in the ladder).

If this still contains a leak, please inform me. If the solution is not clear, please ask :)

PS: for all fairness, inspiration was stolen from http://www.codeguru.com/forum/showthread.php?t=179284

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文