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 \ 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)