I am writing a grammar for a complete programming language of my own design. This language has several types of expressions that are combined in different ways in different situations. I have a pretty good idea of how I want it to work, but I am having trouble with factoring out the shift/reduce and reduce/reduce conflicts. I am using Bison v3.0.4 under Xubuntu 16.04. The full grammar (including the *.output file) can be seen in my github at https://github.com/chucktilbury/Simple1 (see expressions.y and expressions.output)
I have gotten pretty far with it. I know it's not the best, but I am learning. If someone could give some pointers to help me get unstuck, I would appreciate it.
Here is a snip of the part of the grammar that is giving me problems:
%{
#include <stdio.h>
%}
%token OPAREN_TOK CPAREN_TOK OCURLY_TOK CCURLY_TOK OBOX_TOK CBOX_TOK
%token COMMA_TOK SCOLON_TOK DOT_TOK COLON_TOK
%token CLASS_TOK FUNC_TOK PRIVATE_TOK PUBLIC_TOK PROTECTED_TOK
%token CREATE_TOK DESTROY_TOK IMPORT_TOK STRUCT_TOK
%token PLUS_TOK MINUS_TOK MULT_TOK DIV_TOK MODULO_TOK ASSIGN_TOK
%token BIT_NOT_TOK BIT_OR_TOK BIT_AND_TOK BIT_XOR_TOK BIT_LSH_TOK BIT_RSH_TOK
%token INT_TOK FLOAT_TOK UNSD_TOK STRG_TOK
%token BOOL_TOK
%token RETURN_TOK BREAK_TOK CONT_TOK IF_TOK ELSE_TOK WHILE_TOK
%token FOR_TOK SWITCH_TOK CASE_TOK
%token OR_TOK AND_TOK NOT_TOK EQ_TOK GEQ_TOK LEQ_TOK
%token NEQ_TOK MORE_TOK LESS_TOK
%token TRUE_TOK FALSE_TOK NOTHING_TOK
%token SYMBOL_TOK UNSIGNED_TOK INTEGER_TOK FLOATING_TOK STRING_TOK
%left MINUS_TOK PLUS_TOK
%left MULT_TOK DIV_TOK
%left NEGATION
%right CARAT_TOK /* exponentiation */
%%
expression
: arithmetic_expression
| boolean_expression
| bitwise_expression
;
compound_symbol
: SYMBOL_TOK
| compound_symbol DOT_TOK SYMBOL_TOK
;
exponent_numeric_value
: FLOATING_TOK
| INTEGER_TOK
;
arithmetic_factor
: INTEGER_TOK
| FLOAT_TOK
| UNSIGNED_TOK
| exponent_numeric_value CARAT_TOK exponent_numeric_value
| compound_symbol
;
arithmetic_expression
: arithmetic_factor
| arithmetic_expression PLUS_TOK arithmetic_expression
| arithmetic_expression MINUS_TOK arithmetic_expression
| arithmetic_expression MULT_TOK arithmetic_expression
| arithmetic_expression DIV_TOK arithmetic_expression
| MINUS_TOK arithmetic_expression %prec NEGATION
| OPAREN_TOK arithmetic_expression CPAREN_TOK
;
boolean_factor
: arithmetic_factor
| TRUE_TOK
| FALSE_TOK
| STRING_TOK
;
boolean_expression
: boolean_factor
| boolean_expression OR_TOK boolean_expression
| boolean_expression AND_TOK boolean_expression
| boolean_expression EQ_TOK boolean_expression
| boolean_expression NEQ_TOK boolean_expression
| boolean_expression LEQ_TOK boolean_expression
| boolean_expression GEQ_TOK boolean_expression
| boolean_expression MORE_TOK boolean_expression
| boolean_expression LESS_TOK boolean_expression
| NOT_TOK boolean_expression
| OPAREN_TOK boolean_expression CPAREN_TOK
;
bitwise_factor
: INTEGER_TOK
| UNSIGNED_TOK
| compound_symbol
;
bitwise_expression
: bitwise_factor
| bitwise_expression BIT_AND_TOK bitwise_expression
| bitwise_expression BIT_OR_TOK bitwise_expression
| bitwise_expression BIT_XOR_TOK bitwise_expression
| bitwise_expression BIT_LSH_TOK bitwise_expression
| bitwise_expression BIT_RSH_TOK bitwise_expression
| BIT_NOT_TOK bitwise_expression
| OPAREN_TOK bitwise_expression CPAREN_TOK
;
%%
This yields 102 shift-reduce and 8 reduce-reduce conflicts. I get that I have some of the tokens reused in rules and the root non-terminal is contrived. I am having trouble figuring out how to organize them so that the correct (sometimes the same) types are associated with the correct type of expression. I have tried reorganizing in various ways. I think it's clear that I am missing something. Maybe my whole approach is all wrong, but I am unclear what the correct approach would be for this.
For a better (but very incomplete) explanation of what I am really trying to do, see the readme on this repository: https://github.com/chucktilbury/toi