3

I need to parse Bash in Java and generate an AST. This is do some analysis on imported shell scripts to check for potential issues. The analysis is a bit more involved than can be done with regex.

Research I have already done:

  • ANTLR is often great for this, but there isn't an open-source grammar for bash or shell
  • The official bash grammar (parse.y) is in yacc which is heavily tied to C
  • There are some yacc-like parsers for Java, e.g. JavaCC. However, converting the bash yacc looks like a big job.
  • There is a BNF grammar for bash but it's only bash 2.0 and misses many features. There are several BNF parsers for Java, e.g. bullwinkle

At this point I'm pretty stuck. Some ideas I had:

  • Update the BNF grammar to support newer bash features
  • Find some semi-automated way to convert the yacc grammar to a format that can be used with Java
  • Run the yacc parser as native code and interface with JNI

Any further suggestions gratefully received!

Linus Fernandes
  • 498
  • 5
  • 30
paj28
  • 2,210
  • 3
  • 25
  • 37
  • 1
    Also, you can try to implement ANTLR grammar by yourself. Writing grammars is not very hard job. – Kirill Dec 22 '19 at 14:09
  • 2
    There is a [java version of gentoo's libbash bashparser](https://github.com/NitorCreations/bashparser). As a usage example I found [crashub bash](https://github.com/crashub/bash/blob/f26985aa5913411c9cff91463147998c8819c8a9/src/main/java/org/crashub/bash/Script.java#L60). – Socowi Sep 10 '20 at 11:24
  • @Socowi - Nice find, thank-you! – paj28 Sep 10 '20 at 15:00
  • The yacc file can be used to get a tentative grammar as i said in an answer here : https://stackoverflow.com/a/55997867/3225638 I'm not sure if you want to keep the logic defined under each production rule but you could always just refer to them any time if you need (although i don't understand them and just wanted the production rules) – Lorenzo Nov 11 '20 at 07:53
  • I want to do this too. Some bash script checking (e.g. for valid paths) would go a long way to helping everyone's productivity. – Sridhar Sarnobat Jan 25 '23 at 23:31
  • 1
    I just added a wiki page with some sample code: https://github.com/NitorCreations/bashparser/wiki – Sridhar Sarnobat Jan 26 '23 at 07:51

0 Answers0