eval.leg.y
contains leg(1)
grammar for simple numerical expressions --- excluding identifiers. Just numbers. It supports floats, and the 4 radices of integer commonly in use. Although that there won't be any parsing errors when floats are passed to non-compliant operators, your C runtime will definitely glitch out so be mindful.
But it also bootstraps the YY_INPUT
through stdin
by making a temporary file and using freopen(3)
to reopen stdin
within that buffer --- after of course having dumped the input and the sentinel (;
) into it. Perhaps this part can be redone. I will update it later in the future to fix this issue.
To use this grammar (after installing leg(1)
which comes bundled with peg(1)
, from here --- mind you that most Debian package managers have it, try sudo apt-get install peg
first):
wget -qO- https://gist.githubusercontent.com/Chubek/d7e5eb46c956d15754c7e93d397aeec4/raw/1a549e45b3996e7c57a896c356b2eae0a3843dea/eval.leg.y | leg -o _eval.yy.c
You may use it like this:
#include "_eval.yy.c"
#include <stdio.h>
#include <string.h>
int main (int argc, int8_t **argv)
{
write_to_parser(argv[1]);
while (yyparse());
printf("%ld", (long)yyfinal);
close_redir();
}
To run a quick test using tcc(1)
:
cat _test.c | tcc -run - 1+1
It is not fully tested. It may run into left recursion --- IT MAY. But PEG grammars are usually less prone to left recursion than their EBNF counterparts (when the EBNF gets shoddily translated into LL(1) anyways, quite honestly, use cases of LL(1) are small).
My Discord is .chubak
. My email is chubakbidpaa@gmail.com
. Thank you.