This topic is now archived and is closed to further replies.


Parser Luvin!

Recommended Posts

Moonwalka    122
Hey sorry about this big code input. I''m designing a string parser whose job it will be to execute commands inputted by the user. These commands will be in a proprietary language with my own keywords, flag, attrib, etc. Before I even begin this headache, I wanted to run you guys by my main design strategy and see what you have to say about it. I''ve never done this before, so I''m sure there''s a lot of revising to do. Enjoy! The strategy here is to have two functions and one powerful class object to represent my ''parser.'' These items are: (1) parse() function - iterates and validates inputted text and stores information into a Command object (2) reckon() function - the subroutine of parse() which validates inputted keywords by matching them with acceptable values (3) Command object - holds information regarding a user command that one or several other programs will utilize and execute And so if a user types in: find player[54] hp ...the parse() function will break the string into four parts and feed each keyword into the reckon() function. Those four parts being: (a) find, (b) player, (c) 54, and (d) hp. In case the user would be wanting to call up Player #54''s hitpoints. If all four parts are validated by reckon(), they are stored into the Command object as data members and then managed when other programs call upon the command''s methods. This is the essence of the design. Without further ado.... Command* parse(string* str) { Command* cmd; /* If passed a bad string, create a bad command object and return it as such.*/ if(!str) { cmd(BAD_CMD); return bcmd;} } // Now define the local variables. // // temp A temporary string for copy purposes. // iter An integer iterator for looping through // characters. // loopbreak Used to break the parsing loop when all the // keywords are parsed. string* temp = new string[strlen(str) + 1]; int iter; iter = 0; bool loopbreak = true; // Now the parsing loop. Iterates through ''str'' until it // either finds a space or a null terminator. At that // point it breaks out of the inner loop, resets the // temporary string to the first keyword and runs it // through a reckon() function which matches strings // with my proprietary keywords. If the keyword is // not recognized a bad command object is generated and // returned. Else local variables are reset and it begins // parsing the next keyword. while(loopbreak) { while(str[iter] != '' '' || str[iter] == ''/0'') { if(str[iter] == ''/0'') { // If we reach the null terminator, the // parsing is done. loopbreak = false; break; } // Else copy the passed string into our temp. temp[iter] = str[iter]; iter++; } // Now that we''ve copied our keyword, let''s reset // the null (and remember to include the space between // this keyword and the next) and run the keyword through // our mysterious recog() function, which at the moment, // always returns true. temp[iter+1] = ''/0''; // If the reckon() function recognizes the keyword as // an acceptable value, store it in one of the command''s // data members. This following one statement is shaky // at best, but its for demonstration purposes only, as // it will need to be redefined when I have more energy. cmd.buffer = reckon(&temp); if(!cmd.buffer) { // And if it doesn''t recognize the keyword, // return a bad command. cmd(BAD_CMD); return cmd; } // Otherwise reset the keyword iterator.... iter = 0; // And now make ''str'' equal to itself minus whatever // characters were just parsed in the first keyword. // This statement (I''m sure) is wrong syntax-wise; // I can fix this easily with a ''lil .NET Help Desk // luvin''. But you get the point. ;-) str -= strlen(temp+1); } // At this point the entire inputted text has been parsed, // and if all the keywords, flags, and attribs were recognized, // they have been successfully stored into our Command object. // Equipped with all the information it needs to query various // programs, the Command object is good-to-go and ready to be // returned! (Hopefully) return cmd; } bool recog(string* s) { return true; // Our clever dummy function. }

Share this post

Link to post
Share on other sites