Consider the problem of matching well nested brackets

Info icon This preview shows pages 7–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Consider the problem of matching well-nested brackets when reading characters from some data source. For example, you might want to determine whether each open parenthesis character ’(’ is later followed by a matching ’)’ , and similarly for ’{’ and ’}’ and ’[’ and ’]’ . Eclipse uses such an algorithm to check that a Java program doesn’t have syntax errors. One subtlety is that such brackets should not only match, but they should also be well-nested . The string "( [ ) ]" is not well-nested because the [ is closed by ) and not ] , as it should be. On the other hand, the string "()[()]" is well-nested. We have written the following test cases that illustrate many more examples of the desired behavior of this method, called BracketMatcher.matched . For the purposes of this problem, the matcher simply ignores non-bracket characters. Be sure you understand these tests before continuing. import static org.junit.Assert. * ; import java.io.Reader; import java.io.StringReader; import org.junit.Test; public class BracketMatcherTest { private void testString( boolean matched, String s) { Reader r = new StringReader(s); assertEquals(matched, BracketMatcher.matched(r)); } @Test public void testMatchesEmpty() { testString( true , ""); } @Test public void testNoMatchOpen() { testString( false , "("); } @Test public void testNoMatchClose() { testString( false , ")"); } @Test public void testMatchOpenClose() { testString( true , "()"); } @Test public void testNoMatchOpenOpenClose() { testString( false , "(()"); } @Test public void testMatchOpenOpenCloseClose() { testString( true , "(())"); } 7
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
@Test public void testMatchOpenOpenCloseCloseJunk() { testString( true , "(x(e)xxabxx)"); } @Test public void testMatchOpen1Open2Close2Close1() { testString( true , "([])"); } @Test public void testNoMatchOpen1Open2Close1Close2() { testString( false , "([)]"); } @Test public void testMatchProblemStatement() { testString( true , "()[()]"); } @Test public void testNoMatchOpen1Open2Close1Close2Junk() { testString( false , "(ax[asda)b]"); } @Test public void testMatchOpen3Open2Open1Close1Close2Close3() { testString( true , "{[()]}"); } } How do we implement such a matching algorithm? We read through the sequence of characters (provided by a Reader object as in the Spellchecking project). Each time we encounter a left bracket (like ’[’ ), we push it onto a stack. Each time we encounter a right-bracket (like ’]’ ), we pop the top of the stack and check to make sure that it matches. If we ever hit a mismatch, or if the stack is empty when it shouldn’t be, then the sequence isn’t well-nested. After reading all of the sequence, if the stack isn’t empty, the sequence isn’t well matched. Implement this algorithm on the following page. We have provided a couple utility methods that may be of use. Your code should pass all of the tests above and it should never raise any exceptions. For your convenience, the documentation of the read() method of the Reader class is found in the Appendix — you should not need to use any other methods from the reader class.
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern