Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANTLR - specifying multiple directories to search in for included grammars

I am extending an existing open-source project, which already has a very advanced Lexer/Parser/TreeParser in ANTLR. I would like to adhere to the current directory structure, so I created my own directory where I would like to put my new (already written) grammar files. They are importing these three original grammar files and to compile the code, I can use java org.antlr.Tool, where I can specify one directory to search for imported grammars with -lib option argument.

My problem is that these three original imported grammar files have some imports themselves and again in different directory. To illustrate (inheritance/importing going to top):

Abstract syntax directory: lexer           parser           tree parser
Original syntax directory: lexer           parser           tree parser
My new   syntax directory: lexer           parser           tree parser

This is the hierarchy. The trouble I am encountering is how to specify "Abstract syntax directory" and also "Original syntax directory" at once with the -lib option on the command line for the ANTLR tool (or any other solution which would allow me to compile my grammar importing the original one with the given directories structure).

I tried -lib directory1 directory2, that just appears to ignore the directory2 (and then tries to compile it, which it cannot, because it is a directory, not an ANTLR grammar). I tried specifying "-lib directory1 -lib directory2", the "-lib directory2" just overwrites the "-lib directory1" then.

It is what I believe a fairly basic need to specify multiple directories to search in, in any more complicated system of grammars, therefore I am sure I missed something. I just cannot google anything useful out.

IF you need any more details, I am happy to provide them, it is my master thesis extending an open source project, so I do not need to keep anything secret. :) If it is not possible, I can live without it, but would really like to keep the consistency of the original project.

like image 413
Matej Kohut Avatar asked Nov 26 '11 17:11

Matej Kohut


People also ask

In which of the following folders will you find ANTLR grammar definition files?

The default location for the grammar files is in the director src/main/antlr4/ . The plugin will produce . java files for the generated parser in the output director target/generated-sources/antlr4/ .

What grammar does ANTLR use?

ANTLR 2 accepts three types of grammar specifications -- parsers, lexers, and tree-parsers (also called tree-walkers). Because ANTLR 2 uses LL(k) analysis for all three grammar variants, the grammar specifications are similar, and the generated lexers and parsers behave similarly.

How does ANTLR parser work?

ANTLR (ANother Tool for Language Recognition) is a tool for processing structured text. It does this by giving us access to language processing primitives like lexers, grammars, and parsers as well as the runtime to process text against them. It's often used to build tools and frameworks.

Is ANTLR context free?

In fact, there are context-free grammars that you can "specify" with ANTLR that it cannot process correctly, which is true of most parser generators. (For ANTLR, this includes grammars with indirect left recursion, ambiguity, arbitrary lookahead, etc.)


1 Answers

Looking at the source of the org.antlr.Tool class:

else if (args[i].equals("-lib")) {
  if (i + 1 >= args.length) {
    System.err.println("missing library directory with -lib option; ignoring");
  }
  else {
    i++;
    libDirectory = args[i];
    if (libDirectory.endsWith("/") || libDirectory.endsWith("\\")) {
      libDirectory = libDirectory.substring(0,libDirectory.length()-1);
    }
    File outDir = new File(libDirectory);
    if (!outDir.exists()) {
      ErrorManager.error(ErrorManager.MSG_DIR_NOT_FOUND,libDirectory);
      libDirectory = ".";
    }
  }
}

it appears just one -lib directory is being read.

I'm assuming your (implied) question is hereby answered (that it is not possible to point to more than 1 -lib directory).

like image 66
Bart Kiers Avatar answered Nov 08 '22 23:11

Bart Kiers