Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
709 views
in Technique[技术] by (71.8m points)

algorithm - How do I find the position of matching parentheses or braces in a given piece of text?

A lot of text editors and IDEs have a feature that highlights matching parentheses, square brackets, or curly braces when the cursor is placed over either the opening or closing character in one of these pairs.

What algorithm is used to find the position of the matching parenthesis, given the position of either an opening or closing parenthesis in a text file? Keep in mind that these characters can be nested, so simply scanning forward or backward through the text until you find the opposite character is insufficient.

Example:

I recently ran into this problem when writing a brainf*ck interpreter in Java. [ and ] in that language are analogous to a while loop, and can be nested. The interpreter needs to find the matching [ or ] depending on the value at the data pointer. See the ROT13 example code for an illustration of nesting.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Given the position of an open parenthesis in an array of characters, there's a simple algorithm that uses a counter to find the matching close parenthesis.

  • Initialize a counter to 1.
  • Loop forward (to the right) through the text.
    • If another open parenthesis is encountered, increment the counter.
    • If a closing parenthesis is encountered, decrement the counter.
  • When the counter reaches zero, you've found the matching closing parenthesis.

In code this looks something like:

public int findClosingParen(char[] text, int openPos) {
    int closePos = openPos;
    int counter = 1;
    while (counter > 0) {
        char c = text[++closePos];
        if (c == '(') {
            counter++;
        }
        else if (c == ')') {
            counter--;
        }
    }
    return closePos;
}

The algorithm for finding the position of the matching open parenthesis given a closing parenthesis is the opposite.

  • Initialize a counter to 1.
  • Loop backwards (to the left) through the text.
    • If an open parenthesis is encountered, decrement the counter.
    • If a closing parenthesis is encountered, increment the counter.
  • When the counter reaches zero, you've found the matching open parenthesis.

In code:

public int findOpenParen(char[] text, int closePos) {
    int openPos = closePos;
    int counter = 1;
    while (counter > 0) {
        char c = text[--openPos];
        if (c == '(') {
            counter--;
        }
        else if (c == ')') {
            counter++;
        }
    }
    return openPos;
}

Note: Both of the examples above assume that parentheses are balanced, so no array bounds checking is done. A real implementation would check that you don't run off the end of the array, and throw an exception (or return an error code) that indicates that parentheses are unbalanced in the input text if you do.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...