Parsing - Regex??

Started by
5 comments, last by harshman_chris 15 years, 1 month ago
Hello, So I have a rather complex problem, and I have not really found a complete answer anywere. I am currently working on a game with XNA, now I have created a commandline debugger for this, which makes my life really easy, but I need the ability to create objects at runtime, without knwoing before what that object will be. So My question is if I have a line such as this: PhysicsActor("Content/ig_box", new BoxObject(new Vector3(2.5f), new Vector(256, 100, 256), Vector3.Zero)) This line will be entered into the commandline, basicly I need to parse through this and pull out each parameter which is delimited by the ',' but as you can see with this line there are Sub Objects inside that. Now I have heard word that Regex can do things like this, but I have been unable to find exactly how. Basicly I need a object[] that contains each parameter, if there are sub parameters then the object[] will hold a object[]. As for the rest I do not need help with creating the object just pulling out the parameters. Thanks Chris
Advertisement
You need to parse yor expression. To do this you can use a library like boost::spirit: Boost::Spirit . It could be helpful to understand how a parser works. Just search for parser in google should show you some infos regarding this.

Kimmi
A complicate solution may indicate a not understood problem.


[twitter]KimKulling[/twitter]
As Kimmi said, you should look into a more full fledged parser for this task. The problem is that standard regular expressions do not deal well with things like nested parens; because they generally model a DFA (deterministic finite automaton). DFAs have no memory, so "counting" the nesting level becomes problematic. Parsers can express more general grammars and can be based on more powerful formalisms (like the pushdown automaton). Give some of these parsers a look and see if they serve your purpose.

Conversely, you could implement some sort of prefix tag system, where prior to an expression you detail all the of the information the regular expression parser would need to know

PhysicsActor("Content/ig_box", new BoxObject(new Vector3(2.5f), new Vector(256, 100, 256), Vector3.Zero))

might become:

[2,3,1,3] PhysicsActor("Content/ig_box", new BoxObject(new Vector3(2.5f), new Vector(256, 100, 256), Vector3.Zero))

This is just off the top of my head, and you can see how messy it might become, but the list of numbers that precedes the line tells (from left to right) how many arguments each encountered function takes. Physics Actor takes 2, BoxObject takes 3 Vector3 takes 1 and Vector takes 3. That way, every time you encounter a '(' you know how to parse the intervening expression. As you can see, this is messy, hackish, and probably not very extensible at all; I suggest you go with a proper parser :).

Cheers,
Rob
You can also use a existing script language like lua. Lua brings a working and well tested parser with it. And it is not such a hard task using luabind. Here is a short tutorial how to do that: Tutorial .

Kimmi

[Edited by - Kimmi on March 13, 2009 5:39:58 PM]
A complicate solution may indicate a not understood problem.


[twitter]KimKulling[/twitter]
Unfortunatly I am limited by the XNA 360, so right now, I am sure of the extend of libraries I can use.

Plus I want to learn the hard way, rather than just find something that does it for me.

One of my biggest problems is that it needs to be extensible, I can right a hackish method, and the debugger is for developing only, although I intend to add a basic script system afterwards to ease the game production.

I know of a few projects that are making lua usable on the Xbox 360 but they are far from complete, at least the ones I have found.

Plus I dont even get to use nice tools like C# Reflection, I had to develop a method to access and create objects by hand for that.

So it kinda makes find a library useless, which is unfortunate.
Google "SLR parser by hand" and "C language grammar"

If your brain explodes, you might as well embed your scripts in the game itself.
Quote:Original post by Nypyren
Google "SLR parser by hand" and "C language grammar"

If your brain explodes, you might as well embed your scripts in the game itself.


It just so happens Brain Exploding is something I am familar with, I have never read a book or had a lesson that has taugh me more than Hands on grinding out through mindless amounts of infomation.

Thanks for the advice, I sense many long nights ahead.

The only real issue is that I am required to work off a C# base.

Edit:

Hmm found a really good article in C# with code samples, by playing with those words, you gave me and following about 1/2 dozen wiki links, ended up with some lecture notes here.

http://www.itu.dk/people/kfl/parsernotes.pdf

They seem really useful so far, never would have found this otherwise.

[Edited by - harshman_chris on March 13, 2009 11:07:11 PM]

This topic is closed to new replies.

Advertisement