]> granicus.if.org Git - re2c/commitdiff
- Update lesson 1
authorhelly <helly@642ea486-5414-0410-9d7f-a0204ed87703>
Sun, 16 Apr 2006 11:22:45 +0000 (11:22 +0000)
committerhelly <helly@642ea486-5414-0410-9d7f-a0204ed87703>
Sun, 16 Apr 2006 11:22:45 +0000 (11:22 +0000)
lessons/001_upn_calculator/calc_001.re
lessons/001_upn_calculator/calc_006.s.c
lessons/001_upn_calculator/calc_006.s.re
lessons/001_upn_calculator/calc_007.b.c
lessons/001_upn_calculator/calc_007.b.re

index 0e5e046197a58140ef494948b464623b255a7e68..911f89bf8cb2cf4e225a342e9e17614141daae8d 100755 (executable)
@@ -38,7 +38,7 @@
     Another way would be to only use two rules:
     "0" [0-9]+
     "0" | ( [1-9] [0-9]* )
-
+    A full description of re2c rule syntax can be found in the manual.
 */
 
 #include <stdlib.h>
index 720c4cefbad43cfda9842c20854c106cbab5bd31..f383e3e2f3f029fe6404e485c3c083e19d4a2cb9 100755 (executable)
@@ -37,6 +37,7 @@ int stack_add()
        
        --depth;
        stack[depth-1] = stack[depth-1] + stack[depth];
+       DEBUG(printf("+\n"));
        return 0;
 }
 
@@ -46,10 +47,11 @@ int stack_sub()
 
        --depth;
        stack[depth-1] = stack[depth-1] - stack[depth];
+       DEBUG(printf("-\n"));
        return 0;
 }
 
-int scan(char *s, int l)
+int scan(char *s)
 {
        char *p = s;
        char *t;
@@ -58,8 +60,6 @@ int scan(char *s, int l)
        #define YYCTYPE         char
        #define YYCURSOR        p
        
-    if (l > 1 && s[l-2] == '0' && s[l-1] >= '0' && s[l-1] <= '9') return 2;
-
        while(!res)
        {
                t = p;
@@ -92,7 +92,7 @@ int scan(char *s, int l)
                        yych = *YYCURSOR;
                        goto yy21;
 yy3:
-#line 114 "calc_006.s.re"
+#line 105 "calc_006.s.re"
                        { continue; }
 #line 98 "<stdout>"
 yy4:
@@ -100,7 +100,7 @@ yy4:
                        if((yych = *YYCURSOR) <= '/') goto yy5;
                        if(yych <= '9') goto yy17;
 yy5:
-#line 116 "calc_006.s.re"
+#line 107 "calc_006.s.re"
                        { res = push_num(t, p, 10); continue; }
 #line 106 "<stdout>"
 yy6:
@@ -108,22 +108,22 @@ yy6:
                        goto yy16;
 yy7:
                        ++YYCURSOR;
-#line 117 "calc_006.s.re"
+#line 108 "calc_006.s.re"
                        { res = stack_add();            continue; }
 #line 114 "<stdout>"
 yy9:
                        ++YYCURSOR;
-#line 118 "calc_006.s.re"
+#line 109 "calc_006.s.re"
                        { res = stack_sub();            continue; }
 #line 119 "<stdout>"
 yy11:
                        ++YYCURSOR;
-#line 119 "calc_006.s.re"
+#line 110 "calc_006.s.re"
                        { res = depth == 1 ? 0 : 2;     continue; }
 #line 124 "<stdout>"
 yy13:
                        ++YYCURSOR;
-#line 120 "calc_006.s.re"
+#line 111 "calc_006.s.re"
                        { res = 1;                                      continue; }
 #line 129 "<stdout>"
 yy15:
@@ -139,7 +139,7 @@ yy17:
                        if(yych <= '/') goto yy19;
                        if(yych <= '9') goto yy17;
 yy19:
-#line 115 "calc_006.s.re"
+#line 106 "calc_006.s.re"
                        { res = push_num(t, p, 8);      continue; }
 #line 145 "<stdout>"
 yy20:
@@ -150,7 +150,7 @@ yy21:
                        if(yych == ' ') goto yy20;
                        goto yy3;
                }
-#line 121 "calc_006.s.re"
+#line 112 "calc_006.s.re"
 
        }
        return res;
@@ -172,7 +172,7 @@ int main(int argc, char **argv)
                                ++inp;
                                len -=2;
                        }
-                       res = scan(inp, len);
+                       res = scan(inp);
                }
                switch(res)
                {
index d7947ccf8e6da0ec23d5a41fa19b46acb00ff115..830af3dd2dce8b75f0cd03af40e2e46aada0a48f 100755 (executable)
     In the example suppose "0" is passed. The scanner reads the first "0" and
     then is in an undecided state. The scanner can earliest decide on the next 
     char what  the token is. In case of a zero the input ends and it was a 
-    number, 0 to be precise. In case of a didit it is and the next character
-    needs to be read. In case of any other character the scanner has found an 
-    error with the any rule [^]. 
+    number, 0 to be precise. In case of a digit it is an octal number and the 
+    next character needs to be read. In case of any other character the scanner
+    will detect an error with the any rule [^]. 
     
-    Now the above shows that re2c may read two characters directly. But only if 
-    the first is a "0". So we could easily check that if the first char is "0"
-    another charcter is present.
+    Now the above shows that the scanner may read two characters directly. But 
+    only if the first is a "0". So we could easily check that if the first char
+    is "0" and the next char is a digit then yet another charcter is present.
+    But we require our inut to be zero terminated. And that means we do not 
+    have to check anything for this scanner.
     
-    if (p[0] == '0' && p[1] == '\0') return 2;
-    
-    But instead of doing so in every loop we can optimize by taking into 
-    account what we know from our input analysis. That is a problem can only
-    arise when the second last character is a "0" and the last is any digit.
-    
-    if (l > 1 && s[l-2] == '0' && s[l-1] >= '0' && s[l-1] <= '9') return 2;
-    
-    However in this example the above check is only necessary if the input is 
-    not terminated by a trailing zero check. In other words it would be used 
-    when reading from a file and directly working on the read buffers. For 
-    zero terminated string input the generated scanner will always find the 
-    terminating zero.
+    However with other rule sets re2c might read more then one character in a 
+    row. In those cases it is normally hard to impossible to avoid YYFILL.
 
 - optimizing the generated code by using -s command line switch of re2c
   . This tells re2c to generate code that uses if statements rather 
@@ -76,6 +67,7 @@ int stack_add()
        
        --depth;
        stack[depth-1] = stack[depth-1] + stack[depth];
+       DEBUG(printf("+\n"));
        return 0;
 }
 
@@ -85,10 +77,11 @@ int stack_sub()
 
        --depth;
        stack[depth-1] = stack[depth-1] - stack[depth];
+       DEBUG(printf("-\n"));
        return 0;
 }
 
-int scan(char *s, int l)
+int scan(char *s)
 {
        char *p = s;
        char *t;
@@ -97,8 +90,6 @@ int scan(char *s, int l)
        #define YYCTYPE         char
        #define YYCURSOR        p
        
-    if (l > 1 && s[l-2] == '0' && s[l-1] >= '0' && s[l-1] <= '9') return 2;
-
        while(!res)
        {
                t = p;
@@ -139,7 +130,7 @@ int main(int argc, char **argv)
                                ++inp;
                                len -=2;
                        }
-                       res = scan(inp, len);
+                       res = scan(inp);
                }
                switch(res)
                {
index c3bd70f7e725bcb4eb976d4f7c3c9d85dddaef19..3e47b8601d45c03b0a3d20db0d72c05960391a5d 100755 (executable)
@@ -1,6 +1,6 @@
 /* Generated by re2c */
 #line 1 "calc_007.b.re"
-/* re2c lesson_001, calc_006, (c) M. Boerger 2006 */
+/* re2c lesson_001, calc_007, (c) M. Boerger 2006 */
 
 #include <stdlib.h>
 #include <stdio.h>
@@ -37,6 +37,7 @@ int stack_add()
        
        --depth;
        stack[depth-1] = stack[depth-1] + stack[depth];
+       DEBUG(printf("+\n"));
        return 0;
 }
 
@@ -46,10 +47,11 @@ int stack_sub()
 
        --depth;
        stack[depth-1] = stack[depth-1] - stack[depth];
+       DEBUG(printf("+\n"));
        return 0;
 }
 
-int scan(char *s, int l)
+int scan(char *s)
 {
        char *p = s;
        char *t;
@@ -58,8 +60,6 @@ int scan(char *s, int l)
        #define YYCTYPE         char
        #define YYCURSOR        p
        
-    if (l > 1 && s[l-2] == '0' && s[l-1] >= '0' && s[l-1] <= '9') return 2;
-
        while(!res)
        {
                t = p;
@@ -210,7 +210,7 @@ int main(int argc, char **argv)
                                ++inp;
                                len -=2;
                        }
-                       res = scan(inp, len);
+                       res = scan(inp);
                }
                switch(res)
                {
index aabd7e9f15e34dfa7a3a266807f08e859d52aec3..469ae5e13666a348e432fae8c6c19891a67b9d6f 100755 (executable)
@@ -1,4 +1,4 @@
-/* re2c lesson_001, calc_006, (c) M. Boerger 2006 */
+/* re2c lesson_001, calc_007, (c) M. Boerger 2006 */
 /*!ignore:re2c
 
 - optimizing the generated code by using -b command line switch of re2c
@@ -41,6 +41,7 @@ int stack_add()
        
        --depth;
        stack[depth-1] = stack[depth-1] + stack[depth];
+       DEBUG(printf("+\n"));
        return 0;
 }
 
@@ -50,10 +51,11 @@ int stack_sub()
 
        --depth;
        stack[depth-1] = stack[depth-1] - stack[depth];
+       DEBUG(printf("+\n"));
        return 0;
 }
 
-int scan(char *s, int l)
+int scan(char *s)
 {
        char *p = s;
        char *t;
@@ -62,8 +64,6 @@ int scan(char *s, int l)
        #define YYCTYPE         char
        #define YYCURSOR        p
        
-    if (l > 1 && s[l-2] == '0' && s[l-1] >= '0' && s[l-1] <= '9') return 2;
-
        while(!res)
        {
                t = p;
@@ -104,7 +104,7 @@ int main(int argc, char **argv)
                                ++inp;
                                len -=2;
                        }
-                       res = scan(inp, len);
+                       res = scan(inp);
                }
                switch(res)
                {