Do not match if char is between quotation marks (AKA has a programming line pattern)

I was assigned to write a compiler for the base programming language . In the base code, codes are separated by new lines or by a character :. For example, compliance with the following codes.
Model No. 1

 10 PRINT "Hello World 1" : PRINT "Hello World 2"

Model No. 2

 10 PRINT "Hello World 1"
 20 PRINT "Hello World 2"

You can test those here .
The first thing I need to do before parsing the codes in my compiler is to split the codes.
I already broke the codes in the lines, but I was stuck in finding a regex for separation. The following code example:
This next code example should be split by 2 PRINT.

 10 PRINT "Hello World 1" : PRINT "Hello World 2"

:
- .

 10 PRINT "Hello World 1" ": PRINT Hello World 2"

, DO, , : " ?

- ?
.:)

+4
3

@Mauren , .
(, - ):
, char* buffer vector<string> source_code.

    /* lines' tokens container */
    std::string token;
    /* Tokenize the file content into seperate lines */
    /* fetch and tokenizing line version of readed data  and maintain it into the container vector*/
    for(int top = 0, bottom = 0; top < strlen(buffer) ; top++)
    {
        /* inline tokenizing with line breakings */
        if(buffer[top] != '\n' || top == bottom)
        { /* collect current line tokens */ token += char(buffer[top]); /* continue seeking */continue; }
        /* if we reach here we have collected the current line tokens */
        /* normalize current tokens */
        boost::algorithm::trim(token);
        /* concurrent statements check point */
        if(token.find(':') != std::string::npos)
        {
            /* a quotation mark encounter flag */
            bool quotation_meet = false;
            /* process entire line from beginning */
            for(int index = 0; true ; index++)
            {
                /* loop exit cond. */
                if(!(index < token.length())) { break; }
                /* fetch currently processing char */
                char _char = token[index];
                /* if encountered  a quotation mark */
                /* we are moving into a string */
                /* note that in basic for printing quotation mark, should use `CHR$(34)` 
                 * so there is no `\"` to worry about! :) */
                if(_char == '"')
                {
                    /* change quotation meeting flag */
                    quotation_meet = !quotation_meet;
                    /* proceed with other chars. */
                    continue;
                }
                /* if we have meet the `:` char and also we are not in a pair quotation*/
                if(_char == ':' && !quotation_meet)
                {
                    /* this is the first sub-token of current token */
                    std::string subtoken(token.substr(0, index - 1));
                    /* normalize the sub-token */
                    boost::algorithm::trim(subtoken);
                    /* add sub-token as new line */
                    source_codes.push_back(subtoken);
                    /* replace the rest of sub-token as new token */
                    /**
                     * Note: We keep the `:` mark intentionally, since every code line in BASIC 
                     * should start with a number; by keeping `:` while processing lines starting with `:` means 
                     * they are meant to execute semi-concurrent with previous numbered statement.
                     * So we use following `substr` pattern instead of `token.substr(index + 1, token.length() - 1);`
                     */
                    token = token.substr(index, token.length() - 1);
                    /* normalize the sub-token */
                    boost::algorithm::trim(token);
                    /* reset the index for new token */
                    index = 0;
                    /* continue with other chars */
                    continue;
                }
            }
            /* if we have any remained token and not empty one? */
            if(token.length())
                /* a the tokens into collection */
                goto __ADD_TOKEN;
        }
__ADD_TOKEN:
        /* if the token is not empty? */
        if(token.length())
            /* add fetched of token to our source code */
            source_codes.push_back(token);
__NEXT_TOKEN:
        /* move pointer to next tokens' position */
        bottom = top + 1;
        /* clear the token buffer */
        token.clear();
        /* a fail safe for loop */
        continue;
    }
    /* We NOW have our source code departed into lines and saved in a vector */
0

, , , , .

string lexeme;
token t;

for char in string
    if char fits current token
        lexeme = lexeme + char;
    else
        t.lexeme = lexeme;
        t.type = type;
        lexeme = null;
    end if
    // other treatments here
end for

, 86.

+1

, :

"(?>[^\\"]++|\\{2}|\\.)*"|:

, , .

, , lex/yacc

0

Source: https://habr.com/ru/post/1534074/


All Articles