]> granicus.if.org Git - re2c/commitdiff
- Change to two pass generation which allows to avoid warnings about unused
authorhelly <helly@642ea486-5414-0410-9d7f-a0204ed87703>
Wed, 30 Mar 2005 23:23:06 +0000 (23:23 +0000)
committerhelly <helly@642ea486-5414-0410-9d7f-a0204ed87703>
Wed, 30 Mar 2005 23:23:06 +0000 (23:23 +0000)
  labels. We first generate virtually everything and store which labels are
  used and then generate the stuff while only writing used labels.

code.cc
globals.h
main.cc

diff --git a/code.cc b/code.cc
index 13198019c9dfc6b87577beae74223d0fae9724cf..26982b1437fe3b851cc387f29b64963d8e1a7f1f 100644 (file)
--- a/code.cc
+++ b/code.cc
@@ -260,6 +260,7 @@ void genGoTo(std::ostream &o, State *from, State *to, bool & readCh)
 
        o << "\tgoto yy" << to->label << ";\n";
        ++oline;
+       vUsedLabels.append(to->label);
 }
 
 void genIf(std::ostream &o, char *cmp, uint v, bool &readCh)
@@ -715,7 +716,10 @@ void Go::genGoto(std::ostream &o, State *from, State *next, bool &readCh)
 
 void State::emit(std::ostream &o, bool &readCh)
 {
-       o << "yy" << label << ":";
+       if (vUsedLabels.contains(label))
+       {
+               o << "yy" << label << ":";
+       }
        /*    o << "\nfprintf(stderr, \"<" << label << ">\");\n";*/
        action->emit(o, readCh);
 }
@@ -947,6 +951,38 @@ void DFA::split(State *s)
        s->go.span[0].to = move;
 }
 
+class null_stream: public std::ostream
+{
+public:
+       null_stream()
+               : std::ostream(&ns)
+       {
+       }
+
+       null_stream& put(char_type)
+       {
+               // nothing to do
+               return *this;
+       }
+       
+       null_stream& write(const char_type *, std::streamsize)
+       {
+               // nothing to do
+               return *this;
+       }
+
+protected:
+       class null_streambuf: public std::streambuf
+       {
+       public:
+               null_streambuf()
+                       : std::streambuf()
+               {
+               }       
+       };
+       null_streambuf   ns;
+};
+
 void DFA::emit(std::ostream &o)
 {
        static uint label = 0;
@@ -1114,6 +1150,7 @@ void DFA::emit(std::ostream &o)
 
        o << "\tgoto yy" << label << ";\n";
        ++oline;
+       vUsedLabels.append(label);
        (void) new Enter(head, label++);
 
        for (s = head; s; s = s->next)
@@ -1121,6 +1158,17 @@ void DFA::emit(std::ostream &o)
                s->label = label++;
        }
 
+       null_stream noWhere;
+
+       unsigned int nOrgOline = oline;
+       for (s = head; s; s = s->next)
+       {
+               bool readCh = false;
+               s->emit(noWhere, readCh);
+               s->go.genGoto(noWhere, s, s->next, readCh);
+       }
+       oline = nOrgOline;
+
        for (s = head; s; s = s->next)
        {
                bool readCh = false;
index 8a59104dd600952ef5f8a5d99768f70e7b0f7058..39f6f1db2d9454b66bc09d6541e4d53e56cf4955 100644 (file)
--- a/globals.h
+++ b/globals.h
@@ -3,6 +3,30 @@
 #define        _globals_h
 
 #include "basics.h"
+#include <list>
+#include <algorithm>
+
+template<typename _Ty>
+class label_list: protected std::list<_Ty>
+{
+public:
+       label_list()
+               : std::list<_Ty>()
+       {
+       }
+       
+       void append(const _Ty &val)
+       {
+               push_back(val);
+               sort();
+               unique();
+       }
+       
+       bool contains(const _Ty &val)
+       {
+               return std::find(begin(), end(), val) != end();
+       }
+};
 
 namespace re2c
 {
@@ -18,6 +42,8 @@ extern uchar ebc2asc[256];
 
 extern uchar *xlat, *talx;
 
+extern label_list<uint> vUsedLabels;
+
 } // end namespace re2c
 
 #endif
diff --git a/main.cc b/main.cc
index 78452626c96056647858443a52d6f7e98545a021..77c7747a9dea1f0b94991e2e49ea01c0f682b866 100644 (file)
--- a/main.cc
+++ b/main.cc
@@ -21,6 +21,7 @@ char *outputFileName = 0;
 bool sFlag = false;
 bool bFlag = false;
 unsigned int oline = 1;
+label_list<uint> vUsedLabels;
 
 using namespace std;