Thread: Working with Parser Generators - Functions

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    87

    Working with Parser Generators - Functions

    This maybe is somewhat off topic, but I've been exploring Bison to generate a C parser. The Bison manual has an example (Multi-function Calc) which demonstrates how to describe the rules for parsing a function for the form fctname(argument). I'm wondering if there is a general way to do this for any number of arguments. For simplicity, I will restrict all argument types to double precision floating point values.

    The grammer rules from the example mentioned above are:
    Code:
         exp:      NUM                { $$ = $1;                         }
                 | VAR                    { $$ = $1->value.var;              }
                 | VAR '=' exp        { $$ = $3; $1->value.var = $3;     }
                 | FNCT '(' exp ')'   { $$ = (*($1->value.fnctptr))($3); }
                 | exp '+' exp         { $$ = $1 + $3;                    }
                 | exp '-' exp          { $$ = $1 - $3;                    }
                 | exp '*' exp          { $$ = $1 * $3;                    }
                 | exp '/' exp          { $$ = $1 / $3;                    }
                 | '-' exp  %prec NEG { $$ = -$2;                        }
                 | exp '^' exp         { $$ = pow ($1, $3);               }
                 | '(' exp ')'             { $$ = $2;                         }
    The 4th line is of interest to me:
    Code:
                 | FNCT '(' exp ')'   { $$ = (*($1->value.fnctptr))($3); }
    If worst came to worst, I could make similar rules for 2, 3, ... arguments up to the max number of the functions I need to work with. However, I was hoping to allow for a plug-in system where users could provide their own functions which would be read in via dlopen()/dlsym() and passed along to the symbol table used by the parser. Thus, a more general approach is more desirable.

    Any one know of a way to do this?

    Thanks,
    Jason

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    could you make another symbol for 'argument'? for example:
    Code:
         exp:      NUM                { $$ = $1;                         }
                 | VAR                    { $$ = $1->value.var;              }
                 | VAR '=' exp        { $$ = $3; $1->value.var = $3;     }
                 | FNCT '(' arg ')'   { $$ = (*($1->value.fnctptr))($3); }
                 | exp '+' exp         { $$ = $1 + $3;                    }
                 | exp '-' exp          { $$ = $1 - $3;                    }
                 | exp '*' exp          { $$ = $1 * $3;                    }
                 | exp '/' exp          { $$ = $1 / $3;                    }
                 | '-' exp  %prec NEG { $$ = -$2;                        }
                 | exp '^' exp         { $$ = pow ($1, $3);               }
                 | '(' exp ')'             { $$ = $2;                         }
    
         arg:   exp
              | exp, arg
    maybe its just been too long since ive done one of these, but let me know if this helps at all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. working with functions
    By robasc in forum C++ Programming
    Replies: 13
    Last Post: 12-27-2005, 01:38 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Problem with a file parser.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 03-17-2005, 09:54 AM
  5. Passing data/pointers between functions #2
    By TankCDR in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 09:49 PM