]> granicus.if.org Git - re2c/commitdiff
- Clean separation of steps
authorhelly <helly@642ea486-5414-0410-9d7f-a0204ed87703>
Mon, 17 Apr 2006 15:39:02 +0000 (15:39 +0000)
committerhelly <helly@642ea486-5414-0410-9d7f-a0204ed87703>
Mon, 17 Apr 2006 15:39:02 +0000 (15:39 +0000)
lessons/002_strip_comments/strip_002.s.c
lessons/002_strip_comments/strip_002.s.re
lessons/002_strip_comments/strip_003.b.c
lessons/002_strip_comments/strip_003.b.re

index be5cff400945d21f51fd1edb898fd517397b220c..ad14ef040e516c40a59c5ace3661e812b398402d 100755 (executable)
@@ -1,7 +1,7 @@
 /* Generated by re2c */
 #line 1 "strip_002.s.re"
 /* re2c lesson 002_strip_comments, strip_002.s, (c) M. Boerger 2006 */
-#line 34 "strip_002.s.re"
+#line 32 "strip_002.s.re"
 
 #include <stdlib.h>
 #include <stdio.h>
@@ -68,7 +68,6 @@ void echo(Scanner *s)
 int scan(FILE *fp)
 {
        int  res = 0;
-       int  nlcomment = 0;
     Scanner s;
 
        if (!fp)
@@ -84,7 +83,7 @@ int scan(FILE *fp)
        {
                s.tok = s.cur;
 
-#line 88 "<stdout>"
+#line 87 "<stdout>"
                {
                        YYCTYPE yych;
 
@@ -95,29 +94,29 @@ int scan(FILE *fp)
                        if((yych = *YYCURSOR) == '*') goto yy5;
                        if(yych == '/') goto yy7;
 yy3:
-#line 124 "strip_002.s.re"
+#line 121 "strip_002.s.re"
                        { fputc(*s.tok, stdout); continue; }
-#line 101 "<stdout>"
+#line 100 "<stdout>"
 yy4:
                        yych = *++YYCURSOR;
                        goto yy3;
 yy5:
                        ++YYCURSOR;
-#line 123 "strip_002.s.re"
+#line 120 "strip_002.s.re"
                        { goto comment; }
-#line 109 "<stdout>"
+#line 108 "<stdout>"
 yy7:
                        ++YYCURSOR;
-#line 122 "strip_002.s.re"
+#line 119 "strip_002.s.re"
                        { goto cppcomment; }
-#line 114 "<stdout>"
+#line 113 "<stdout>"
                }
-#line 125 "strip_002.s.re"
+#line 122 "strip_002.s.re"
 
 comment:
                s.tok = s.cur;
 
-#line 121 "<stdout>"
+#line 120 "<stdout>"
                {
                        YYCTYPE yych;
                        if((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
@@ -126,24 +125,24 @@ comment:
                        ++YYCURSOR;
                        if((yych = *YYCURSOR) == '/') goto yy14;
 yy12:
-#line 130 "strip_002.s.re"
+#line 127 "strip_002.s.re"
                        { goto comment; }
-#line 132 "<stdout>"
+#line 131 "<stdout>"
 yy13:
                        yych = *++YYCURSOR;
                        goto yy12;
 yy14:
                        ++YYCURSOR;
-#line 129 "strip_002.s.re"
+#line 126 "strip_002.s.re"
                        { goto commentws; }
-#line 140 "<stdout>"
+#line 139 "<stdout>"
                }
-#line 131 "strip_002.s.re"
+#line 128 "strip_002.s.re"
 
 commentws:
                s.tok = s.cur;
 
-#line 147 "<stdout>"
+#line 146 "<stdout>"
                {
                        YYCTYPE yych;
                        if((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
@@ -162,41 +161,34 @@ yy18:
                        ++YYCURSOR;
                        if((yych = *YYCURSOR) == 0x0A) goto yy25;
 yy19:
-#line 143 "strip_002.s.re"
+#line 133 "strip_002.s.re"
                        { goto commentws; }
-#line 168 "<stdout>"
+#line 167 "<stdout>"
 yy20:
                        ++YYCURSOR;
 yy21:
-#line 135 "strip_002.s.re"
-                       {
-                               if (!nlcomment)
-                               {
-                                       echo(&s);
-                               }
-                               nlcomment = 0;
-                               continue;
-                       }
-#line 181 "<stdout>"
+#line 132 "strip_002.s.re"
+                       { echo(&s); continue; }
+#line 173 "<stdout>"
 yy22:
                        yych = *++YYCURSOR;
                        goto yy19;
 yy23:
                        ++YYCURSOR;
-#line 144 "strip_002.s.re"
-                       { echo(&s); nlcomment = 0; continue; }
-#line 189 "<stdout>"
+#line 134 "strip_002.s.re"
+                       { echo(&s); continue; }
+#line 181 "<stdout>"
 yy25:
                        ++YYCURSOR;
                        yych = *YYCURSOR;
                        goto yy21;
                }
-#line 145 "strip_002.s.re"
+#line 135 "strip_002.s.re"
 
 cppcomment:
                s.tok = s.cur;
 
-#line 200 "<stdout>"
+#line 192 "<stdout>"
                {
                        YYCTYPE yych;
                        if((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
@@ -206,15 +198,15 @@ cppcomment:
                        ++YYCURSOR;
                        if((yych = *YYCURSOR) == 0x0A) goto yy33;
 yy29:
-#line 150 "strip_002.s.re"
+#line 140 "strip_002.s.re"
                        { goto cppcomment; }
-#line 212 "<stdout>"
+#line 204 "<stdout>"
 yy30:
                        ++YYCURSOR;
 yy31:
-#line 149 "strip_002.s.re"
+#line 139 "strip_002.s.re"
                        { echo(&s); continue; }
-#line 218 "<stdout>"
+#line 210 "<stdout>"
 yy32:
                        yych = *++YYCURSOR;
                        goto yy29;
@@ -223,7 +215,7 @@ yy33:
                        yych = *YYCURSOR;
                        goto yy31;
                }
-#line 151 "strip_002.s.re"
+#line 141 "strip_002.s.re"
 
        }
 
index b440a6b40b4f36b8f3be3939d9c86ddf762caf0c..21f11d0b32aa88f8bed3701f1282b4ee85fdeed3 100755 (executable)
@@ -4,6 +4,13 @@
 - complexity
   . When a comemnt is preceeded by a new line and followed by whitespace and a 
     new line then we can drop the trailing whitespace and new line.
+  . Additional to what we strip out already what about two consequtive comment 
+    blocks? When two comments are only separated by whitespace we want to drop 
+    both. In other words when detecting the end of a comment block we need to 
+    check whether it is followed by only whitespace and the a new comment in
+    which case we continure ignoring the input. If it is followed only by white
+    space and a new line we strip out the new white space and new line. In any
+    other case we start outputting all that follows.
     But we cannot simply use the following two rules:
          "*" "/" WS* "/" "*" { continue; }
          "*" "/" WS* NL      { continue; }
   . When shifting buffer contents as done in our fill function the marker needs
     to be corrected, too.
 
-- formatting
-  . Until now we only used single line expression code and we always had the 
-    opening { on the same line as the rule itself. If we have multiline rule
-    code and care for formatting we can nolonger rely on re2c. Now we have 
-    to indent the rule code ourself. Also we need to take care of the opening
-    {. If we keep it on the same line as the rule then re2c will indent it 
-    correctly and the emitted #line informations will be correct. If we place
-    it on the next line then the #line directivy will also point to that line
-    and not to the rule.
 */
 
 #include <stdlib.h>
@@ -97,7 +95,6 @@ void echo(Scanner *s)
 int scan(FILE *fp)
 {
        int  res = 0;
-       int  nlcomment = 0;
     Scanner s;
 
        if (!fp)
@@ -132,16 +129,9 @@ comment:
 commentws:
                s.tok = s.cur;
 /*!re2c
-       NL                      {
-                               if (!nlcomment)
-                               {
-                                       echo(&s);
-                               }
-                               nlcomment = 0;
-                               continue;
-                       }
+       NL                      { echo(&s); continue; }
        WS                      { goto commentws; }
-       ANY                     { echo(&s); nlcomment = 0; continue; }
+       ANY                     { echo(&s); continue; }
 */
 cppcomment:
                s.tok = s.cur;
index 21c7ff861d82b09302a8dfda7c527e4c4cd97306..eb166021a6d02f07436a5f77ba95356b3dabbaac 100755 (executable)
@@ -1,7 +1,7 @@
 /* Generated by re2c */
 #line 1 "strip_003.b.re"
 /* re2c lesson 002_strip_comments, strip_003.b, (c) M. Boerger 2006 */
-#line 29 "strip_003.b.re"
+#line 37 "strip_003.b.re"
 
 #include <stdlib.h>
 #include <stdio.h>
@@ -104,7 +104,7 @@ int scan(FILE *fp)
                                if((yych = *YYCURSOR) == '*') goto yy12;
                                if(yych == '/') goto yy14;
 yy3:
-#line 122 "strip_003.b.re"
+#line 130 "strip_003.b.re"
                                { fputc(*s.tok, stdout); continue; }
 #line 110 "<stdout>"
 yy4:
@@ -128,7 +128,7 @@ yy8:
 yy9:
                                ++YYCURSOR;
                                YYCURSOR = YYCTXMARKER;
-#line 120 "strip_003.b.re"
+#line 128 "strip_003.b.re"
                                { echo(&s); nlcomment = 1; continue; }
 #line 134 "<stdout>"
 yy11:
@@ -138,17 +138,17 @@ yy11:
                                goto yy8;
 yy12:
                                ++YYCURSOR;
-#line 121 "strip_003.b.re"
+#line 129 "strip_003.b.re"
                                { goto comment; }
 #line 144 "<stdout>"
 yy14:
                                ++YYCURSOR;
-#line 119 "strip_003.b.re"
+#line 127 "strip_003.b.re"
                                { goto cppcomment; }
 #line 149 "<stdout>"
                        }
                }
-#line 123 "strip_003.b.re"
+#line 131 "strip_003.b.re"
 
 comment:
                s.tok = s.cur;
@@ -163,7 +163,7 @@ comment:
                                ++YYCURSOR;
                                if((yych = *YYCURSOR) == '/') goto yy21;
 yy19:
-#line 128 "strip_003.b.re"
+#line 136 "strip_003.b.re"
                                { goto comment; }
 #line 169 "<stdout>"
 yy20:
@@ -171,12 +171,12 @@ yy20:
                                goto yy19;
 yy21:
                                ++YYCURSOR;
-#line 127 "strip_003.b.re"
+#line 135 "strip_003.b.re"
                                { goto commentws; }
 #line 177 "<stdout>"
                        }
                }
-#line 129 "strip_003.b.re"
+#line 137 "strip_003.b.re"
 
 commentws:
                s.tok = s.cur;
@@ -207,14 +207,14 @@ commentws:
                                ++YYCURSOR;
                                if((yych = *YYCURSOR) == 0x0A) goto yy37;
 yy26:
-#line 142 "strip_003.b.re"
+#line 150 "strip_003.b.re"
                                { goto commentws; }
 #line 213 "<stdout>"
 yy27:
                                yych = *(YYMARKER = ++YYCURSOR);
                                if(yych == '/') goto yy35;
 yy28:
-#line 134 "strip_003.b.re"
+#line 142 "strip_003.b.re"
                                {
                                        if (!nlcomment)
                                        {
@@ -228,7 +228,7 @@ yy29:
                                ++YYCURSOR;
                                if((yych = *YYCURSOR) == '*') goto yy33;
 yy30:
-#line 143 "strip_003.b.re"
+#line 151 "strip_003.b.re"
                                { echo(&s); nlcomment = 0; continue; }
 #line 234 "<stdout>"
 yy31:
@@ -239,7 +239,7 @@ yy32:
                                goto yy30;
 yy33:
                                ++YYCURSOR;
-#line 133 "strip_003.b.re"
+#line 141 "strip_003.b.re"
                                { goto comment; }
 #line 245 "<stdout>"
 yy35:
@@ -253,7 +253,7 @@ yy37:
                                goto yy28;
                        }
                }
-#line 144 "strip_003.b.re"
+#line 152 "strip_003.b.re"
 
 cppcomment:
                s.tok = s.cur;
@@ -269,13 +269,13 @@ cppcomment:
                                ++YYCURSOR;
                                if((yych = *YYCURSOR) == 0x0A) goto yy45;
 yy41:
-#line 149 "strip_003.b.re"
+#line 157 "strip_003.b.re"
                                { goto cppcomment; }
 #line 275 "<stdout>"
 yy42:
                                ++YYCURSOR;
 yy43:
-#line 148 "strip_003.b.re"
+#line 156 "strip_003.b.re"
                                { echo(&s); continue; }
 #line 281 "<stdout>"
 yy44:
@@ -287,7 +287,7 @@ yy45:
                                goto yy43;
                        }
                }
-#line 150 "strip_003.b.re"
+#line 158 "strip_003.b.re"
 
        }
 
index 7b1f8748d2eaf585f1535ffb61f9df476ae820ca..065d31ab43b4de2e371d2edd58ed28614d97b691 100755 (executable)
@@ -2,14 +2,10 @@
 /*!ignore:re2c
 
 - more complexity
-  . Additional to what we strip out already what about two consequtive comment 
-    blocks? When two comments are only separated by whitespace we want to drop 
-    both. In other words when detecting the end of a comment block we need to 
-    check whether it is followed by only whitespace and the a new comment in
-    which case we continure ignoring the input. If it is followed only by white
-    space and a new line we strip out the new white space and new line. In any
-    other case we start outputting all that follows.
-  . The solution to the above is to use trailing contexts.
+  . Right now we strip out trailing white space and new lines after a comment
+    block. This can be a problem when the comment block was not preceeded by 
+    a new line.
+  . The solution is to use trailing contexts.
 
 -  trailing contexts
   . Re2c allows to check for a portion of input and only recognize it when it 
     a pointer variable needs to be provided.
   . As with YYMARKER the corrsponding variable needs to be corrected if we 
     shift in some buffer.
-
+  . Still this is not all we need to solve the problem. What is left is that
+    the information whether we detected a trailing context was detected has to 
+    be stored somewhere. This is done by the new variable nlcomment.
+
+- formatting
+  . Until now we only used single line expression code and we always had the 
+    opening { on the same line as the rule itself. If we have multiline rule
+    code and care for formatting we can nolonger rely on re2c. Now we have 
+    to indent the rule code ourself. Also we need to take care of the opening
+    {. If we keep it on the same line as the rule then re2c will indent it 
+    correctly and the emitted #line informations will be correct. If we place
+    it on the next line then the #line directivy will also point to that line
+    and not to the rule.
 */
 
 #include <stdlib.h>