8

Does anybody know of a Haskell library which can parse arbitrary Bash scripts?

A cursory search of Hackage indicates that there's a package called bash for writing scripts, but I don't see anything for parsing them.

Basically I've just had a large collection of Bash scripts dumped on me, and I'd like to do some code analysis on it. But the first stage is obviously to be able to parse this stuff.

I don't know Bash very well personally. I suppose I could sit down and wage through the volumous man-page to get the complete BNF grammar for it. (I imagine it's very complex, given the shell's long and backwards-compatible history.) I was just wondering whether somebody else has already done this work for me...

7
  • 4
    Best of luck. You will need it if you are going to write the parser yourself. Commented Oct 24, 2012 at 17:55
  • I think you would be better off trying to look at the bash code and working from the parser there. The likelihood of there being a Haskell bash parser in existence seems remote. There are possible libraries that you could use natively from Haskell to get an AST. Possibilities: libbash; jBash; someone else wanting to write a parser, etc. Commented Oct 24, 2012 at 18:35
  • 3
    The yacc file in bash source code is 6000 lines long, and seems to have a fair amount of hackery. But I think the BNF itself is not that bad. Commented Oct 24, 2012 at 19:01
  • 1
    @MathematicalOrchid sh is not normally a symlink to bash, though I believe it is on OSX and some other systems. Debian has a checkbashisms tool that is pretty good. The most commonly used extension is [[ Commented Oct 25, 2012 at 16:08
  • 1
    @singpolyma The script I have definitely contains copious usage of [[. So, yes, I guess it is Bash... Commented Oct 25, 2012 at 17:12

1 Answer 1

4

Perhaps extend language-sh.

Language.Sh is a collection of modules for parsing and manipulating expressions in shell grammar. This is part of a larger project, shsh. Please note that the API is somewhat unstable until we reach version 1.0.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.