开发者

Parse log file with an unknown number of nested statements

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 开发者_如何学Pythonbe 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.


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>();
}


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>")));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜