From my understanding, parser generators accept as input some form of context-free grammar description. The context-sensitive features are handled during semantic rather than syntactic analysis (please correct me if this is wrong).

Since shell languages inherently have a lot of context-sensitive features, is it worthwhile using a parser generator? If not, what techniques could be used for a precise definition and parsing of a shell language?

In conclusion: my understanding of what context-sensitive grammar means was wrong.

  • 4
    Citation needed on the context-sensitiveness of shell languages...
    – Telastyn
    Oct 11, 2019 at 15:56
  • 3
    “context-free” and ”context-sensitive“ has a specific meaning in computer science that does not match it's everyday usage. Most programming languages incl. shell languages have a context-free syntax. In practice, it's often possible to extend CFGs with context-sensitive features by introducing pseudotokens, e.g. INDENT and DEDENT tokens in Python or a token representing a word boundary. However, such approaches require scannerless parsers or a more complex lexer.
    – amon
    Oct 11, 2019 at 16:07
  • 1
    Upvoting given the edits and good comments/answers Oct 13, 2019 at 1:32

1 Answer 1


Context-free grammars are grammars where the left side of every production rule is a non terminal term (aka an abstract grammar construct) whereas the right side can be a mix of terminals (aka tokens that you find in the language) and non-terminals.

Context-sensitive grammars are more tricky, since both the left and the right side of production rules can be a mix of terminals and non terminals. To express it simply, the meaning of a term depends on its surroundings (aka “context”).

Command shells usually have a straightforward grammar that can be expressed with context-free grammars. I’d really wonder if you’d really need a context sensitive one.

I’d tempted to say that command shells are easily implemented using simple top down interpreter (see also the interpreter pattern) and that parser generators seem to me somewhat an overkill. But I have not seen your language so I cannot tell for sure that it makes sense in your case.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.