1

This is the first time I use Antlr4 and I have a question regarding to the test rig. I've finished the installation process and try out the sample given in both Antlr4 main site and the github page. Here's what I've done:

Installation:

/usr/local/lib$ sudo curl -O https://www.antlr.org/download/antlr-4.7.1-complete.jar
/usr/local/lib$ export CLASSPATH=".:/usr/local/lib/antlr-4.7.1-complete.jar:$CLASSPATH"
/usr/local/lib$ alias grun='java org.antlr.v4.gui.TestRig'

I ran only antlr4 and grun to see if everything is installed properly:

$ antler4
ANTLR Parser Generator  Version 4.7.1
 -o ___              specify output directory where all output is generated
 -lib ___            specify location of grammars, tokens files
 .
 .
$ grun
java org.antlr.v4.gui.TestRig GrammarName startRuleName
  [-tokens] [-tree] [-gui] [-ps file.ps] [-encoding encodingname]
  .
  .

So went ahead and created an example Hello.g4 in /tmp which looks like this

/// Define a grammar called Hello
grammar Hello;
r  : 'hello' ID ;         // match keyword hello followed by an identifier
ID : [a-z]+ ;             // match lower-case identifiers
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines

and ran:

$ antlr4 Hello.g4
$ javac Hello*.java

According to the example, it should at least return a gui of a tree and some output when I do grun Hello r -tree

However, when I did that, nothing happened. Just blinking cursor as if it's computing something. Kinda like this

/tmp/Hello$ grun Hello r -tree
|

Is something wrong with the Hello.g4 code or am I missing something during the installation or grun just takes very long to finish compiling? I've also tried with several examples from the Antlr4 page, still got the same thing.

thank you for any response in advance.

1 Answers1

0

It's not doing anything because it's waiting for input. grun -tree runs the parser on some input and then prints the parse tree for that input. But it can't do that until it has some input text to parse.

So you should enter some input that's valid according to the grammar (or not, if you want to test the error reporting) and then press Ctrl+D (Ctrl+Z if you're on Windows) to close the input stream. Alternatively you can write the input into a file and then run grun Hello r -tree < my_input.txt.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • @NuttawornSujumnong Yes, that's right (assuming there are line breaks before and after "hello parrt"). `(r hello parrt)` is what that input looks like as a "tree" (`grun -tree` prints the parse tree as an s-expression, not an actual ASCII graphic of a tree). – sepp2k Jun 06 '18 at 21:22
  • Alright. I just add `hello parrt` manually as an input and then press ctrl+D and it looks just like in the example. Thank you for the explanation. – Nuttaworn Sujumnong Jun 06 '18 at 21:22