Sign in to follow this  

Parsing - Regex??

This topic is 3201 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

This topic is 3201 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this