Hi, I'm having a major problem with an algorithm for Java. I am building a program that recursively parses a simple programming language that follows obvious production grammars. My main issue is nesting. For instance, the pattern might be something like this:

   Create Jpanel1:{
         Create JButton1
         Create JPanel2:{
              Create JButton2
              Create JButton3
         Create JTextField1
         Create JPanel3:{
             Create JPanel4:{
                  Create JButton4

By now you've probably got an idea of what I'm talking about. In order to add JButton1 to JPanel1 I have to teach JButton1 who it's parent is (JPanel1 of course). You'll also notice that JButton1 is a terminal node and that the recursion comes from the existence of these other JPanel nodes that then lead to some terminal node. In any case, every one of these guy's must know who it's immediate parent is so it can be added to it's parent with the GUI's add(element) method.

Recommended Answers

All 2 Replies

That's not enough info for anyone to understand where or why you are having difficulty. How far have you got - is there any code or documented design yet? Have you got as far as pushing each non-terminal node onto a stack and popping it when you finish processing its children?

Member Avatar

Using recursion it should automagically preserve order, you're doing something wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.