   Coding a parser: problem
  hi,i´m working on a scripting language for some time now.everything works almost fine.but the problem is the unflexibility:currently, my parsing code for arithmetic expressions looks kinda ugly this:   if (operator == ''+'')    if (arg1 == CONSTANT && arg2 == CONSTANT)   if (data_type(arg1) == FLOAT || data_type(arg2) == FLOAT)  ....  else   ....    else if (data_type(arg1) == FLOAT || data_type(arg2) == FLOAT)  ....  else   ....    elseelse if (operator == ''-'')   if (arg1 == CONSTANT && arg2 == CONSTANT)   if (data_type(arg1) == FLOAT || data_type(arg2) == FLOAT)  ....  else   ....    else if (data_type(arg1) == FLOAT || data_type(arg2) == FLOAT)  ....  else   ....    elseend ifyou see the problem. but i haven´t yet found a solution, maybe some class design pattern.... nothing.templates don´t work since the parameters are runtime-dependent.can someone give me a hint? 
  Rule of thumb: Don''t fix something that isn''t broken!If it works, I see no reason why (apart from maybe replacingoperators with symbols and doing a switch on these symbols)you should alter working code.You could do some tricks, sure.They would propalby make the code look ''less ugly'', but Idon''t believe the code would be faster afterwards...;  Look at Boost''s Spirit or HAPY. ;  Use the power of virtual functions.  You don''t need templates.  One solution is to have everything in the form of a tree.  In an ''add'' node place objects derived from a generic ''Number'' class.  You may have Floats, Ints, etc.  Then your add logic would become something like:Number Add::execute() {  NumType t1 = arg1->type(); //''type'' returns enumeration values  NumType t2 = arg2->type();   if (t1 == t2) return arg1->add(arg2);  else if (t1 < t2) {    arg2->promoteToMyType(arg1); //A generic number may have a    //temp variable inside it for use    return arg2->addWithTemporary();    }  else { //t1 > t2    arg1->promoteToMyType(arg2);    return arg1->addWithTemporary();    }}That would be the entire function.  I leave the details to you for how the ''promoteToMyType'' function may work and the ''addWithTemporary'' - they should not be very hard.HTH,David;  Consider using tools like flex/lex and bison/yacc. ;  you might also want to check out recursive decent parsers. I wrote a couple when i was in school and they seemed to work out nicely. ;  You''re trying to perform the functions of the lexical scanner and  the parser at the same time. Don''t do that. Separate these into two modules, and let the parser access the scanner through a clearly defined interface(a function like GetNextToken() or something like that). You are trying to do way too much here.   --AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [Blog] [RSS] [Browse the source] [IRC channel] ;  I''m not sure he''s talking about parsing at all. That looks like the code for an evaluator.- Josh;  Actually, my parser works recursive-descent, and i already havea separated scanner module which extracts the lexemes and converts it to a token.each non-terminal symbol has a related procedurefor example, my expression parsing code looks like this:to simplify it here, i only post code for going down the term layer, actually, the parse_term() calls parse_factor() to handle the higher precedence multiply and divide operations as well as operands and braces. parse_expr(){    parse_term();    while (true)    {   scanner.get_next_token();   if (currToken.type != TOKEN_ADD && currToken.type |= TOKEN_SUBTRACT)   {  scanner.rewind_token_stream();  break;   }  int op = currToken.type; parse_term();   if (op == TOKEN_ADD)  // insert "ADD" instruction   if (op == TOKEN_SUBTRACT)  // insert "SUB" instruction    }} [edited by - Gammastrahler on February 6, 2004 9:38:27 AM] [edited by - Gammastrahler on February 6, 2004 9:39:19 AM];  It looks like you have done a pretty good job of doing your job, and you have found the limitation to your approach (if you call it a limitation - it is really just more work and tedium.)  If you want to see another way (other than the other projects the contributors have recommended) you may see mine:http://www.randommonkeyworks.com/Programming/compiler/compiler_agreement.htmlIt is fairly easy to modify my approach to use the method I suggested in my original post.Good luck with your project!David 