Hi, I have just find a Parser library:
ell - Project Hosting on Google Code (http://code.google.com/p/ell/)
It 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 (http://code.google.com/p/ell/downloads/list) which contains a simple Calculator sample and a more complex XMLparser sample.
All the grammars can be coded in C++ style. :D
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 :D)
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. :D
BTW: Codelite use the same mechanism (Yacc and Lex) to analyze the "one line statement".
There is a modified grammar: support simple class declaration:
struct Calc : ell::Parser<char>, ell::Grammar<char>
{
Calc()
: ell::Parser<char>(& root, & blank)
{
flags.look_ahead = false;
state = ( str("class") >> ident >> block >> ch(';') [& Calc::ClassDecl])
| (+ident) >>( ch(';') [& Calc::Variable]
|ch('(') >> argument >> ch(')')>> ch(';')[& Calc::Function] );
argument = (+ident) >> *( ch(',') >> (+ident));
block = ch('{')>> (*state) >> ch('}');
root = state >> ell::Grammar<char>::end;
ELL_NAME_RULE(state);
ELL_NAME_RULE(root);
ELL_NAME_RULE(argument);
ELL_NAME_RULE(block);
}
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;
}
void ClassDecl()
{
std::cout<< "This is a Class Decl" << std::endl ;
}
ell::Rule<char> state, root, argument, block;
};
And here is the test command output:
> class A;
1: before ";": expecting block
> class A {int a;};
This is a variable
This is a Class Decl
> int aaa;
This is a variable
> class A {int a; int b; int c};
This is a variable
This is a variable
1: before "};": expecting [;] or ([(] argument [)] [;])
> class A {int a; int b; int c;};
This is a variable
This is a variable
This is a variable
This is a Class Decl
>
:D