Satsana

Bash BNF-like Grammar

May 5th, 2019 (edited)
1,487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.18 KB | None | 0 0
  1. inputunit:  simple_list simple_list_terminator
  2.  
  3. word_list:  WORD
  4.     |   word_list WORD
  5.  
  6. redirection:    '>' WORD
  7.     |   '<' WORD
  8.     |   NUMBER '>' WORD
  9.     |   NUMBER '<' WORD
  10.     |   REDIR_WORD '>' WORD
  11.     |   REDIR_WORD '<' WORD
  12.     |   GREATER_GREATER WORD
  13.     |   NUMBER GREATER_GREATER WORD
  14.     |   REDIR_WORD GREATER_GREATER WORD
  15.     |   GREATER_BAR WORD
  16.     |   NUMBER GREATER_BAR WORD
  17.     |   REDIR_WORD GREATER_BAR WORD
  18.     |   LESS_GREATER WORD
  19.     |   NUMBER LESS_GREATER WORD
  20.     |   REDIR_WORD LESS_GREATER WORD
  21.     |   LESS_LESS WORD
  22.     |   NUMBER LESS_LESS WORD
  23.     |   REDIR_WORD LESS_LESS WORD
  24.     |   LESS_LESS_MINUS WORD
  25.     |   NUMBER LESS_LESS_MINUS WORD
  26.     |   REDIR_WORD  LESS_LESS_MINUS WORD
  27.     |   LESS_LESS_LESS WORD
  28.     |   NUMBER LESS_LESS_LESS WORD
  29.     |   REDIR_WORD LESS_LESS_LESS WORD
  30.     |   LESS_AND NUMBER
  31.     |   NUMBER LESS_AND NUMBER
  32.     |   REDIR_WORD LESS_AND NUMBER
  33.     |   GREATER_AND NUMBER
  34.     |   NUMBER GREATER_AND NUMBER
  35.     |   REDIR_WORD GREATER_AND NUMBER
  36.     |   LESS_AND WORD
  37.     |   NUMBER LESS_AND WORD
  38.     |   REDIR_WORD LESS_AND WORD
  39.     |   GREATER_AND WORD
  40.     |   NUMBER GREATER_AND WORD
  41.     |   REDIR_WORD GREATER_AND WORD
  42.     |   GREATER_AND '-'
  43.     |   NUMBER GREATER_AND '-'
  44.     |   REDIR_WORD GREATER_AND '-'
  45.     |   LESS_AND '-'
  46.     |   NUMBER LESS_AND '-'
  47.     |   REDIR_WORD LESS_AND '-'
  48.     |   AND_GREATER WORD
  49.     |   AND_GREATER_GREATER WORD
  50.  
  51. simple_command_element: WORD
  52.     |   ASSIGNMENT_WORD
  53.     |   redirection
  54.  
  55. redirection_list: redirection
  56.     |   redirection_list redirection
  57.  
  58. simple_command: simple_command_element
  59.     |   simple_command simple_command_element
  60.  
  61. command:    simple_command
  62.     |   shell_command
  63.     |   shell_command redirection_list
  64.     |   function_def
  65.     |   coproc
  66.  
  67. shell_command:  for_command
  68.     |   case_command
  69.     |   WHILE compound_list DO compound_list DONE
  70.     |   UNTIL compound_list DO compound_list DONE
  71.     |   select_command
  72.     |   if_command
  73.     |   subshell
  74.     |   group_command
  75.     |   arith_command
  76.     |   cond_command
  77.     |   arith_for_command
  78.  
  79. for_command:    FOR WORD newline_list DO compound_list DONE
  80.     |   FOR WORD newline_list '{' compound_list '}'
  81.     |   FOR WORD ';' newline_list DO compound_list DONE
  82.     |   FOR WORD ';' newline_list '{' compound_list '}'
  83.     |   FOR WORD newline_list IN word_list list_terminator newline_list DO compound_list DONE
  84.     |   FOR WORD newline_list IN word_list list_terminator newline_list '{' compound_list '}'
  85.     |   FOR WORD newline_list IN list_terminator newline_list DO compound_list DONE
  86.     |   FOR WORD newline_list IN list_terminator newline_list '{' compound_list '}'
  87.  
  88. arith_for_command:  FOR ARITH_FOR_EXPRS list_terminator newline_list DO compound_list DONE
  89.     |       FOR ARITH_FOR_EXPRS list_terminator newline_list '{' compound_list '}'
  90.     |       FOR ARITH_FOR_EXPRS DO compound_list DONE
  91.     |       FOR ARITH_FOR_EXPRS '{' compound_list '}'
  92.  
  93. select_command: SELECT WORD newline_list DO list DONE
  94.     |   SELECT WORD newline_list '{' list '}'
  95.     |   SELECT WORD ';' newline_list DO list DONE
  96.     |   SELECT WORD ';' newline_list '{' list '}'
  97.     |   SELECT WORD newline_list IN word_list list_terminator newline_list DO list DONE
  98.     |   SELECT WORD newline_list IN word_list list_terminator newline_list '{' list '}'
  99.     |   SELECT WORD newline_list IN list_terminator newline_list DO compound_list DONE
  100.     |   SELECT WORD newline_list IN list_terminator newline_list '{' compound_list '}'
  101.  
  102. case_command:   CASE WORD newline_list IN newline_list ESAC
  103.     |   CASE WORD newline_list IN case_clause_sequence newline_list ESAC
  104.     |   CASE WORD newline_list IN case_clause ESAC
  105.  
  106. function_def:   WORD '(' ')' newline_list function_body
  107.  
  108.     |   FUNCTION WORD '(' ')' newline_list function_body
  109.  
  110.     |   FUNCTION WORD newline_list function_body
  111.  
  112. function_body:  shell_command
  113.     |   shell_command redirection_list
  114.  
  115. subshell:   '(' compound_list ')'
  116.  
  117. coproc:     COPROC shell_command
  118.     |   COPROC shell_command redirection_list
  119.     |   COPROC WORD shell_command
  120.     |   COPROC WORD shell_command redirection_list
  121.     |   COPROC simple_command
  122.  
  123. if_command: IF compound_list THEN compound_list FI
  124.     |   IF compound_list THEN compound_list ELSE compound_list FI
  125.     |   IF compound_list THEN compound_list elif_clause FI
  126.  
  127.  
  128. group_command:  '{' compound_list '}'
  129.  
  130. arith_command:  ARITH_CMD
  131.  
  132. cond_command:   COND_START COND_CMD COND_END
  133.  
  134. elif_clause:    ELIF compound_list THEN compound_list
  135.     |   ELIF compound_list THEN compound_list ELSE compound_list
  136.     |   ELIF compound_list THEN compound_list elif_clause
  137.  
  138. case_clause:    pattern_list
  139.     |   case_clause_sequence pattern_list
  140.  
  141. pattern_list:   newline_list pattern ')' compound_list
  142.     |   newline_list pattern ')' newline_list
  143.     |   newline_list '(' pattern ')' compound_list
  144.     |   newline_list '(' pattern ')' newline_list
  145.  
  146. case_clause_sequence:  pattern_list SEMI_SEMI
  147.     |   case_clause_sequence pattern_list SEMI_SEMI
  148.     |   pattern_list SEMI_AND
  149.     |   case_clause_sequence pattern_list SEMI_AND
  150.     |   pattern_list SEMI_SEMI_AND
  151.     |   case_clause_sequence pattern_list SEMI_SEMI_AND
  152.  
  153. pattern:    WORD
  154.     |   pattern '|' WORD
  155.  
  156. /* A list allows leading or trailing newlines and
  157.    newlines as operators (equivalent to semicolons).
  158.    It must end with a newline or semicolon.
  159.    Lists are used within commands such as if, for, while.  */
  160.  
  161. list:       newline_list list0
  162.  
  163. compound_list:  list
  164.     |   newline_list list1
  165.  
  166. list0:      list1 '\n' newline_list
  167.     |   list1 '&' newline_list
  168.     |   list1 ';' newline_list
  169.  
  170.  
  171. list1:      list1 AND_AND newline_list list1
  172.     |   list1 OR_OR newline_list list1
  173.     |   list1 '&' newline_list list1
  174.     |   list1 ';' newline_list list1
  175.     |   list1 '\n' newline_list list1
  176.     |   pipeline_command
  177.  
  178. simple_list_terminator: '\n'
  179.     |   yacc_EOF
  180.  
  181. list_terminator:'\n'
  182.     |   ';'
  183.     |   yacc_EOF
  184.  
  185. newline_list:
  186.     |   newline_list '\n'
  187.  
  188. /* A simple_list is a list that contains no significant newlines
  189.    and no leading or trailing newlines.  Newlines are allowed
  190.    only following operators, where they are not significant.
  191.  
  192.    This is what an inputunit consists of.  */
  193.  
  194. simple_list:    simple_list1
  195.     |   simple_list1 '&'
  196.     |   simple_list1 ';'
  197.  
  198. simple_list1:   simple_list1 AND_AND newline_list simple_list1
  199.     |   simple_list1 OR_OR newline_list simple_list1
  200.     |   simple_list1 '&' simple_list1
  201.     |   simple_list1 ';' simple_list1
  202.  
  203.     |   pipeline_command
  204.  
  205. pipeline_command: pipeline         
  206.     |   BANG pipeline_command
  207.     |   timespec pipeline_command
  208.     |   timespec list_terminator
  209.     |   BANG list_terminator
  210.  
  211. pipeline:   pipeline '|' newline_list pipeline
  212.     |   pipeline BAR_AND newline_list pipeline
  213.     |   command
  214.  
  215. timespec:   TIME
  216.     |   TIME TIMEOPT
  217.     |   TIME TIMEOPT TIMEIGN
Advertisement
Add Comment
Please, Sign In to add comment