Installing ANTLR4 on Windows

These directions are geared towards people who are going to develop ANTLR projects using Python3.

  1. Open a Command Prompt in Windows 10. You can do this by clicking on the Windows icon in the lower left corner, typing "command", and clicking on "Command Prompt".
    • You might consider pinning Command Prompt to Start.
    • By default, the Command Prompt font is a dull grey. For a better color, right click on the command bar, select Colors, click on Screen Text, and then pick the color of your choice. I use a bright yellow or bright green. You might also want to increase the font size.
  2. Type the command python (or just py) and see if you get a >>> prompt. If you don't but you know that Python3 is installed:
    1. Click on the Windows icon and select Settings.
    2. Enter "environment" in the search bar and click on Edit the system environment variables.
    3. Click on the Environment Variables button.
    4. Click on Path in the System variables section (the lower section) and click on Edit....
    5. Click on New, Browse..., and browse to where your Python3 is installed.
    6. Click on OK until you are out of the Environment Variables window (at least - you could exit all the way out of the Windows Settings window.
    7. Close and re-open the Command Prompt. This ensures you now have the environment variables set up. Confirm it works by typing py. In some cases, you may need to re-install Python3: see the next step.
  3. If Python3 is not installed already,
    1. Visit python.org/downloads/windows and download the most recent stable release of Python3 (as of this writing, Python 3.10.7), selecting the Windows x86-64 (that is, 64-bit) executable installer, and installing it.
    2. Note the path that it will be installed at - you may need this later. A screen shot should be adequate.
    3. Be sure installing pip is checked.
    4. Open a new command prompt (or try the current one - it may work) and type py. You should see a >>> prompt. If not, you may need to add Python3 to your path (see the directions above).
  4. If you aren't running Python3, type py at the Command Prompt. If you aren't used to it, this is the interactive Python shell where you can type Python code. For example, enter
            def cube(n):
              return n * n * n
    
    (noting that you indent the return line by at least a couple spaces) and hit an extra return. You can then test it:
            cube(9999)
    
    and you should get back 999700029999.
  5. Exit Python3 by typing Control-Z and hitting return. Control-Z marks the end-of-file in Windows. You can also exit by entering exit().
  6. Enter pip at the command prompt. You will likely get a "command not found" message. You can follow the above directions to add pip to your path, or you can just execute the command directly. The trick is to find it. The brute-force way is to enter
            cd \
            dir/s pip.exe
    
    This searches your full computer and will take a while. But you can probably find it in
            C:\users\username\AppData\Local\Programs\Python\PythonXXX\Scripts
    
    where username is your MSOE login name and XXX is something like 39-64. Use cd to get to this directory, noting you can hit the Tab key to fill out filenames. For me, I type
            cd c:\users\hasker\AppData\Local\Programs\Python
            cd Python39-64\scripts
    
  7. Now that you can execute pip, type
            python -m pip install antlr4-python3-runtime
    
  8. If you do not have Java installed, install openjdk11. I do it with choco install openjdk11, but you can probably find an installer online. Make sure java is in your PATH variable.
  9. Follow the steps in https://levlaz.org/setting-up-antlr4-on-windows/ to set up antlr4. You will need to create a command called antlr4.bat and put it in your path as the blog indicates (so you can type antlr4 at the command prompt), but you will not need grun.bat.
  10. Download the adder grammar and type
            antlr4 -Dlanguage=Python3 adder.g4
            py adder.py
    
    (where you might use python3 instead of py) If you get a "command not found" for either one, try closing the command prompt and re-opening it. Otherwise post your error message to the General channel of your class's Teams site and ask for help - it's very likely someone else has run into the same issue.

    Windows users are likely to get the warning messages

            ANTLR runtime and generated code versions disagree: 4.8!=4.5.3
    
    This has to do with a mismatch between libraries; you can ignore it.