Developer forums (C::B DEVELOPMENT STRICTLY!) > CodeCompletion redesign

parser library: ELL is quite simple and useful

(1/3) > >>

ollydbg:
Hi, I have just find a Parser library:
ell - Project Hosting on Google Code
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 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.


--- Code: ---#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;

};

--- End code ---

You can see, I have defined three production Rules:


--- Code: ---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

--- End code ---

These grammars were used in the Current CC's parser( As CC use a hand written, heuristic parser  :D)

Here is a test result:


--- Code: ---> 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
>

--- End code ---


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".

ollydbg:
There is a modified grammar: support simple class declaration:

--- Code: ---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;

};

--- End code ---

And here is the test command output:

--- Code: ---> 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
>
--- End code ---


 :D

oBFusCATed:
If you need a C/C++ parsing library look in the clang's sources.
It's parser library should be the best opensource for c and c++ (one day)...

ollydbg:

--- Quote from: oBFusCATed on April 29, 2010, 01:12:18 pm ---If you need a C/C++ parsing library look in the clang's sources.
It's parser library should be the best opensource for c and c++ (one day)...

--- End quote ---
Thanks for the hint. I just remembered that we have discussed clang some months ago. Eran (the author of Codelite IDE) said that Clang is not fully support C++. see here Re: modify codecompletion plugin to macro parser.

So, what I concern is a "fast and heuristic parser", For example, if the parser see some statement like below:


--- Code: ---aaa bbb ccc;
--- End code ---

It will just regard "ccc" as a variable name.

So, we don't need a strict type checking system.

oBFusCATed:

--- Quote from: ollydbg on April 29, 2010, 02:20:39 pm ---Thanks for the hint. I just remembered that we have discussed clang some months ago. Eran (the author of Codelite IDE) said that Clang is not fully support C++. see here Re: modify codecompletion plugin to macro parser.

--- End quote ---
Yes, it doesn't have full support, but there is great progress...
See here: http://clang.llvm.org/cxx_status.html


--- Quote from: ollydbg on April 29, 2010, 02:20:39 pm ---So, what I concern is a "fast and heuristic parser", For example, if the parser see some statement like below:


--- Code: ---aaa bbb ccc;
--- End code ---

It will just regard "ccc" as a variable name.

So, we don't need a strict type checking system.

--- End quote ---
clang has a modular structure -> it is split in libraries
Also clang is meant to be used by IDEs ... for codecompletion and refactoring... xcode use it(not 100% sure).

Navigation

[0] Message Index

[#] Next page

Go to full version