解析具有未知数量嵌套语句的日志文件

发布于 2024-11-29 11:33:36 字数 452 浏览 0 评论 0原文

我有一个以下形式的日志文件:

begin; x1
begin; y1
end; y1
begin; z1
begin; z2
end; z2
end; z1
end;x1

我希望将此文件解析为如下所示的数据结构:

x1 >
    y1
    z1 >
        z2

因此 x1 事件包含 y1 & z1 事件,并且 z1 事件包含 z2 事件。

在这种情况下是否有可能使用的标准算法?

我想也许递归可以通过在每个“开始”语句上分支来正确解析所有子事件来帮助我。如有任何建议,我们将不胜感激。

编辑: 最终目标是在分层 ListView 类型组件内的 GUI 上显示事件。我希望通过能够像这样显示日志文件,可以更好地可视化系统内的事件序列。

I have a log file in the form:

begin; x1
begin; y1
end; y1
begin; z1
begin; z2
end; z2
end; z1
end;x1

I am looking to parse this file into a data structure that could look like the following:

x1 >
    y1
    z1 >
        z2

so the x1 event contains the y1 & z1 events and the z1 event contains the z2 event.

Is there a standard algorithm that might be of use in this situation?

I'm thinking perhaps recursion might be able to help me here by branching on each 'begin' statement to correctly parse all sub-events. Any suggestions would be gratefully received.

Edit:
The ultimate goal for this will be to display the events on a GUI within a hierarchical ListView-type component. I am hoping that by being able to display the log files like this it will be possible to better visualize sequence of events within my system.

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

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

发布评论

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

评论(2

嘿嘿嘿 2024-12-06 11:33:36

我会选择递归下降解析器。

LogTree Parse()
{
    LogTree current = new LogTree();
    if (!ReadBegin(current))
        return null;
    LogTree child = null;
    while ((child = Parse()) != null)
    {
        current.Chilren.Add(Child);
    }
    if (!ReadEnd(current))
        return null;
    return current;
}

bool ReadBegin(LogTree current)
{
    if (nexttoken != "begin")
        return false;
    readNextToken();
    current.Name = nexttoken;
    readNextToken();
    return true;
}

bool ReadEnd(LogTree current)
{
    if (nexttoken != "end")
        return false;
    readNextToken();
    if (current.Name != nexttoken)
        return false;
    readNextToken();
    return true;
}

等等。

这里我们有

class LogTree
{
    public string Name;
    public List<LogTree> Children = new List<LogTree>();
}

I would go for a recursive descent parser.

LogTree Parse()
{
    LogTree current = new LogTree();
    if (!ReadBegin(current))
        return null;
    LogTree child = null;
    while ((child = Parse()) != null)
    {
        current.Chilren.Add(Child);
    }
    if (!ReadEnd(current))
        return null;
    return current;
}

bool ReadBegin(LogTree current)
{
    if (nexttoken != "begin")
        return false;
    readNextToken();
    current.Name = nexttoken;
    readNextToken();
    return true;
}

bool ReadEnd(LogTree current)
{
    if (nexttoken != "end")
        return false;
    readNextToken();
    if (current.Name != nexttoken)
        return false;
    readNextToken();
    return true;
}

etc.

Here we have

class LogTree
{
    public string Name;
    public List<LogTree> Children = new List<LogTree>();
}
夏末染殇 2024-12-06 11:33:36

为什么不将其转换为 XML 作为使用数据的最简单方法:

var xml = XDocument.Parse(string.Join("",text.Replace("; ", ";")
                                .Split(' ')
                                .Select(i => i.StartsWith("begin;") ? 
                                    i.Replace("begin;", "<node>") : "</node>")));

why not convert it to XML as the easiest way to use the data:

var xml = XDocument.Parse(string.Join("",text.Replace("; ", ";")
                                .Split(' ')
                                .Select(i => i.StartsWith("begin;") ? 
                                    i.Replace("begin;", "<node>") : "</node>")));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文