简体   繁体   中英

Using ANTLR from beanshell or Java program

Is it possible to compile syntax parser from grammar defintion (in String variable) in runtime using ANTLR .jar ?

I want to synthesize grammar description programmatically in beanshell environment, and then call ANTLR to make parser object, without using any files.

Yes it is possible since the parser generator itself is written in Java.

What you need is:

  • put the ANTLR compile-time library on runtime classpath along with the ANTLR runtime library
  • invoke ANTLR's parser generator - in ANTLR 3 the generator class is org.antlr.Tool .
  • if you then want to use the parser in the same Java application, you'll have to load the newly created classes dynamically

The only thing I'm not sure about is whether it is possible to build the parser from a String - maybe you'll have to save it into a temporary file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM