通过解析纯文本文件生成数据结构 [英] Generating data structures by parsing plain text files

查看:119
本文介绍了通过解析纯文本文件生成数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为正在编写的游戏编写了文件解析器,以使自己可以轻松更改游戏的各个方面(例如角色/舞台/碰撞数据).例如,我可能有一个像这样的字符类:

I wrote a file parser for a game I'm writing to make it easy for myself to change various aspects of the game (things like the character/stage/collision data). For example, I might have a character class like this:

class Character
{
public:
    int x, y; // Character's location
    Character* teammate;
}

我将解析器设置为使用类似于C ++的语法从文件中读取数据结构

I set up my parser to read in from a file the data structure with syntax similar to C++

Character Sidekick
{
    X = 12
    Y = 0
}

Character AwesomeDude
{
    X = 10
    Y = 50
    Teammate = Sidekick
}

这将创建两个数据结构并将它们放在map <std::string, Character*>中,其中密钥字符串是我给它命名的任何名称(在本例中为Sidekick和AwesomeDude).当我的解析器看到一个指向类的指针(例如队友指针)时,它足够聪明,可以在地图中查找以获取指向该数据结构的指针.问题是我无法将Sidekick的队友声明为AwesomeDude,因为尚未将其放置在Character地图中.

This will create two data structures and put them in a map<std::string, Character*>, where the key string is whatever name I gave it (in this case Sidekick and AwesomeDude). When my parser sees a pointer to a class, like the teammate pointer, it's smart enough to look up in the map to fetch the pointer to that data structure. The problem is that I can't declare Sidekick's teammate to be AwesomeDude because it hasn't been placed into the Character map yet.

我正在尝试找到解决此问题的最佳方法,以便可以让我的数据结构引用尚未添加到地图的对象.我能想到的两个最简单的解决方案是(a)添加转发声明数据结构的功能,或(b)使解析器两次读取文件,一次是用指向空数据结构的指针填充映射,第二次是仔细填写.

I'm trying to find the best way to solve this so that I can have my data structures reference objects that haven't yet been added to the map. The two easiest solutions that I can think of are (a) add the ability to forward declare data structures or (b) have the parser read through the file twice, once to populate the map with pointers to empty data structures and a second time to go through and fill them in.

(a)的问题是我还可以决定要在类上调用的构造函数,并且如果我向前声明某些内容,则必须使该构造函数与其余数据区分开,这可能会造成混淆. (b)的问题是我可能想在自己的文件中声明Sidekick和AwesomeDude.我必须使我的解析器能够一次读取一个文件列表,而不是一次读取一个文件(我想这还不错,尽管有时我可能想要从一个文件列表中读取文件列表.文件). (b)还具有无法使用稍后在构造函数本身中声明的数据结构的缺点,但我认为这没什么大不了的.

The problem with (a) is that I also can decide which constructor to call on a class, and if I forward declare something I'd have to have the constructor be apart from the rest of the data, which could be confusing. The problem with (b) is that I might want to declare Sidekick and AwesomeDude in their own files. I'd have to make my parser be able to take a list of files to read rather than just one at a time (this isn't so bad I guess, although sometimes I might want to get a list of files to read from a file). (b) also has the drawback of not being able to use data structures declared later in the constructor itself, but I don't think that's a huge deal.

哪种方法听起来更好?我有没有想到的第三种选择?似乎应该有一些巧妙的解决方案,使用指针引用或绑定之类的方法...:-/我想这基于我想要赋予自己的功能,但在某种程度上是主观的,但是欢迎任何输入.

Which way sounds like a better approach? Is there a third option I haven't thought of? It seems like there ought to be some clever solution to this with pointer references or binding or something... :-/ I suppose this is somewhat subjective based on what features I want to give myself, but any input is welcome.

推荐答案

第一次遇到引用时,只需将其存储为引用即可.然后,您可以将字符,引用或任何其他内容放在以后需要解析的引用"列表中.

When you encounter the reference the first time, simply store it as a reference. Then, you can put the character, or the reference, or whatever on a list of "references that need to be resolved later".

完成文件后,遍历具有引用的引用并对其进行解析.

When the file is done, run through those that have references and resolve them.

这篇关于通过解析纯文本文件生成数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆