Thread: Nested Parsing

  1. #1
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Cool Nested Parsing

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define OPEN '{'
    #define CLOSE '}'
    #define MAX_DEPTH 10
    #define MAX_SIZE 128
    
    char results[MAX_DEPTH][MAX_SIZE];
    int lock[MAX_DEPTH];
    
    char *data = "this is a test { to see if it works } when you nest "
        "{ on different {inset values} } data { { that are nested } data }.";
    
    int main(int args,char *argv[]) {
    
        int spot = -1, read = 0, nest = 0;
        
        while (++spot < strlen(data)) {
            if (data[spot] == OPEN) {
                nest++;
                results[nest][lock[nest]++] = '~';
            } else if (data[spot] == CLOSE) {
                nest--;
            } else {
                results[nest][lock[nest]++] = data[spot];
            }
        }
    
        for (int i=0; i<MAX_DEPTH; i++) printf("%s\n",results[i]);
    
        return 0;
    };
    thoughts ?
    "without goto we would be wtf'd"

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Better variable names perhaps?

    Because I got as far as 'lock' and 'nest', and thought the next thing was 'monster'.

    Also, calling strlen() every time around the loop is a waste of effort.

    Try this, which has the advantage of not having the messy start at -1 thing.
    for ( i = 0 ; data[i] != '\0' ; i++ )
    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
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    thought the next thing was 'monster'
    Ha, i noticed that as well. I thought it was funny so i kept it.
    I'm weird like that.

    the messy start at -1 thing
    Not sure how this is "messy".

    calling strlen() every time around the loop is a waste of effort
    also slows it down i bet. I should declare the variable first and just keep comparing to that.

    Code:
    int sLen = strlen(data);
    while (++spot < sLen) {
       if (data[spot] == OPEN) {
         nest++;
         results[nest][lock[nest]++] = '~';
       } else if (data[spot] == CLOSE) {
         nest--;
       } else {
         results[nest][lock[nest]++] = data[spot];
       }
     }
    Last edited by Structure; 01-31-2020 at 11:55 AM.
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-07-2019, 09:32 AM
  2. help with nested ifs
    By marlin in forum C Programming
    Replies: 4
    Last Post: 02-15-2012, 10:17 PM
  3. Nested While
    By skg29 in forum C Programming
    Replies: 4
    Last Post: 10-03-2011, 04:48 AM
  4. String parsing(parsing comments out of HTML file)
    By slcjoey in forum C# Programming
    Replies: 0
    Last Post: 07-29-2006, 08:28 PM
  5. Help with using nested For
    By webbizdesign in forum C Programming
    Replies: 5
    Last Post: 09-29-2003, 10:50 PM

Tags for this Thread