Advertisement Jump to content
Sign in to follow this  
Mr Lane

Text File Parsing

This topic is 4882 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, I am writing a parser for my text based file format, and am getting into knots: its getting so damn complex, especially when I want to deal with whitespace I need loops everywhere. This is what the format looks like:
Right now I am using a combination of fscanfs() and fgetc() and its becomming very complex. Is there a better way to go about all this? I am reading straight out of the file into my data structures, rather than writing the file to an array in mem then scanning through that. I dont think that would make things nay easier. Thanks Any ideas?

Share this post

Link to post
Share on other sites
The standard approach is to first write an underlying 'lexer' which takes care of identify the basic language components (integers, identifiers, single characters) and handles whitespace/comments automatically.

So basically you'd an interface kind of like this:
struct token {
enum {
} type;

const char *idValue;
signed intValue;
char charValue;

token parseToken();
Often you'll need lookahead too, i.e. the ability to push back the last parsed token back onto the stream.
This is pretty much what lex/flex does for you anyway. And for relatively simple languages like this it should be fairly easy to write it manually.

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. 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!