Merge: Nitgs optims
[nit.git] / contrib / nitcc / src / grammar.nit
index 74784db..e6851f0 100644 (file)
@@ -27,7 +27,7 @@ class Gram
        # Dump of the concrete grammar and the transformations
        fun pretty: String
        do
-               var res = new Buffer
+               var res = new FlatBuffer
                for p in prods do
                        if p.spe != null then
                                res.append("{p.name} \{-> {p.spe.name}\}=\n")
@@ -42,8 +42,14 @@ class Gram
                                if a == last then res.append(" ;\n") else res.append(" |\n")
                        end
                        if p.is_nullable then res.append "\t// is nullable\n"
-                       if not p.firsts.is_empty then res.append "\t// firsts: {p.firsts.join(" ")}\n"
-                       if not p.afters.is_empty then res.append "\t// afters: {p.afters.join(" ")}\n"
+                       if not p.firsts.is_empty then
+                               res.append "\t// firsts:\n"
+                               for x in p.firsts do res.append "\t//   {x}\n"
+                       end
+                       if not p.afters.is_empty then
+                               res.append "\t// afters:\n"
+                               for x in p.afters do res.append "\t//   {x}\n"
+                       end
                end
                return res.to_s
        end
@@ -501,12 +507,20 @@ class LRAutomaton
                        res.add "s{s.number} {s.name}\n"
                        res.add "\tCORE\n"
                        for i in s.core do
-                               res.add "\t\t{s.item_to_s(i)}\n"
+                               res.add "\t\t{i}\n"
+                               if i.next != null then continue
+                               for i2 in s.lookahead(i) do
+                                       res.add "\t\t\t{i2}\n"
+                               end
                        end
                        res.add "\tOTHER ITEMS\n"
                        for i in s.items do
                                if s.core.has(i) then continue
-                               res.add "\t\t{s.item_to_s(i)}\n"
+                               res.add "\t\t{i}\n"
+                               if i.next != null then continue
+                               for i2 in s.lookahead(i) do
+                                       res.add "\t\t\t{i2}\n"
+                               end
                        end
                        res.add "\tTRANSITIONS {s.outs.length}\n"
                        for t in s.outs do
@@ -548,12 +562,12 @@ class LRAutomaton
                for s in states do
                        f.write "s{s.number} [label=\"{s.number} {s.name.escape_to_dot}|"
                        for i in s.core do
-                               f.write "{s.item_to_s(i).escape_to_dot}\\l"
+                               f.write "{i.to_s.escape_to_dot}\\l"
                        end
                        f.write("|")
                        for i in s.items do
                                if s.core.has(i) then continue
-                               f.write "{s.item_to_s(i).escape_to_dot}\\l"
+                               f.write "{i.to_s.escape_to_dot}\\l"
                        end
                        f.write "\""
                        if not s.is_lr0 then
@@ -916,13 +930,6 @@ class LRState
                return i.alt.prod.afters
        end
 
-       fun item_to_s(i: Item): String
-       do
-               var l = lookahead(i)
-               if l.is_empty then return i.to_s
-               return "{i} \{ {l.join(" ")} \}"
-       end
-
        # Set of all reductions
        var reduces = new ArraySet[Alternative]
        # Set of all shifts
@@ -981,31 +988,41 @@ class LRState
                                print "REDUCE/REDUCE Conflict on state {self.number} {self.name} for token {t}:"
                                for i in a do print "\treduce: {i}"
                        else if guarded_shift.has_key(t) then
-                               for ri in a.to_a do for si in guarded_shift[t] do
-                                       if lookahead(ri).has(si) then
-                                               var p = ri.alt.prod
-                                               var csi: nullable Item = null
-                                               for bsi in back_expand(si) do
-                                                       if bsi.alt.prod == p then
-                                                               csi = bsi
-                                                               break
-                                                       end
-                                               end
-                                               if csi == null then continue
-                                               print "Automatic Dangling on state {self.number} {self.name} for token {t}:"
-                                               print "\treduce: {ri}"
-                                               print "\tshift:  {si}"
-                                               if si != csi then
-                                                       print "\tcore shift: {csi}"
+                               var ri = a.first
+                               var confs = new Array[Item]
+                               var ress = new Array[String]
+                               var g = guarded_shift[t]
+                               for si in lookahead(ri) do
+                                       if si.next != t then continue
+                                       if not g.has(si) then
+                                               confs.add(si)
+                                               continue
+                                       end
+                                       var p = ri.alt.prod
+                                       var csi: nullable Item = null
+                                       for bsi in back_expand(si) do
+                                               if bsi.alt.prod == p then
+                                                       csi = bsi
+                                                       break
                                                end
-                                               a.remove(ri)
+                                       end
+                                       if csi == null then
+                                               confs.add(si)
+                                               continue
+                                       end
+                                       ress.add "\tshift:  {si}"
+                                       if si != csi then
+                                               ress.add "\tcore shift: {csi}"
                                        end
                                end
-                               if a.is_empty then
+                               if confs.is_empty then
+                                       print "Automatic Dangling on state {self.number} {self.name} for token {t}:"
+                                       print "\treduce: {ri}"
+                                       for r in ress do print r
                                        guarded_reduce.keys.remove(t)
                                else
                                        print "SHIFT/REDUCE Conflict on state {self.number} {self.name} for token {t}:"
-                                       for i in a do print "\treduce: {i}"
+                                       print "\treduce: {ri}"
                                        for i in guarded_shift[t] do print "\tshift:  {i}"
                                end
                        end
@@ -1055,7 +1072,7 @@ class Item
 
        redef fun to_s
        do
-               var b = new Buffer
+               var b = new FlatBuffer
                b.append("{alt.prod.name}::{alt.name}=")
                for i in [0..alt.elems.length[
                do