lukeparser / pybison

Use Bison directly from Python
https://pypi.org/project/pybison
GNU General Public License v2.0
26 stars 6 forks source link

The library does not handle action (call the function in part 3 of the bison file) #31

Open leviettung200 opened 2 years ago

leviettung200 commented 2 years ago

My bison file contains the action in Grammar rules. This action will call the functions declared at the end of the bison. However your library doesn't handle it, what can I do to fix it? Functions declared at the end of the bison file are necessary for business logic.

Sample Grammar rule:

CMD_LINE_RR :
    CMD_TOKEN_RR LT_INTVAL LT_COMMA LT_TEXT_EX LT_RETURN
    {
        _CMD_START("CMD_LINE_RR(0)");
        cmd_rr(cmd_line , $2, $4, "", "", "", "", "", "", "", "", "" );

        next_cmd();
    }
    |CMD_TOKEN_RR LT_INTVAL LT_COMMA LT_INTVAL LT_COMMA LT_INTVAL LT_COMMA LT_INTVAL LT_COMMA LT_INTVAL LT_COMMA LT_INTVAL LT_COMMA LT_INTVAL LT_COMMA LT_INTVAL LT_COMMA LT_INTVAL LT_COMMA LT_INTVAL LT_COMMA LT_INTVAL LT_RETURN
    {
        _CMD_START("CMD_LINE_RR(1)");
        cmd_rr(cmd_line , $2, $4, $6, $8, $10, $12, $14, $16, $18, $20, $22 );

        next_cmd();
    }
    |CMD_TOKEN_RR LT_INTVAL LT_COMMA LT_INTVAL LT_COMMA LT_INTVAL LT_COMMA LT_INTVAL LT_COMMA LT_INTVAL LT_COMMA LT_INTVAL LT_COMMA LT_INTVAL LT_COMMA LT_INTVAL LT_COMMA LT_INTVAL LT_RETURN
    {
        _CMD_START("CMD_LINE_RR(2)");
        cmd_rr(cmd_line , $2, $4, $6, $8, $10, $12, $14, $16, $18, "", "" );

        next_cmd();
    }
;

Sample c code(function):

int _CMD_START(char *s){
    #ifdef DEBUG
    printf("[%s][Line:%04d](+_+)%s\n",__FUNCTION__,__LINE__,s);
    #endif

    return 0;
}
da-h commented 2 years ago

Hi @leviettung200,

the goal of pybison ist to combine bison and python. It does so by implementing the grammar syntax into the docstrings of a "parser" python file. Thus, while it is possible to use C code in the lexer, pybison does not allow C code in the grammar actions. The actions of the grammar rules can however be programmatically used using python. (See the examples folder for some used cases.)

Best da-h

leviettung200 commented 2 years ago

I'm seeking a Python, Java, JS, or C# solution that allows C code in the grammar actions. Creating a parser file enables me in parsing the input and getting the output.

Can you give me any other suggestions? Thanks @da-h !!

da-h commented 2 years ago

Python-wise, it is relatively easy to write C-extensions.

So, if C-code in the grammar actions is necessary for your solution, with an python-api I would try plain bison (instead of pybison) to generate the parser itself and write an python C-extension as an entrypoint for the resulting parser.

leviettung200 commented 2 years ago

Can you please describe the process in greater detail?

If at all possible, I'd prefer my solution to avoid having to modify the parser file (using flex and bison files directly). e.g., writing a wrapper for C code (which helps me to compile flex and bison).

My specific problem was migrating input file parsing from on-premise to the Azure Function App