Thread: How to assign indexes to ' if-the-else' bundle.

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    68

    How to assign indexes to ' if-the-else' bundle.

    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++;
    Last edited by john7; 12-06-2022 at 03:24 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Does your code logically deduce this
    Code:
    if (flg0 == 2) then //1
    {
        var4=1;
    }
    else  //1
    {
        var4=2;
    }
    from this
    Code:
    if (flg0 == 2) then //1
        var4=1;
    else  //1
        var4=2;
    Parsing usually results in a tree of some sort.
    Each if/else can vary in depth.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    68
    Quote Originally Posted by Salem View Post
    Does your code logically deduce this
    Code:
    if (flg0 == 2) then //1
    {
        var4=1;
    }
    else  //1
    {
        var4=2;
    }
    from this
    Code:
    if (flg0 == 2) then //1
        var4=1;
    else  //1
        var4=2;
    Parsing usually results in a tree of some sort.
    Each if/else can vary in depth.
    Well... For now the braces are not mandatory in my script like in any grown man script .
    That's the problem - to track the if/else depth. Otherwise the question wouldn't arise .

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    https://cs.wmich.edu/~gupta/teaching...aur%20form.htm
    Code:
    <compound-statement> ::= { {<declaration>}* {<statement>}* }
    
    <statement> ::= <labeled-statement>
                  | <expression-statement>
                  | <compound-statement>
                  | <selection-statement>
                  | <iteration-statement>
                  | <jump-statement>
    
    <labeled-statement> ::= <identifier> : <statement>
                          | case <constant-expression> : <statement>
                          | default : <statement>
    
    <expression-statement> ::= {<expression>}? ;
    
    <selection-statement> ::= if ( <expression> ) <statement>
                            | if ( <expression> ) <statement> else <statement>
                            | switch ( <expression> ) <statement>
    
    <iteration-statement> ::= while ( <expression> ) <statement>
                            | do <statement> while ( <expression> ) ;
                            | for ( {<expression>}? ; {<expression>}? ; {<expression>}? ) <statement>
    
    <jump-statement> ::= goto <identifier> ;
                       | continue ;
                       | break ;
                       | return {<expression>}? ;
    You need something like this for your mini language.

    And
    Recursive descent parser - Wikipedia

    Basically, your parser should be recursive.
    So evaluating an if inside an if is a recursive call, and you create a recursive data structure like a tree.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    68
    Thank you. It's a direction.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Get matrix indexes from the value
    By koplersky in forum C++ Programming
    Replies: 6
    Last Post: 10-17-2013, 02:45 PM
  2. Humble Indie Bundle
    By bernt in forum General Discussions
    Replies: 3
    Last Post: 12-29-2010, 11:13 AM
  3. Getting Indexes
    By satty in forum C Programming
    Replies: 2
    Last Post: 08-09-2010, 01:11 AM
  4. MySQL Indexes
    By LuckY in forum Tech Board
    Replies: 1
    Last Post: 05-09-2007, 06:04 PM
  5. Undefined symbols from ld? ; making bundle; gcc 3.3
    By profxtjb in forum C Programming
    Replies: 5
    Last Post: 06-15-2004, 01:31 AM

Tags for this Thread