Skip to content

Commit

Permalink
nitcc: add examples in case of conflicts #2842
Browse files Browse the repository at this point in the history
Add example of possible prefix (past) and suffix (future) in case of conflict shift/reduce or reduce/reduce.

Example:

```
SHIFT/REDUCE Conflict on state 3 'a' for token 'b':
A possible past: ['a']
SHIFT on item: x::x_1=.'b'
A possible future: ['b',Eof]
_start
├╴e
│ ├╴'a'
│ └╴x
│   └╴'b'
└╴Eof

REDUCE on item: x::x_0= 'a'.
A possible future: ['b',Eof]
_start
├╴e
│ ├╴x
│ │ └╴'a'
│ └╴'b'
└╴Eof

AMBIGUITY detected: same elements, different trees
```

This should help the user to contextualize some conflicts and enable them to fix their grammar.
  • Loading branch information
privat authored Jul 21, 2024
2 parents da4205e + a45fd3d commit 0dcfe3a
Show file tree
Hide file tree
Showing 43 changed files with 605 additions and 42 deletions.
369 changes: 327 additions & 42 deletions contrib/nitcc/src/grammar.nit

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions contrib/nitcc/tests/amb.input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
id
4 changes: 4 additions & 0 deletions contrib/nitcc/tests/amb.sablecc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Grammar amb;
Parser
Ignored #10, #32;
e = 'id' | e | e;
1 change: 1 addition & 0 deletions contrib/nitcc/tests/amb2.input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-0+
6 changes: 6 additions & 0 deletions contrib/nitcc/tests/amb2.sablecc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Grammar amb;
Parser
Ignored #10, #32;
e = '-' a | b '+' | '0';
a = e ;
b = e ;
1 change: 1 addition & 0 deletions contrib/nitcc/tests/amb3.input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a b
5 changes: 5 additions & 0 deletions contrib/nitcc/tests/amb3.sablecc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Grammar amb;
Parser
Ignored #10, #32;
e = x 'b' | 'a' x ;
x = 'a' | 'b';
1 change: 1 addition & 0 deletions contrib/nitcc/tests/calc0.input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1+2+3*(4+5)
11 changes: 11 additions & 0 deletions contrib/nitcc/tests/calc0.sablecc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Grammar calc;
Lexer
n = ('0'..'9')+ ;
Parser
Ignored #10, #32;
e
= e '+' e
| e '*' e
| '(' e ')'
| n
;
1 change: 1 addition & 0 deletions contrib/nitcc/tests/eq.input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
id=id=id
9 changes: 9 additions & 0 deletions contrib/nitcc/tests/eq.sablecc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Grammar eq;
Parser
Ignored #10, #32;
e =
var '=' e |
var ;
c =
e '=' e ;
var = 'id' ;
1 change: 1 addition & 0 deletions contrib/nitcc/tests/eq2.input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
id=id=id
10 changes: 10 additions & 0 deletions contrib/nitcc/tests/eq2.sablecc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Grammar eq;
Parser
Ignored #10, #32;
e =
var '=' e |
'assert' c |
var ;
c =
e '=' e ;
var = 'id' ;
1 change: 1 addition & 0 deletions contrib/nitcc/tests/eq3.input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
id=id=id
10 changes: 10 additions & 0 deletions contrib/nitcc/tests/eq3.sablecc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Grammar eq;
Parser
Ignored #10, #32;
e =
var '=' e |
c |
var ;
c =
e '=' e ;
var = 'id' ;
1 change: 1 addition & 0 deletions contrib/nitcc/tests/geom.input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
triangle 0x0 0@0 centre circle 0x0 0@0
8 changes: 8 additions & 0 deletions contrib/nitcc/tests/geom.sablecc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Grammar geom;
Parser
Ignored #10, #32;
forme =
'triangle' point point point |
'circle' point point ;
point = num 'x' num | num '@' num | 'centre' forme;
num = '0';
1 change: 1 addition & 0 deletions contrib/nitcc/tests/lg.input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<0><0>>
1 change: 1 addition & 0 deletions contrib/nitcc/tests/lg.input2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<0><0>0
6 changes: 6 additions & 0 deletions contrib/nitcc/tests/lg.sablecc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Grammar lg;
Parser
Ignored #10, #32;
e = e '<' a | e '>' a | a ;
a = n | '<' e '>' ;
n = '0' ;
1 change: 1 addition & 0 deletions contrib/nitcc/tests/lg2.input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<0><0>>
1 change: 1 addition & 0 deletions contrib/nitcc/tests/lg2.input2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<0><0>0
5 changes: 5 additions & 0 deletions contrib/nitcc/tests/lg2.sablecc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Grammar lg;
Parser
Ignored #10, #32;
e = e '<' e | e '>' e | n | '<' e '>' ;
n = '0' ;
4 changes: 4 additions & 0 deletions contrib/nitcc/tests/sav/amb.input.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Start
e_0
'id'@(1:1-1:3)
Eof@(2:1-2:1)=''
1 change: 1 addition & 0 deletions contrib/nitcc/tests/sav/amb.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Error: there is conflicts
10 changes: 10 additions & 0 deletions contrib/nitcc/tests/sav/amb2.input.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Start
e_1
b
e_0
'-'@(1:1-1:2)
a
e_2
'0'@(1:2-1:3)
'+'@(1:3-1:4)
Eof@(2:1-2:1)=''
1 change: 1 addition & 0 deletions contrib/nitcc/tests/sav/amb2.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Error: there is conflicts
6 changes: 6 additions & 0 deletions contrib/nitcc/tests/sav/amb3.input.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Start
e_1
'a'@(1:1-1:2)
x_1
'b'@(1:3-1:4)
Eof@(2:1-2:1)=''
1 change: 1 addition & 0 deletions contrib/nitcc/tests/sav/amb3.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Error: there is conflicts
23 changes: 23 additions & 0 deletions contrib/nitcc/tests/sav/calc0.input.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Start
e_0
e_3
n@(1:1-1:2)='1'
'+'@(1:2-1:3)
e_0
e_3
n@(1:3-1:4)='2'
'+'@(1:4-1:5)
e_1
e_3
n@(1:5-1:6)='3'
'*'@(1:6-1:7)
e_2
'('@(1:7-1:8)
e_0
e_3
n@(1:8-1:9)='4'
'+'@(1:9-1:10)
e_3
n@(1:10-1:11)='5'
')'@(1:11-1:12)
Eof@(2:1-2:1)=''
13 changes: 13 additions & 0 deletions contrib/nitcc/tests/sav/eq.input.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Start
e_0
var
'id'@(1:1-1:3)
'='@(1:3-1:4)
e_0
var
'id'@(1:4-1:6)
'='@(1:6-1:7)
e_1
var
'id'@(1:7-1:9)
Eof@(2:1-2:1)=''
1 change: 1 addition & 0 deletions contrib/nitcc/tests/sav/eq.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Error: there is conflicts
13 changes: 13 additions & 0 deletions contrib/nitcc/tests/sav/eq2.input.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Start
e_0
var
'id'@(1:1-1:3)
'='@(1:3-1:4)
e_0
var
'id'@(1:4-1:6)
'='@(1:6-1:7)
e_2
var
'id'@(1:7-1:9)
Eof@(2:1-2:1)=''
1 change: 1 addition & 0 deletions contrib/nitcc/tests/sav/eq2.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Error: there is conflicts
13 changes: 13 additions & 0 deletions contrib/nitcc/tests/sav/eq3.input.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Start
e_0
var
'id'@(1:1-1:3)
'='@(1:3-1:4)
e_0
var
'id'@(1:4-1:6)
'='@(1:6-1:7)
e_2
var
'id'@(1:7-1:9)
Eof@(2:1-2:1)=''
1 change: 1 addition & 0 deletions contrib/nitcc/tests/sav/eq3.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Error: there is conflicts
32 changes: 32 additions & 0 deletions contrib/nitcc/tests/sav/geom.input.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Start
forme_0
'triangle'@(1:1-1:9)
point_0
num
'0'@(1:10-1:11)
'x'@(1:11-1:12)
num
'0'@(1:12-1:13)
point_1
num
'0'@(1:14-1:15)
'@'@(1:15-1:16)
num
'0'@(1:16-1:17)
point_2
'centre'@(1:18-1:24)
forme_1
'circle'@(1:25-1:31)
point_0
num
'0'@(1:32-1:33)
'x'@(1:33-1:34)
num
'0'@(1:34-1:35)
point_1
num
'0'@(1:36-1:37)
'@'@(1:37-1:38)
num
'0'@(1:38-1:39)
Eof@(2:1-2:1)=''
19 changes: 19 additions & 0 deletions contrib/nitcc/tests/sav/lg.input.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Start
e_2
a_1
'<'@(1:1-1:2)
e_1
e_2
a_0
n
'0'@(1:2-1:3)
'>'@(1:3-1:4)
a_1
'<'@(1:4-1:5)
e_2
a_0
n
'0'@(1:5-1:6)
'>'@(1:6-1:7)
'>'@(1:7-1:8)
Eof@(2:1-2:1)=''
19 changes: 19 additions & 0 deletions contrib/nitcc/tests/sav/lg.input2.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
NParserError@(2:1-2:1)=''
Nodes[Node]
'<'@(1:1-1:2)
e_2
a_0
n
'0'@(1:2-1:3)
'>'@(1:3-1:4)
'<'@(1:4-1:5)
e_1
e_2
a_0
n
'0'@(1:5-1:6)
'>'@(1:6-1:7)
a_0
n
'0'@(1:7-1:8)
Eof@(2:1-2:1)=''
1 change: 1 addition & 0 deletions contrib/nitcc/tests/sav/lg.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Error: there is conflicts
15 changes: 15 additions & 0 deletions contrib/nitcc/tests/sav/lg2.input.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
NParserError@(2:1-2:1)=''
Nodes[Node]
'<'@(1:1-1:2)
e_2
n
'0'@(1:2-1:3)
'>'@(1:3-1:4)
e_3
'<'@(1:4-1:5)
e_2
n
'0'@(1:5-1:6)
'>'@(1:6-1:7)
'>'@(1:7-1:8)
Eof@(2:1-2:1)=''
17 changes: 17 additions & 0 deletions contrib/nitcc/tests/sav/lg2.input2.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
NParserError@(2:1-2:1)=''
Nodes[Node]
'<'@(1:1-1:2)
e_2
n
'0'@(1:2-1:3)
'>'@(1:3-1:4)
'<'@(1:4-1:5)
e_1
e_2
n
'0'@(1:5-1:6)
'>'@(1:6-1:7)
e_2
n
'0'@(1:7-1:8)
Eof@(2:1-2:1)=''
1 change: 1 addition & 0 deletions contrib/nitcc/tests/sav/lg2.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Error: there is conflicts

0 comments on commit 0dcfe3a

Please sign in to comment.