0

Following this question, I'm trying to learn how to use the TestRig / grun tool. Consider the grammar file in this repo. I ran the below commands :

export CLASSPATH=".:/usr/local/Cellar/antlr/&ltversion&gt/antlr-&ltversion&gt-complete.jar:$CLASSPATH"
antlr &ltgrammarName&gt.g4
javac &ltgrammarName&gt*.java

but when I run

grun <grammarName> <inputFile>

it gets stuck without returning any error messages. I have tested this with other examples as well to no avail. I would appreciate it if you could help me know what is the problem and how I can resolve it.

Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193

1 Answers1

1

the normal grun alias takes the grammarName and startRule as parameters and expects the input from stdin:

grun <grammarName> <startRule> < <inputFile>

example:

grun ElmerSolver sections -tree  < examples/ex001.sif

If you want to run just the Lexer, you can use the "pseudo-startrule" "tokens":

grun ElmerSolver tokens -tokens  < examples/ex001.sif

With your sample, this gives me:

[@0,0:9='Simulation',<'Simulation'>,1:0]
[@1,11:13='End',<'End'>,2:0]
[@2,16:24='Equation ',<'Equation '>,4:0]
[@3,25:25='1',<Integer>,4:9]
[@4,27:29='End',<'End'>,5:0]
[@5,30:29='<EOF>',<EOF>,5:3]

(That's using the grammar changes I made in the previous answer, but should demonstrate the results)

Mike Cargal
  • 6,610
  • 3
  • 21
  • 27
  • I get the `No method for rule sections or it has arguments` error now. – Foad S. Farimani Apr 05 '21 at 17:48
  • Well, I guess that's because the `sections` definition is commented out at the moment. But is there a way to just get the tokens? – Foad S. Farimani Apr 05 '21 at 17:50
  • 1
    Well, how about that... the IS a way to do just the tokens. Had to check the source code. I'll amend my answer momentarily. – Mike Cargal Apr 05 '21 at 18:00
  • OK awesome. So I can conclude that my grammar file is not totally garbage so far? – Foad S. Farimani Apr 05 '21 at 18:05
  • 1
    “Totally garbage” would be quite harsh, so no, definitely not garbage. I did make a few suggestions in my previous answer that you might want to consider. Beyond those comments, there’s some unusual handling of EOL (but some specifications can be weird about things like that, so I can’t say it’s necessarily wrong.) – Mike Cargal Apr 05 '21 at 18:15