From cb2a34b1a3e4f1dc28ace238c843b43c31d25077 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Mon, 22 Jul 2024 13:17:35 -0400 Subject: [PATCH 1/2] nitcc: detect and handle correctly states without shifts nor reduces Signed-off-by: Jean Privat --- contrib/nitcc/src/lrautomaton.nit | 17 ++++++++++++----- contrib/nitcc/tests/calc1.input | 1 + contrib/nitcc/tests/calc1.sablecc | 1 + contrib/nitcc/tests/gram_empty.input | 1 + contrib/nitcc/tests/gram_empty.sablecc | 3 +++ contrib/nitcc/tests/gram_empty2.input | 1 + contrib/nitcc/tests/gram_empty2.sablecc | 4 ++++ contrib/nitcc/tests/sav/calc1.input.res | 17 +++++++++++++++++ contrib/nitcc/tests/sav/calc1.res | 1 - contrib/nitcc/tests/sav/gram_empty.input.res | 3 +++ contrib/nitcc/tests/sav/gram_empty.res | 1 + contrib/nitcc/tests/sav/gram_empty2.input.res | 5 +++++ 12 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 contrib/nitcc/tests/calc1.input create mode 100644 contrib/nitcc/tests/gram_empty.input create mode 100644 contrib/nitcc/tests/gram_empty.sablecc create mode 100644 contrib/nitcc/tests/gram_empty2.input create mode 100644 contrib/nitcc/tests/gram_empty2.sablecc create mode 100644 contrib/nitcc/tests/sav/calc1.input.res create mode 100644 contrib/nitcc/tests/sav/gram_empty.input.res create mode 100644 contrib/nitcc/tests/sav/gram_empty.res create mode 100644 contrib/nitcc/tests/sav/gram_empty2.input.res diff --git a/contrib/nitcc/src/lrautomaton.nit b/contrib/nitcc/src/lrautomaton.nit index 6a955fde32..392ecddb12 100644 --- a/contrib/nitcc/src/lrautomaton.nit +++ b/contrib/nitcc/src/lrautomaton.nit @@ -440,11 +440,9 @@ redef class Generator add "\tredef fun action(parser) do" if s.need_guard then add "\t\tparser.peek_token.action_s{s.number}(parser)" - else if s.reduces.length == 1 then + else add "\t\treduce_{s.reduces.first.cname}(parser)" #gen_reduce_to_nit(s.reduces.first) - else - abort end add "\tend" @@ -613,8 +611,11 @@ class LRState # Shifts guarded by tokens var guarded_shift = new HashMap[Token, Set[Item]] - # Does the state need a guard to perform an action? - fun need_guard: Bool do return not shifts.is_empty or reduces.length > 1 + # Does the state need a guard that look at (or consume) the next token to perform an action? + # The only `false` case is a state with a single reduce action, it can be performed without checking the token. + # A shift always consume the token. + # Empty state with no valid future, must consume the token to produces a syntax error + fun need_guard: Bool do return not (shifts.is_empty and reduces.length == 1) # Is the state LR0? fun is_lr0: Bool do return reduces.length <= 1 and shifts.is_empty or reduces.is_empty @@ -650,6 +651,12 @@ class LRState abort end end + if shifts.is_empty and reduces.is_empty then + print "---" + print "Empty state {self} without any reduce or shift" + print "The possible past: {prefix} has no valid future" + end + # Token to remove as reduction guard to solve S/R conflicts var removed_reduces = new Array[Token] for t, a in guarded_reduce do diff --git a/contrib/nitcc/tests/calc1.input b/contrib/nitcc/tests/calc1.input new file mode 100644 index 0000000000..9b5676cc94 --- /dev/null +++ b/contrib/nitcc/tests/calc1.input @@ -0,0 +1 @@ +1 + 2 - 3 - 4 diff --git a/contrib/nitcc/tests/calc1.sablecc b/contrib/nitcc/tests/calc1.sablecc index 6e61381370..1d7b4f96a5 100644 --- a/contrib/nitcc/tests/calc1.sablecc +++ b/contrib/nitcc/tests/calc1.sablecc @@ -6,6 +6,7 @@ Ignored #10, #32; e = e '+' a | e '-' a + | a ; a = | '(' e ')' diff --git a/contrib/nitcc/tests/gram_empty.input b/contrib/nitcc/tests/gram_empty.input new file mode 100644 index 0000000000..587be6b4c3 --- /dev/null +++ b/contrib/nitcc/tests/gram_empty.input @@ -0,0 +1 @@ +x diff --git a/contrib/nitcc/tests/gram_empty.sablecc b/contrib/nitcc/tests/gram_empty.sablecc new file mode 100644 index 0000000000..f3f55c8dbc --- /dev/null +++ b/contrib/nitcc/tests/gram_empty.sablecc @@ -0,0 +1,3 @@ +Parser +Ignored #10, #32; +s = s 'x' ; diff --git a/contrib/nitcc/tests/gram_empty2.input b/contrib/nitcc/tests/gram_empty2.input new file mode 100644 index 0000000000..c951c1f1fb --- /dev/null +++ b/contrib/nitcc/tests/gram_empty2.input @@ -0,0 +1 @@ +y x z diff --git a/contrib/nitcc/tests/gram_empty2.sablecc b/contrib/nitcc/tests/gram_empty2.sablecc new file mode 100644 index 0000000000..c55f48822b --- /dev/null +++ b/contrib/nitcc/tests/gram_empty2.sablecc @@ -0,0 +1,4 @@ +Parser +Ignored #10, #32; +s = a ; +a = 'x' s | 'y' s 'z'; diff --git a/contrib/nitcc/tests/sav/calc1.input.res b/contrib/nitcc/tests/sav/calc1.input.res new file mode 100644 index 0000000000..d48fb9cdeb --- /dev/null +++ b/contrib/nitcc/tests/sav/calc1.input.res @@ -0,0 +1,17 @@ +Start + e_1 + e_1 + e_0 + e_2 + a_2 + n@(1:1-1:2)='1' + '+'@(1:3-1:4) + a_2 + n@(1:5-1:6)='2' + '-'@(1:7-1:8) + a_2 + n@(1:9-1:10)='3' + '-'@(1:11-1:12) + a_2 + n@(1:13-1:14)='4' + Eof@(2:1-2:1)='' diff --git a/contrib/nitcc/tests/sav/calc1.res b/contrib/nitcc/tests/sav/calc1.res index 9841662c3f..e69de29bb2 100644 --- a/contrib/nitcc/tests/sav/calc1.res +++ b/contrib/nitcc/tests/sav/calc1.res @@ -1 +0,0 @@ -Runtime error: Aborted (./lrautomaton.nit:507) diff --git a/contrib/nitcc/tests/sav/gram_empty.input.res b/contrib/nitcc/tests/sav/gram_empty.input.res new file mode 100644 index 0000000000..36a16bc5df --- /dev/null +++ b/contrib/nitcc/tests/sav/gram_empty.input.res @@ -0,0 +1,3 @@ +NParserError@(1:1-1:2)='x' +Nodes[Node] + 'x'@(1:1-1:2) diff --git a/contrib/nitcc/tests/sav/gram_empty.res b/contrib/nitcc/tests/sav/gram_empty.res new file mode 100644 index 0000000000..d748cd06ae --- /dev/null +++ b/contrib/nitcc/tests/sav/gram_empty.res @@ -0,0 +1 @@ +Empty state 0 without any reduce or shift diff --git a/contrib/nitcc/tests/sav/gram_empty2.input.res b/contrib/nitcc/tests/sav/gram_empty2.input.res new file mode 100644 index 0000000000..50cd911b86 --- /dev/null +++ b/contrib/nitcc/tests/sav/gram_empty2.input.res @@ -0,0 +1,5 @@ +NParserError@(1:5-1:6)='z' +Nodes[Node] + 'y'@(1:1-1:2) + 'x'@(1:3-1:4) + 'z'@(1:5-1:6) From b8ab7e6da23acb216e560f1005bdf883302847b7 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Thu, 25 Jul 2024 17:49:03 -0400 Subject: [PATCH 2/2] nitcc: fix make check rule Signed-off-by: Jean Privat --- contrib/nitcc/src/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contrib/nitcc/src/Makefile b/contrib/nitcc/src/Makefile index f59c029b2a..8746ee6c69 100644 --- a/contrib/nitcc/src/Makefile +++ b/contrib/nitcc/src/Makefile @@ -18,9 +18,9 @@ nitcc: nitcc1 nitcc.nit nitcc.sablecc check: examples tests tests: - cd ../tests && ./run + $(MAKE) -C ../tests examples: - $(MAKE) -C ../example + $(MAKE) -C ../examples clean: rm -r \