如果一个字符被引号包含(也称为编程字符串模式),请勿进行匹配。

4

我被分配编写一个针对BASIC编程语言的编译器。在BASIC中,代码可以通过换行或:符号进行分隔。例如,以下两个代码块是有效的:
模型 #1

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

型号# 2

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

你可以在这里测试:http://www.calormen.com/jsbasic/。 在我的编译器中解析代码之前,首先需要分割代码。我已经将代码分成了行,但是我无法找到一个正则表达式来分割以下代码示例:此代码示例应该分成两个PRINT代码。
 10 PRINT "Hello World 1" : PRINT "Hello World 2"

但是,不要匹配这个:
下面的代码示例是一个独立的命令。
 10 PRINT "Hello World 1" ": PRINT Hello World 2"

问题

是否有任何正则表达式模式可以匹配以上代码示例的第一个,其中:"对之外,并且不匹配第二个?

有谁可以帮帮我吗?
任何信息都会有所帮助。 :)


1
你不应该使用正则表达式来解析这种结构。正则表达式只能匹配正则语言,这与你的问题不符。你应该使用类似于这个的结构。 - Mauren
@Mauren 是的,确实如此。我最终会这样做,但首先需要对源代码进行标记化,并净化代码(即删除注释等...)。因此,我认为我首先需要对“:”进行标记化。 - dariush
2
我建议您通过构建循环来进行标记化,这样您可以查看每个字符并决定它属于哪个标记,而不是通过正则表达式来完成。请查看先前链接的源代码的第86行。 - Mauren
@Mauren 谢谢,这非常有帮助 :) - dariush
@Mauren:我同意“完整”的正则表达式解决方案并不是这种任务的最佳方式,但是不要认为像boost(或其他现代正则表达式工具)这样的库无法匹配非正则语言。我们远离理论考虑和POSIX正则表达式引擎的能力。 - Casimir et Hippolyte
3个回答

1
我认为最好的选择是使用循环等设备对源代码进行标记化,而不是尝试使用正则表达式进行标记化。
在伪代码中:
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
以下答案是您提议的实现。谢谢。 - dariush

0
避免这种问题的想法是在尝试匹配冒号之前匹配引号内的内容,例如:
"(?>[^\\"]++|\\{2}|\\.)*"|:

你可以添加捕获组以了解备选项的哪个部分已匹配。

然而,完成这种任务的好工具可能是lex/yacc。


@Dariush:你在尝试做什么? - Casimir et Hippolyte
@Dariush:方法不同。如果你把交替的第一部分放在一个捕获组中,你只需要检查捕获组是否为空或存在,就可以知道模式是否匹配任何引号外的冒号。 - Casimir et Hippolyte

0
感谢@Mauren的帮助,我成功实现了想要做的事情。
这是我的代码(或许能帮到后来者):
请注意源文件内容存储在char* buffervector<string> source_code中。
    /* lines' tokens container */
    std::string token;
    /* Tokenize the file's 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's tokens */ token += char(buffer[top]); /* continue seeking */continue; }
        /* if we reach here we have collected the current line's 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's 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 */

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接