263 lines
4.1 KiB
Text
263 lines
4.1 KiB
Text
%{
|
|
void yyerror(const char *);
|
|
int yylex(void);
|
|
%}
|
|
|
|
%token ERROR
|
|
%token NATURAL IDENTIFIER
|
|
%token FN TYPE
|
|
%token BREAK CONTINUE DO ELIF ELSE END FOR IF IN MATCH OF RETURN THEN WHILE
|
|
%token AND OR NOT
|
|
%token EQ NEQ LTE GTE
|
|
%token SHL SHR ROL ROR
|
|
|
|
%%
|
|
|
|
toplevel:
|
|
toplevel definition
|
|
|
|
|
|
|
definition:
|
|
FN IDENTIFIER '(' parameter_list ')' optional_type block END
|
|
| TYPE type_declaration
|
|
|
|
parameter_list:
|
|
nonempty_parameter_list
|
|
| nonempty_parameter_list ','
|
|
|
|
|
|
|
nonempty_parameter_list:
|
|
parameter_declaration
|
|
| nonempty_parameter_list ',' parameter_declaration
|
|
|
|
parameter_declaration:
|
|
IDENTIFIER ':' type
|
|
| IDENTIFIER IDENTIFIER ':' type
|
|
| '=' IDENTIFIER ':' type
|
|
|
|
optional_type:
|
|
':' type
|
|
|
|
|
|
|
type_declaration_list:
|
|
type_declaration_list type_declaration
|
|
|
|
|
|
|
type_declaration:
|
|
IDENTIFIER '{' field_list '}'
|
|
| IDENTIFIER OF type_declaration_list END
|
|
|
|
field_list:
|
|
nonempty_field_list
|
|
| nonempty_field_list ','
|
|
|
|
|
|
|
nonempty_field_list:
|
|
field_declaration
|
|
| nonempty_field_list ',' field_declaration
|
|
|
|
field_declaration:
|
|
IDENTIFIER ':' type
|
|
| type
|
|
|
|
block:
|
|
statement_list optional_return_statement
|
|
|
|
statement_list:
|
|
statement_list statement
|
|
|
|
|
|
|
optional_return_statement:
|
|
RETURN expr
|
|
| RETURN
|
|
|
|
|
|
|
statement:
|
|
namespaced_name '(' argument_list ')'
|
|
| assignment
|
|
| modification
|
|
| IF expr THEN block elif_list optional_else END
|
|
| MATCH expr IN match_option_list END
|
|
| WHILE expr DO block END
|
|
| FOR pattern IN expr DO block optional_else END
|
|
| BREAK
|
|
| CONTINUE
|
|
|
|
namespaced_name:
|
|
IDENTIFIER
|
|
| namespaced_name '.' IDENTIFIER
|
|
|
|
elif_list:
|
|
elif_list ELIF expr THEN block
|
|
|
|
|
|
|
optional_else:
|
|
ELSE block
|
|
|
|
|
|
|
match_option_list:
|
|
match_option_list match_option
|
|
|
|
|
|
|
match_option:
|
|
pattern_conjunction THEN block END
|
|
| ELSE block END
|
|
|
|
pattern_conjunction:
|
|
pattern
|
|
| pattern_conjunction '|' pattern
|
|
|
|
assignment:
|
|
pattern optional_type '=' expr
|
|
|
|
pattern:
|
|
IDENTIFIER
|
|
| NATURAL
|
|
| namespaced_name '{' argument_pattern_list '}'
|
|
| '@' place
|
|
| '$' atom
|
|
|
|
argument_pattern_list:
|
|
nonempty_argument_pattern_list
|
|
| nonempty_argument_pattern_list ','
|
|
|
|
|
|
|
nonempty_argument_pattern_list:
|
|
argument_pattern
|
|
| nonempty_argument_pattern_list ',' argument_pattern
|
|
|
|
argument_pattern:
|
|
IDENTIFIER ':' pattern
|
|
| '=' IDENTIFIER
|
|
| pattern
|
|
|
|
place:
|
|
IDENTIFIER
|
|
| place '[' expr ']'
|
|
| place '.' IDENTIFIER
|
|
|
|
modification:
|
|
'@' place binary_operator '=' expr
|
|
|
|
binary_operator: sum_operator | product_operator | '&' | '|' | '^' | shift_operator
|
|
|
|
expr:
|
|
conjunction
|
|
| disjunction
|
|
| comparison
|
|
| bitop
|
|
|
|
conjunction:
|
|
comparison OR comparison
|
|
| conjunction OR comparison
|
|
|
|
disjunction:
|
|
comparison AND comparison
|
|
| disjunction AND comparison
|
|
|
|
comparison:
|
|
sum
|
|
| sum EQ sum
|
|
| sum NEQ sum
|
|
| sum not_less_operator sum
|
|
| sum not_less_operator sum not_less_operator sum
|
|
| sum '>' sum
|
|
| sum GTE sum
|
|
|
|
not_less_operator: '<' | LTE
|
|
|
|
sum:
|
|
product
|
|
| sum sum_operator product
|
|
|
|
sum_operator: '+' | '-'
|
|
|
|
product:
|
|
unary
|
|
| product product_operator unary
|
|
|
|
product_operator: '*' | '/' | '%'
|
|
|
|
bitop:
|
|
bitand
|
|
| bitor
|
|
| bitxor
|
|
| bitshift
|
|
|
|
bitand:
|
|
unary '&' unary
|
|
| bitand '&' unary
|
|
|
|
bitor:
|
|
unary '|' unary
|
|
| bitor '|' unary
|
|
|
|
bitxor:
|
|
unary '^' unary
|
|
| bitxor '^' unary
|
|
|
|
bitshift:
|
|
unary shift_operator unary
|
|
|
|
shift_operator: SHL | SHR | ROL | ROR
|
|
|
|
unary:
|
|
atom
|
|
| '-' unary
|
|
| '~' unary
|
|
| NOT unary
|
|
|
|
atom:
|
|
literal
|
|
| IDENTIFIER
|
|
| atom '(' argument_list ')'
|
|
| atom '[' expr ']'
|
|
| atom '.' IDENTIFIER
|
|
| '(' expr ')'
|
|
|
|
literal:
|
|
NATURAL
|
|
| '[' expr_list ']'
|
|
| atom '{' argument_list '}'
|
|
|
|
expr_list:
|
|
nonempty_expr_list
|
|
| nonempty_expr_list ','
|
|
|
|
|
|
|
nonempty_expr_list:
|
|
expr
|
|
| nonempty_expr_list ',' expr
|
|
|
|
argument_list:
|
|
nonempty_argument_list
|
|
| nonempty_argument_list ','
|
|
|
|
|
|
|
nonempty_argument_list:
|
|
argument
|
|
| nonempty_argument_list ',' argument
|
|
|
|
argument:
|
|
IDENTIFIER ':' expr
|
|
| '=' IDENTIFIER
|
|
| expr
|
|
|
|
type:
|
|
namespaced_name
|
|
| type '[' expr ']'
|
|
|
|
%%
|
|
|
|
// Some distributions of bison remove the provided liby (see
|
|
// https://bugs.gentoo.org/204416). Include implementations of yyerror()
|
|
// and main() inline to avoid depending on it.
|
|
#include <stdio.h>
|
|
|
|
void yyerror(const char *s) {
|
|
fprintf(stderr, "%s\n", s);
|
|
}
|
|
|
|
int main(void) {
|
|
return yyparse();
|
|
}
|