123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|447|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> On Topic -> Parsing scripts

Sun, 24 Oct 2010, 23:02
Afr0


How would you go about parsing something like this? Or rather... how would you execute it? It seems to me that a function like SetSharedProperties would know about the various UI elements created by the script. But how does it know about the properties of those objects?
Would it be best to run this type of script in a Virtual Machine, as opposed to a 'straigh-ahead' approach where you just parse through the script once and execute the functions?



-=-=-
Afr0 Games

Project Dollhouse on Github - Please fork!
Sun, 24 Oct 2010, 23:59
JL235
I don't quite get what you are trying to do. So I'm just going to answer about how to parse it. I have two methods; a proper technical way and a cheap-ass one that you could get working within a few hours.

The 'proper' way is to use a compiler-compiler to define a 'grammar' for the language. This is to describe what makes up the tokens (<, >, strings, identifiers, =, comments) and you define how it's structured. When a bit is matched some associated code is run. The best thing to do is to have these events build a tree that describes your script. This is how most programming languages are parsed.

The cheap way is to use an XML parser. Because it's not valid XML you'll first need to fix it up, and for this you can use regular expressions. First replace the comments with an empty string (the regex is something like: '#.*$'). Then you need to fix those strings in the attributes which don't have a field (the "BookmarkBackImage" bit in: <DefineImage "BookmarkBackImage"). To fix them you could replace ' "' with 'name="' and so now all of them are stored under a name attribute.

Lots of languages (like C# if your still using it) have an XML parser in-built for you to use. There are also lots of free ones provided online (including one here by HoboBen although I don't know if his supports attributes).

Once you have it parsed into a tree (using either of the two methods) you can navigate around the tree and do whatever it is your doing.

Hope that helps!
Mon, 25 Oct 2010, 00:40
Afr0
I didn't actually write the script, but obviously it describes a UI window and a set of controls (buttons and a listbox in this case). So I'm trying to find the best way to implement a UI system that utilizes these scripts.
Thanks for your advice though... should make it easier to parse at least!

-=-=-
Afr0 Games

Project Dollhouse on Github - Please fork!
Mon, 25 Oct 2010, 01:16
JL235
You'll have to work over the parsed script each node at a time, creating an alternative GUI component for each element you hit.

For example if your writing this in Java then you could use a JTabbedPane for doing the tabs. Whatever your using; pretty much all GUI libraries should be able to provide alternatives to all of those described (like tabs and buttons).

For anyone else, there is more info on grammars and Compiler-Compilers at Wikipedia.
Wed, 27 Oct 2010, 01:40
HoboBen
I wouldn't use a compiler-compiler; does the file actually have a grammar for you to define? It looks made-up-as-it-goes-along.

I'm not sure what <Begin><End> do, but they don't actually look like they actually do anything. Also the tags that are stand-alone aren't terminated, e.g. <ending with this slash />, which could be a problem.

In short, it's a mess and automatically converting it to XML will be a painful experience.

However, it looks like none of the tags actually have any heirachy, which means that you can treat everything like a simple command-based script.

e.g.


Becomes simply:



You'll need a very simple parsing solution to work out var="String", etc.

For each item build a list of variables and values and in the case of e.g. "BookmarkBackImage" on its own perhaps another field for a stand-alone literal.

Then simply switch/case against the first item, e.g. "DefineImage", use the data you've extracted, and try to figure out what to do for each command.

If speed is an issue, "precompile" the scripts by loading them and saving them in a *sensible* format. Probably XML, but a really simple binary with direct integer-to-command mapping would be easy, too.



-=-=-
blog | work | code | more code
Wed, 27 Oct 2010, 02:36
Afr0
Yeah I've already made a list of internal C++ functions that are called by the scripts.
Thanks for your input!

-=-=-
Afr0 Games

Project Dollhouse on Github - Please fork!
Wed, 27 Oct 2010, 08:53
JL235
HoboBen I wouldn't use a compiler-compiler; does the file actually have a grammar for you to define? It looks made-up-as-it-goes-along.

I disagree. It might be made up, but it's consistent with how it's made up. A grammar for this wouldn't be much more difficult then writing one for XML.

I didn't notice the <begin>/<end> tags being different, but you can just replace '<end>' with '</begin>' to fix this. Adding a slash to the end of every tag (except the begin and end ones) should then make it all valid XML.

It's lots of changes, but most of those you can do with a few regular expressions.