softdevteam/grmtools

how to use lrlex with rust code

Closed this issue · 1 comments

  • we can use c code, and c context with flex, just like below
struct comile_context ctx;
int test_compile() {
     struct comile_store store;
     
     yaccparse(&store);
}

this c code is with yacc export function yaccparse. and yacc 's param is a pointer for struct comile_store .
ctx is global context, or we can also say that it is a global var for yacc.
and in .l file, we can use c code with ctx :

%{
extern struct comile_context ctx;
%}
%%
testsymbol    {
                               do somthing with ctx. 
                               for example, store the count of testsymbol at ctx,
                               for example, get some sub strings from yytext, then store them at ctx.
                       }

if there are two testsymbol in input, the c code in {} will execute twice.
there are two problem metioned:

  • how to write rust code with lrlex
  • how to get yytext in .l with lrlex

lrlex is currently much simpler than flex and can't execute user code (in part because, in my opinion, trying to make an automatic lexer too flexible makes it ever-more complex, without making it much more flexible).

Instead, if you need a highly flexible lexer, you can use the advice at https://softdevteam.github.io/grmtools/master/book/manuallexer.html to write your own manual lexer whose output is then parsed by lrpar.