Public Group

# How can I make a text parser?

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

## 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 on other sites
Thanks guys for the quick answers 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 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 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 on other sites
class MyObj
{
string myStr;
int myInt;
float myFloat;

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

{
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 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 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).

1. 1
Rutin
19
2. 2
3. 3
JoeJ
14
4. 4
5. 5

• 18
• 19
• 10
• 13
• 17
• ### Forum Statistics

• Total Topics
631694
• Total Posts
3001751
×