Jump to content
  • Advertisement
Sign in to follow this  
louie999

How can I make a text parser?

This topic is 1171 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

Hi, so I got a question.

 

How can I make a text parser? like:

1. Open a file.

2. Read the contents of file.

3. Consider something like this in the text file:

NewObject MyObject

HP   = 100
DMG  = 5
Weapon = SomeWeapon

End

How can I make it find keywords such as "NewObject" then save it's value(which is "MyObject") to an std::map or something?

4. How can I make it identify wrong things in the text file? like, if there is a "NewObject" keyword then there should be and "End" keyword, if not then it will produce an error.

 

Is there any good tutorials, c++ functions to allow me make this? Any help is appreciated. Thanks in advance... 

 

Share this post


Link to post
Share on other sites
Advertisement
Thanks guys for the quick answers smile.png I think I'm beggining to understand now. Though, are there also pattern matching in C++? like in Lua I would do something like:
string.match(str, "(.*) = (%d+),(%d+),(%d+)")
to match something like,
something = 1,2,3

Share this post


Link to post
Share on other sites

Using templates you could create something like that, but it isn't an easy beginner task to write.

 

I don't believe there is something like that already in the standard library, but I'm not familiar with all the new C++11 and C++14 library extensions so I could be wrong. C++11 did add a regex library (#include <regex>).

Share this post


Link to post
Share on other sites

C++ has regex now, but regex alone isn't enough to implement a parser for a programming language -- the basic shortcoming is that regex can't count things for itself, and counting tokens in one form or another is necessary (e.g. keeping track of matching brackets is a form of counting) in any programming language you'd want to use. Regex is sufficient for a more declarative sytnax though -- say, key-value pairs for an initialization file.

 

Regex is a reasonable tool for tokenizing symbols though -- you just need to program the parsing/semantic analysis around that.

Share this post


Link to post
Share on other sites
class MyObj
{
string myStr;
int myInt;
float myFloat;

void WriteToStream(ofstream & file)
    {
    file << myStr << ' ' << myInt << ' ' << myFloat << std::endl;
    }

void ReadFromStream(ifstream & file)
    {
    file >> myStr >> myInt >> myFloat;
    }
};

I think the simplest way to do this is as above. The first method uses a stream to output the classes fields in order, with a ' ' delimiter.

The second simply reads the same data back in and the stream will automatically know about the space for the delimiter since that's how streams work.

 

You can write any number of these to the same file and read them in until eof.

 

Otherwise if you really need something more complex, then you probably need to be using xml or json.

Share this post


Link to post
Share on other sites
If you want to be robust and well-designed, I recommend reading up on the following:

https://en.wikipedia.org/wiki/Recursive_descent_parser
https://en.wikipedia.org/wiki/LALR_parser

And examine the tools available to do most of the hard work for you:

https://en.wikipedia.org/wiki/Compiler-compiler
https://en.wikipedia.org/wiki/Category:Parser_generators Edited by Nypyren

Share this post


Link to post
Share on other sites

How can I make a text parser?


I hope you realize this is an entire branch of computer science?

Really, if you're not writing a text parser as an end in itself, choose to use a widespread common format (XML, YAML, JSON, INI) and use a library. There are many, all of which have been tested.

 

Otherwise, look in to writing lexical analyzers, defining grammars built in the lexemes and using semantic insertion, and creating parsers for those grammars.  There's handy tools for that, like boost::spirit (if it's still around).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!