N
N
newmersedez2020-11-17 14:12:40
Parsing
newmersedez, 2020-11-17 14:12:40

How to print a string according to a given algorithm?

Hello, I am writing an interpreter again, and so ..... There is, for example, an input file containing instructions of the form:

A := B + C;;; ss D := E&G;;;;;
C := F <> z;;
X := \A;
read(D, base);
write(D, base);


Instructions can be written on one line, have many spaces and tabs, contain errors, etc., but that's not the point. I have a method that just parses this string and returns a dynamic array of strings(That is, let's say the string C := F <> z;; it will parse as "C" ":=" "F" "<>" "z "";" ";" ).

I want instructions to be displayed on the screen. If there is one instruction per line, like, for example, 2-5 lines, then it is clear how to display them. The problem is how to separate the output of multiple instructions written on the same line. That is, if I have <Operation1> <Operation2> in the line (as in line 1), then I want to draw the following output:

<Operation1>
<Operation2>


Important: instruction separators are the semicolon character ;

Please tell me how to do it? It's simple, but I've already broken my head. Here is a snippet from a failed attempt
#include <stdio.h>
#include <string.h>
#include "stringparse.h"

int     operation_processing(FILE *file)
{
    int     i;
    int     j;
    int     k;
    int     size;
    char    buf[BUFSIZ];
    char    **parsed_string = NULL;

    i = 0;
    size = 0;
    while(!feof(file))
    {
        fgets(buf, sizeof(buf), file);
        strtok(buf, "\n");
        parsed_string = string_parse(buf, &size);
        for(i = 0; i < size; i++)
        {
            while(strcmp(parsed_string[i], ";"))
            {
                printf("%s  ", parsed_string[i]);
                i++;
                if(!strcmp(parsed_string[i], ";"))
                {
                    while(!strcmp(parsed_string[i], ";"))
                    {
                        printf(";  ");
                        i++;
                    }
                    printf("\n");
                }
            }
        }
    }
    return (0);
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question