I'm stumbled at an algorithmic problem - to assign indexes.

In a simple case it's trivial

Code:

if (flg0 == 2) then //1
var4=1;
else //1
var4=2;

But if I have nested /if-the-else/

Code:

if (flg0 == 2) then //1
{ //1
if (flg1 == 1) then //2
var4=1;
else //2
{ //2
if (flg2 == 3) then //3
{//3
var5=1;
}//3
}//2
}//1
else //1
var5=2;

How to keep track of the previous "strayed" /else/ and /}/ ?

I split it to tokens

Code:

switch (token.type)
{
case If:
expr[expr_size].type = EXPR_TYPE_IF;
expr[expr_size].idx++;
break;
case Then:
expr[expr_size].type = EXPR_TYPE_THEN;
expr[expr_size].idx++;
break;
case Else:
expr[expr_size].type = EXPR_TYPE_ELSE;
expr[expr_size].idx++;
break;
case BlockStart:
expr[expr_size].type = EXPR_TYPE_STBLK;
expr[expr_size].idx++;
break;
case BlockEnd:
expr[expr_size].type = EXPR_TYPE_ENDBLK;
expr[expr_size].idx++;
break;
}

Right now it's trivial expr[expr_size].idx++;