I wounder how to implement indentation as block delimiters in bison + flex. Just like in python. I'm writing my own programming language ( mostly for fun, but I intend to use it together with a game engine ), I'll try to come up with something special that minimizes boilerplate and maximizes dev speed.
I have already written an compiler ( actually a `langToy' to Nasm translator ) in C, but failed. By some reason it was only able to handle one string in the whole source file ( well, I had been awake for more than 48 hours - so... You know, brain meltdown ).
I don't know if curly brackets and/or begin -> end are easier to implement ( I don't have problem doing that ) or if it's just my brain that locks up.
Thanks in advance!
Update: Okay, I have no clue about how to do it with flex. I have problems with returning multiple DEDENTs to the parser. Flex/Bison are relatively new to me.
Update 2: This is the flex-file I've come up with so far; it does not quite get it:
%x t
%option noyywrap
%{
int lineno = 0, ntab = 0, ltab = 0, dedent = 0;
%}
%%
<*>\n { ntab = 0; BEGIN(t); }
<t>\t { ++ntab; }
<t>. { int i; /* my compiler complains not c99 if i use for( int i=0... */
if( ntab > ltab )
printf("> indent >\n");
else if( ntab < ltab )
for( i = 0; i < ltab - ntab; i++ )
printf("< dedent <\n");
else
printf("= =\n");
ltab = ntab; ntab = 0;
BEGIN(INITIAL);
/* move to next rule */
REJECT;}
. /* ignore everything else for now */
%%
main()
{
yyin = fopen( "test", "r" );
yylex();
}
You can try to play around with it, maybe you sees what I'm missing. returning multiple dedents would be an ease in haXe ( return t_dedent( num ); ).
This code doesn't always match the indents/dedents correctly.
Update 3: I think that I will give up hope on flex and do it my own way, If anyone knows how to do it in flex I would be happy to hear it anyways.