SCRIPTS ? ? ?
I know I will get flamed for this, as its probably been asked 10000+ times and there are probably 10+ functions inthe standard C/C++ libraries for this. Anyway im working on a script engine, I need to know how to split up the line of script code into useable things, for example
CREATE(10,10,firelord) <- lets say that is my script
when I read it in, how do I split up the string so that I know that its calling CREATE how do I seperate the paranthesis from the parameters? In other words, how do I write a script parser.
Any help on this will be greatly appreciated its bugged me for a while cause I have been running with a stupid bad scripting language tat I made years ago. To solve the problem then I didnt use paranthesis but had like only 10 command types.
ohh, also dont send me to the article on this site about script writting, its in VB and that does not help me at all. The therory helps me, but not the actual parseing code. I am working in c/c++
thanks....
something...
November 16, 2000 03:50 PM
You could convert it to tokens perhaps. The first token would contain "CREATE" or whatever the command is.
There''s a function in the C standard library that''s called strtok() that will extract tokens from a string given the token seperators. For a really basic scripting system, it works ok. From there you''d have to do actual strcmp() and atoi(), etc. calls to analyze the extracted tokens.
For more complex languages I recommend using lex and yacc. (flex and bison are some free versions.) The O''Rielly book is really good as a reference.
For more complex languages I recommend using lex and yacc. (flex and bison are some free versions.) The O''Rielly book is really good as a reference.
THANKS! I have been wondering about the same thing FOREVER, I gotta try that
Peon
int ParseFunc(char *szInput, char *szFuncName, char **szParams, int iMaxParams){ char *szPrev, *szCurr, *szNext int iParenLvl = 0, iParmCount = 0; szPrev = szCurr = szNext = szInput; // strip leading spaces for (szNext; *szNext && (*szNext == '' ''); szNext++); szCurr = szNext; // find end of function name by '' '' or ''('' for (szNext; *szNext && (*szNext != ''('') && (*szNext != '' ''); szNext++); memcpy(szFuncName, szCurr, szNext - szCurr); *(szFuncName + (szNext - szCurr)) = 0; // save start and end of function name szPrev = szCurr; szCurr = szNext; // strip spaces between function name and (; error if no ( for (szNext; *szNext && (*szNext == '' ''); szNext++); if (*szNext != ''('') return(-1); // Save position of ( szPrev = szNext; // step past ( and increment paren level szNext++; iParenLvl++; // strip leading spaces before first param for (szNext; *szNext && (*szNext == '' ''); szNext++); // save start of param szCurr = szNext; // while not end of line or closing ) for (szNext; *szNext && iParenLvl > 0) { // find space, ) or , not enclosed in parens, note no " or '' handling for (szNext; *szNext && (((*szNext != '' '') && (*szNext != '','') && (*szNext != '')'')) || iParenLvl > 1); *szNext++) { if (*szNext == ''('') iParenLevel++; else if (*szNext == '')'') iParenLevel--; } // missing closing ) if (*szNext) return(-2); // max params exceeded if (iParamCount >= iMaxParams) return(-3); // save param memcpy(*szParams, szCurr, szNext - szCurr); *(szParams + (szNext - szCurr)) = 0; iParamCount++; // strip trailing spaces from param for (szNext; *szNext && (*szNext == '' ''); szNext++); // if more params continue otherwise exit if (*szNext == '','') szNext++; else if (*szNext == '')'') iParenLvl--; else return(-4); \\ logic error // strip leading spaces from next param for (szNext; *szNext && (*szNext == '' ''); szNext++); // save start of previous param szPrev = szCurr; szCurr = szNext; // next param } if (*szNext != '')'') return(-2); return(iParamCount);}
Now admittedly I just wrote this and didn''t test it, but it should get you in the realm of what you want. If you allow quoted strings then you have to keep track of if you are in a string. When in a quoted string you should replace "" with ". If you allow mixing of '' and " you need to track which started the string. You should also use a function to classify tokens such as iswhite(*szNext) for classifying whitespace. You can also simplify the processing by having the input routine strip whitespace. If you want to allow lines to span lines then you have the input routine read data until it has a full line.
If you want to get into actual expressions, assignment and things more complex than this simple syntax then you should look at more complex means of parsing such as using a graph to describe the syntax and doing by character parsing.
Keys to success: Ability, ambition and opportunity.
November 16, 2000 06:57 PM
One other thing. While it will handle func ( param1 , param2( subparam1 , subparam2 ) ) it will not handle func ( param1, param2 ( subparam1 , subparam2 ) ). That is inconsistant. So if you want to actually handle functions as params then you need to check if ( is the first non-blank character after encountering a blank in a param. If you want to handle expressions then you have to use only , or ) to terminate the param since the example won''t handle a + b either.
Hehe, one last thing of course is that I would have to strongly urge you to consider using Python or one of the other scripting languages freely available. You can then forget about all the parsing, expression evaluation, etc and just focus on what functions you need to expose to the scripting language. With Python you will have to write interface routines, unless you do a COM server, since your parms come in as a single python object. That object is a tuple and then you ask for specific parts of it like getting a record from a database and then asking for specific fields. If you wrap your interface in an object you can simplify the number of interface routines you need dramatically. You can also then make decisions about whether to implement additional functionality as extensions to that wrapper class in python or actually coding a function in your program. I know how to parse, but personally compared to what it would take to code what Python already does it just isn''t worth it. That is without even considering the library of scripts already available for Python such as COM, Sockets, Windows, matrix manipulation, OpenGL, complex numbers, image manipulation and much more.
Keys to success: Ability, ambition and opportunity.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement