2

I have ANTLR parser and tree grammar that is working fine. I want user to input the grammar in the JTextarea(I am using java swing for gui development). Once, the grammar is in the JTextarea, the user shall press a button (run) and the result of the grammar (the actions) shall be shown in another text area.

For the sake of simplicity, the simplest HelloWorld ANTLR grammar(parser and tree grammar) can be used to demonstrate how can we run ANTLR grammar from Java Swing gui's JTextarea.

I haven't found anything that relates ANTLR with JavaSwing GUI or anything that says how to run ANTLR Grammar from Java Swing GUI's JTextArea.

Any thoughts/suggestions/simple example/demonstration?
EDIT-Solution
Below is a portion of code from my GUI class that deals with the simulate button. User inputs ANTLR grammar in a JTextarea.

private void addReRunButtonListener() {
        btnReRun.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                 try {
                     //for Random Simulation
                     if(rdbtnRandomWithSeed.isSelected())
                     {
                      String line = "";
                      String nl ;

                      Process p = Runtime.getRuntime().exec("java org.antlr.Tool protocol.g protocolWalker.g");
                      p.waitFor();
                      p = Runtime.getRuntime().exec("javac protocolLexer.java protocolParser.java protocolWalker.java SimulatorRandom.java");
                      p.waitFor();
                      p = Runtime.getRuntime().exec("java RandomSimulator");

                      OutputStreamWriter stdin = new OutputStreamWriter(p.getOutputStream ());

                      stdin.write(simAlgoEdit.getText()+"\n");

                              stdin.close();

                      BufferedReader bri = new BufferedReader
                        (new InputStreamReader(p.getInputStream()));
                      nl = bri.readLine();
                      while (nl != null) {
                          line = line + nl + "\n";
                          nl = bri.readLine();

                      }
                      bri.close();

                      System.out.println("Done.");
                      simGraphicsArea.setText(line);
                      graphicsArea.setText(line);

                     }

The above code also grabs the output and inserts it in another JTextarea in my GUI. @trashgod: Thank you very much for your help.

Thank you

Rizwan Abbasi
  • 151
  • 2
  • 10

1 Answers1

3

You can evoke ANTLR using ProcessBuilder just as you would from the command line. There's a related example here. You can use the append() method of JTextArea to display the result from stdout.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • See also this [example](http://stackoverflow.com/a/3245805/230513) that uses `append()`. – trashgod Nov 01 '12 at 01:00
  • I have gone through ProcessBuilder class as well as various resources like you mentioned. I have tried a simple example that start notepad process in Windows7. please see my EDIT I am still not sure how to use it in my case. Have looked into the link example you provided but that was for Mac. – Rizwan Abbasi Nov 01 '12 at 15:13
  • The strings used in the example are Mac specific, but the code is portable. Try substituting a DOS command like `dir`, so you can read `stdout`. – trashgod Nov 01 '12 at 18:11
  • Is it possible tomorrow? I am getting it hard to grasp that processes' stuff – Rizwan Abbasi Nov 01 '12 at 18:44
  • You might want to work through a few [examples](http://stackoverflow.com/search?q=user%3A230513+%5Bjava%5D+ProcessBuilder&submit=search) on your own first. – trashgod Nov 01 '12 at 19:08
  • Yes, sure and I'll let you know then. – Rizwan Abbasi Nov 01 '12 at 19:25
  • You're very welcome, and permit me to say that I admire your diligence. – trashgod Nov 09 '12 at 18:24