Eric Lavigne
2007-05-27 20:22:55 UTC
Menhir (similar to ocamlyacc) indicates that my grammar file has a
shift/reduce conflict. If I remove the definition of "box" from my
grammar file, then it works correctly and Menhir produces no warnings.
I have attached my grammar file at the end of this email in hopes that
someone will explain what I did wrong.
The following site explains that shift/reduce is a type of ambiguity,
but I do not see how this applies to my situation.
http://plus.kaist.ac.kr/~shoh/ocaml/ocamllex-ocamlyacc/ocamlyacc-tutorial/sec-shift-reduce-conflicts.html
This parser is designed to parse an array of numbers, which may be a
0-D, 1-D, 2-D, or 3-D array, as shown in examples below.
0-D (scalar):
1
1-D (row):
1 2 3 4 5
1-D (column):
1
2
3
2-D (matrix):
1 2 3
4 5 6
3-D (box):
1 2 3
1 2 3
4 5 6
4 5 6
The grammar file:
%{
open Tree
%}
/* Declarations */
%token NEWLINE EOF
%token <float> NUMBER
%start main
%type <float Tree.t> main
%%
/* Grammar rules */
ending:
| EOF {}
| NEWLINE ending {};
scalar:
| NUMBER NEWLINE {Leaf $1};
row:
| NUMBER NUMBER NEWLINE {[(Leaf $1); (Leaf $2)]}
| NUMBER row { (Leaf $1) :: $2 };
column:
| NUMBER NEWLINE NUMBER NEWLINE {[(Leaf $1);(Leaf $3)]}
| NUMBER NEWLINE column {(Leaf $1) :: $3};
matrix:
| row row {[(Node $1);(Node $2)]}
| row matrix { (Node $1) :: $2 };
box:
| matrix NEWLINE matrix {[(Node $1);(Node $3)]}
| matrix NEWLINE box {(Node $1) :: $3};
main:
| scalar ending { $1 }
| row ending { Node $1 }
| column ending { Node $1 }
| box ending { Node $1 }
| matrix ending { Node $1 };
%%
shift/reduce conflict. If I remove the definition of "box" from my
grammar file, then it works correctly and Menhir produces no warnings.
I have attached my grammar file at the end of this email in hopes that
someone will explain what I did wrong.
The following site explains that shift/reduce is a type of ambiguity,
but I do not see how this applies to my situation.
http://plus.kaist.ac.kr/~shoh/ocaml/ocamllex-ocamlyacc/ocamlyacc-tutorial/sec-shift-reduce-conflicts.html
This parser is designed to parse an array of numbers, which may be a
0-D, 1-D, 2-D, or 3-D array, as shown in examples below.
0-D (scalar):
1
1-D (row):
1 2 3 4 5
1-D (column):
1
2
3
2-D (matrix):
1 2 3
4 5 6
3-D (box):
1 2 3
1 2 3
4 5 6
4 5 6
The grammar file:
%{
open Tree
%}
/* Declarations */
%token NEWLINE EOF
%token <float> NUMBER
%start main
%type <float Tree.t> main
%%
/* Grammar rules */
ending:
| EOF {}
| NEWLINE ending {};
scalar:
| NUMBER NEWLINE {Leaf $1};
row:
| NUMBER NUMBER NEWLINE {[(Leaf $1); (Leaf $2)]}
| NUMBER row { (Leaf $1) :: $2 };
column:
| NUMBER NEWLINE NUMBER NEWLINE {[(Leaf $1);(Leaf $3)]}
| NUMBER NEWLINE column {(Leaf $1) :: $3};
matrix:
| row row {[(Node $1);(Node $2)]}
| row matrix { (Node $1) :: $2 };
box:
| matrix NEWLINE matrix {[(Node $1);(Node $3)]}
| matrix NEWLINE box {(Node $1) :: $3};
main:
| scalar ending { $1 }
| row ending { Node $1 }
| column ending { Node $1 }
| box ending { Node $1 }
| matrix ending { Node $1 };
%%