OO simple text parser

Status
Not open for further replies.

snotnose

Ars Praefectus
3,454
Subscriptor
(tl;dr: How to design an object oriented log file parser. Not exactly, but close enough)

First off, this has nothing to do with networking. My example matches my data format without me needing to describe things you've never heard of. This is not a question on actually parsing data, but on an OO design.

I've got lines of text that are formatted as:

Nxx [type] (N1, N2, ..., Nx) (Mem=x, Disc=x, Eth=x)

N is the node ID, 1-1024
type is the resource type
(N1, N2, ..., Nx) are node IDs that directly connect to this node, so far the most I've seen are 5 but theoretically it could have 1023 connections.
Mem=x is the memory, Disc available disc space, Eth the number of network connections, etc. If the value is 0 the whole field is not present.

For example:

N12 [printer] (30) (Mem=34, Eth=1)
N18 [server] (12, 17, 36, 120) (Mem=194, Disc=740, Eth=4)
N129 [router] (13, 94, 248) (Eth=16)

Not all nodes are defined. That is, there may not be a node 42 at all. That line will not be present (e.g. there are no lines "N42 [] () ()").

More (almost) irrelevant details. Node IDs and connectors don't change, everything else does. Yes, my printer can turn into a router with 1 connection (told you this had nothing to do with networking). Second, each file has a header with a source and a date, my save file also keeps track of the last time a line changed and the source of the change.

I've done this sort of thing several times in awk and perl via regexps and stdin/stdout. But now I want to do it in an object oriented way, probably in Java.

I can think of 2 ways to do it. First, the core object is the node. The main data structure is an array of 1025 nodes (node 0 doesn't exist and I don't want to dork with converting node[0] to N1). The node class reads a line and fills itself in. This seems like procedural programming with OO lipstick.

The other way I can think of is to have the parser itself be the core object, returning a ID:VALUE pair. Ex: ID:12, TYPE:printer, CONNECTION:30, etc. But again, this just seems like adding OO lipstick to a procedural program.

The goal? I get reports from various places at various times. Each report has a partial view of the entire system. I want to read a save file, parse a report, update the database, and write a new save file. I'm currently using bash, sort, and Vi to maintain my central database, can you say "pain in the keister?"

Keeping in mind actually parsing the line isn't the problem, but the OO design of the beast is: Comments?
 

Hattori HANZo

Ars Scholae Palatinae
1,205
[url=http:/civis/viewtopic.php?p=25609301#p25609301:3ljfxamc said:
snotnose[/url]":3ljfxamc]
I've done this sort of thing several times in awk and perl via regexps and stdin/stdout. But now I want to do it in an object oriented way, probably in Java.
[...]
Keeping in mind actually parsing the line isn't the problem, but the OO design of the beast is: Comments?

So, I guess you're in for the fun of it?
I'll probably get harshly critiqued for neither being a good OO-programmer nor having any idea of how to write a parser, but I feel like driveling.

I'd first try to find a structure that makes sense for representing the case at hand. You don't have a formal grammar provided and for some details you should formalize this more, imo. For example, would "N12 [printer] (Mem=12)" be a valid line? Could you rely on getting an empty set of parens? Is the order of the list of connected nodes and properties fixed, or could you also get "N12 [printer] (Mem=13) (N34, N15)"?

If you know that kinds of things, translate the line structure into an object structure. I see "token token tokengroup tokengroup" here. You also could further differentiate between the node list kind of tokengroup and the property list kind. Tokens are of type node, type, and property. What kind of object scheme could we construct from this?

Well, we have an abstract token. There's three derived classes representing each kind, each implementing the abstract parse method inherited from the abstract base class. You could have the node token be abstract and have two derived classes, one representing the kind of node token that is the node and one representing neighbouring nodes. The parse functions here would be trivial, like looking if the string representing the type is in a list you defined and then setting the enum appropriately, which is the only attribute the class would have, or seeing if the string morsel it gets starts with an N and then trying to parse an int from the space-stripped remainder, again being the only attribute.
Then we have a tokengroup. The method that splits them could maybe not be abstract, as it is the same for both groups, they're separated by commas, but you need to return different kinds of tokens. You could have the base class determine the kind to make it inheritable, but that's besides the point of the base class being unspecific and unaware of descendants. I'd rather have the split method just split by commas and make the two tokengroup classes wrap the results in different types (node tokens and property tokens).

So the thing should kinda go like this:
bla = new NodeObject(LineAsText)
This calls functions to extract token groups (stuff inside parens) and single tokens. The tokengroup with properties gets further split down into single tokens.
These tokens get handed back and filled into the class representing a line of your input. Now you have parsed a line. All you need is a wrapper that takes a file and feeds it line by line into the constructor that starts the parsing and saves the resulting object into a list or maybe an array list.

How about errors?
Well, it depends. If there are complex requirements, it might make sense to distinguish between the intermediate representation as a set of tokens and the result, so you can have a method for an intermediate object to be validated instead of just trying to fill in what happens to be there.


This strikes me as kind of a parody of object orientation, if I think about it. If I would have to deal with this specific task and your specific example, I'd cut out most of the objects and would probably live with OO-lipstick on the procedural pig.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
There's nothing much OO about the problem, so why model it that way?

It's feasible to have a format where the line contains enough info to a simple parser to detect which type it is, then hand of parsing the full thing to a newly created instance of it via a virtual method call. However that's nasty in other ways because them you start needing to slacken other desirable to model with the type system aspects, like immutability. In addition error handling becomes complicated.

If the set of types is small there's not a problem having an explicit static Parse(reader) -> instance on each type, and having a switch statement in the parser code. Unless you have a plugin highly dynamic architecture you're done. Even if it were runtime discoverable the convention would be pretty simple so long as your platform/language of choice has a strong reflection API.
 
Status
Not open for further replies.