Разрешение сдвига / уменьшения в зубре для необязательных конечных атрибутов - PullRequest
0 голосов
/ 22 апреля 2020

Я пытаюсь разработать свой собственный язык программирования. В моем файле грамматики у меня есть что-то похожее на:

moduled_name: NAME attribute_trail {$$=$2? createNode('.',$1,$2) : $1;}
    ;

attribute_trail: {$$=NULL:}
    | attribute_trail_item {$$=$1;}
    ;

attribute_trail_item: '.' NAME {$$=$2;}
    | attribute_trail_item '.' NAME {$$=createNode('.',$1,$3);}
    ;

Итак, moduled_name будет что-то вроде foo или foo.bar или foo.bar.kazam. Однако Бизон сообщает об ошибке сдвига / уменьшения:

state 11

21 moduled_name: NAME . attribute_trail

'.'  shift, and go to state 41

'.'       [reduce using rule 22 (attribute_trail)]
$default  reduce using rule 22 (attribute_trail)

attribute_trail       go to state 42
attribute_trail_item  go to state 43

РЕДАКТИРОВАТЬ:

Вот моя грамматика:

    0 $accept: file $end

    1 file: global_content
    2     | file global_content

    3 global_content: main_definition
    4               | import_statement
    5               | export_statement
    6               | struct_definition
    7               | source_definition
    8               | pipe_definition
    9               | sink_definition
   10               | filter_definition
   11               | predicate_definition
   12               | global_var_definition

   13 main_definition: MAIN '{' statement_list '}'

   14 import_statement: IMPORT NAME ';'

   15 export_statement: EXPORT NAME ';'

   16 struct_definition: STRUCT NAME '{' struct_field '}'

   17 struct_field: NAME ':' type
   18             | struct_field ',' NAME ':' type

   19 type: TYPE
   20     | moduled_name

   21 moduled_name: NAME attribute_trail

   22 attribute_trail: /* empty */
   23                | attribute_trail_item

   24 attribute_trail_item: '.' NAME
   25                     | attribute_trail_item '.' NAME

   26 source_definition: SOURCE optional_name '(' arg_list ')' ARROW type '{' statement_list '}'

   27 pipe_definition: PIPE optional_name '(' arg_list ')' ARROW type '{' statement_list '}'

   28 sink_definition: SINK optional_name '(' arg_list ')' '{' statement_list '}'

   29 local_definition: LOCAL '(' arg_list ')' '{' statement_list '}'

   30 filter_definition: FILTER optional_name '(' arg_list ')' '{' statement_list '}'

   31 predicate_definition: PREDICATE NAME '(' NAME ':' type ')' '{' statement_list '}'

   32 global_var_definition: compilation_expression ARROW NAME ';'

   33 optional_name: /* empty */
   34              | NAME

   35 arg_list: /* empty */
   36         | arg_element

   37 arg_element: NAME ':' type possible_assignment
   38            | arg_element ',' NAME ':' type possible_assignment

   39 possible_assignment: /* empty */
   40                    | '=' compilation_expression

   41 compilation_expression: moduled_name
   42                       | LITERAL attribute_trail
   43                       | compilation_expression '[' compilation_expression ']' attribute_trail
   44                       | '[' compilation_array_literal ']' attribute_trail
   45                       | '(' compilation_expression ')' attribute_trail
   46                       | function_call attribute_trail
   47                       | compilation_expression AND compilation_expression
   48                       | compilation_expression OR compilation_expression
   49                       | NOT compilation_expression
   50                       | compilation_expression '+' compilation_expression
   51                       | compilation_expression '-' compilation_expression
   52                       | compilation_expression '*' compilation_expression
   53                       | compilation_expression '/' compilation_expression
   54                       | compilation_expression '%' compilation_expression
   55                       | compilation_expression '^' compilation_expression
   56                       | compilation_expression '|' compilation_expression
   57                       | compilation_expression '&' compilation_expression
   58                       | compilation_expression LSHIFT compilation_expression
   59                       | compilation_expression RSHIFT compilation_expression

   60 expression: moduled_name
   61           | LITERAL attribute_trail
   62           | CONTEXT attribute_trail
   63           | expression '[' expression ']' attribute_trail
   64           | '[' array_literal ']' attribute_trail
   65           | '(' expression ')' attribute_trail
   66           | function_call attribute_trail
   67           | expression AND expression
   68           | expression OR expression
   69           | NOT expression
   70           | expression COMPARISON expression
   71           | expression '+' expression
   72           | expression '-' expression
   73           | expression '*' expression
   74           | expression '/' expression
   75           | expression '%' expression
   76           | expression '^' expression
   77           | expression '|' expression
   78           | expression '&' expression
   79           | expression LSHIFT expression
   80           | expression RSHIFT expression
   81           | moduled_name IS type
   82           | '(' expression ')' IS type
   83           | '(' arrow_statement ')'

   84 compilation_array_literal: /* empty */
   85                          | compilation_array_element

   86 compilation_array_element: compilation_expression
   87                          | compilation_array_element ',' compilation_expression

   88 array_literal: /* empty */
   89              | array_element

   90 array_element: expression
   91              | array_element ',' expression

   92 function_call: moduled_name '(' call_list ')'

   93 call_list: /* empty */
   94          | call_arg
   95          | call_list ',' call_arg

   96 call_arg: expression
   97         | NAME '=' expression

   98 statement_list: /* empty */
   99               | actual_statement_list

  100 actual_statement_list: general_statement
  101                      | actual_statement_list general_statement

  102 general_statement: ';'
  103                  | statement ';'
  104                  | if_section
  105                  | while_section

  106 if_section: IF expression '{' statement_list '}' eif_section else_section

  107 eif_section: /* empty */
  108            | eif_section EIF expression '{' statement_list '}'

  109 else_section: /* empty */
  110             | ELSE '{' statement_list '}'

  111 while_section: WHILE expression '{' statement_list '}'

  112 statement: PROD expression
  113          | DROP
  114          | END
  115          | CONT
  116          | BREAK
  117          | ABORT expression
  118          | VERIFY expression
  119          | NAME AS type
  120          | expression OPERATOR_ASSIGNMENT expression
  121          | arrow_statement

  122 arrow_statement: arrow_sender ARROW arrow_receiver

  123 arrow_sender: arrow_sender_item
  124             | '(' arrow_sender_list ',' arrow_sender_item ')'

  125 arrow_sender_item: expression
  126                  | source_definition

  127 arrow_sender_list: arrow_sender_item
  128                  | arrow_sender_list ',' arrow_sender_item

  129 arrow_receiver: arrow_receiver_item
  130               | arrow_receiver ARROW arrow_receiver_item

  131 arrow_receiver_item: moduled_name attribute_trail
  132                    | moduled_name attribute_trail ':' NAME
  133                    | TYPE attribute_trail
  134                    | pipe_definition attribute_trail
  135                    | sink_definition attribute_trail
  136                    | sink_definition attribute_trail ':' NAME
  137                    | local_definition attribute_trail

1 Ответ

2 голосов
/ 23 апреля 2020

Проблема исходит из правила

 131 arrow_receiver_item: moduled_name attribute_trail

(и других подобных). Таким образом, moduled_name может иметь attribute_trail, а за ним может следовать attribute_trail. Не ясно, является ли этот атрибут следом частью moduled_name или частью arrow_receiver_item.

...