GRAMMAR 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # GRAMMAR
  2. <command> ::= <token>+
  3. <pipe> ::= <command> '|' <pipe>
  4. | <command>
  5. <conj> ::= <pipe> '&&' <conj>
  6. | <pipe>
  7. <disj> ::= <conj> '||' <disj>
  8. | <conj>
  9. <line> ::= <disj> ('<' <token>)? ('>' <token>)? '&'?
  10. or in words:
  11. A command is a sequence of tokens like: grep -v foo
  12. A pipe is a sequence of commands with | between: ps aux | grep watch | grep -v grep
  13. A disj is a sequence of pipes with || in between, like: mount /proc || exit -1
  14. note: | binds tighter than && which binds tighter than ||
  15. And each line can optionally be set to run in the background/in parallel using &
  16. you can also (optionally) choose to redirect stdin/out
  17. # AST
  18. So the abstract syntax tree ends up being a right associative tree of '||' nodes,
  19. containing right associative trees of '&&' nodes, containing ... '|' nodes which
  20. contain command nodes.
  21. # VARIABLES
  22. After tokenization variables are expanded.
  23. The names of variables are made from these charactors: [A-Z0-9_]
  24. "Environment variable names used by the utilities in the Shell and Utilities volume of IEEE Std 1003.1-2001 consist solely of uppercase letters, digits, and the '_' (underscore) from the characters defined in Portable Character Set and do not begin with a digit."
  25. * http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
  26. The syntax is $VAR or ${VAR}. The reason we have ${VAR} is to allow things like ${FOO}BAR.
  27. # SYNTAX
  28. builtins
  29. 'cd' <directory>