Hi, I have just find a Parser library:
ell - Project Hosting on Google CodeIt is a Embedded LL library. A very light C++ library to embbed EBFN grammars in your code.You can download the source code in
Downloads which contains a simple Calculator sample and a more complex XMLparser sample.
All the grammars can be coded in C++ style.
I have do a simple test( I try to analyze a variable definition or a function declaration). I just modify the Calculator sample. here is the my own defined grammar.
#include <stack>
#include <ell/Grammar.h>
#include <ell/Parser.h>
struct Calc : ell::Parser<char>, ell::Grammar<char>
{
Calc()
: ell::Parser<char>(& root, & blank)
{
flags.look_ahead = false;
state = (+ident) >>( ch(';') [& Calc::Variable]
|ch('(') >> argument >> ch(')')>> ch(';')[& Calc::Function] );
argument = (+ident) >> *( ch(',') >> (+ident));
root = state >> ell::Grammar<char>::end;
ELL_NAME_RULE(state);
ELL_NAME_RULE(root);
ELL_NAME_RULE(argument);
}
double eval(const char * expr)
{
parse(expr);
return 1.0;
}
protected:
void Variable()
{
std::cout<< "This is a variable" << std::endl ;
//return 1.0;
}
void Function()
{
std::cout<< "This is a Function" << std::endl ;
//return 1.0;
}
ell::Rule<char> state, root, argument;
};
You can see, I have defined three production Rules:
statement rule is some statement like: AAA BBB CCC;
Function declaration rule is some statement like: AAA BBB CCC (argument);
argument rule is some statement like(comma separated tokens) : DDD, EEE FFF, GGG
These grammars were used in the Current CC's parser( As CC use a hand written, heuristic parser
)
Here is a test result:
> aaa bbb ccc;
This is a variable
> aaa bbb ccc ddd(int i)
1: before end: expecting [;]
> aaa bbb ccc ddd(int i);
This is a Function
>
So, you can see, it works quite good!
Though I'm not sure ELL can generate more complex parser, but I think it at least can parse "one line statement", which is the base for CC to do "suggestion list".
If you have any interests and comments, please let me know.
BTW: Codelite use the same mechanism (Yacc and Lex) to analyze the "one line statement".