]> granicus.if.org Git - re2c/commitdiff
- Fix lessons to use unsgned char for -s/b
authorhelly <helly@642ea486-5414-0410-9d7f-a0204ed87703>
Tue, 20 Feb 2007 17:21:49 +0000 (17:21 +0000)
committerhelly <helly@642ea486-5414-0410-9d7f-a0204ed87703>
Tue, 20 Feb 2007 17:21:49 +0000 (17:21 +0000)
12 files changed:
re2c/lessons/001_upn_calculator/calc_006.s.c
re2c/lessons/001_upn_calculator/calc_006.s.re
re2c/lessons/001_upn_calculator/calc_007.b.c
re2c/lessons/001_upn_calculator/calc_007.b.re
re2c/lessons/001_upn_calculator/windows/main.b.c
re2c/lessons/001_upn_calculator/windows/main.b.re
re2c/lessons/002_strip_comments/strip_001.s.c
re2c/lessons/002_strip_comments/strip_001.s.re
re2c/lessons/002_strip_comments/strip_002.s.c
re2c/lessons/002_strip_comments/strip_002.s.re
re2c/lessons/002_strip_comments/strip_003.b.c
re2c/lessons/002_strip_comments/strip_003.b.re

index 4c470e09d20fe7692a9c47eba52eb5e07ea87d0f..779198350d50f7b0f090b1d8e090068d8400e5f8 100755 (executable)
@@ -1,7 +1,7 @@
 /* Generated by re2c */
 #line 1 "calc_006.s.re"
 /* re2c lesson 001_upn_calculator, calc_006, (c) M. Boerger 2006 */
-#line 34 "calc_006.s.re"
+#line 36 "calc_006.s.re"
 
 #include <stdlib.h>
 #include <stdio.h>
@@ -12,7 +12,7 @@
 int  stack[4];
 int  depth = 0;
 
-int push_num(const char *t, const char *l, int radix)
+int push_num(const unsigned char *t, const unsigned char *l, int radix)
 {
        int num = 0;
        
@@ -24,7 +24,7 @@ int push_num(const char *t, const char *l, int radix)
        --t;
        while(++t < l)
        {
-               num = num * radix + (*t - '0');
+               num = num * radix + (*t - (unsigned char)'0');
        }
        DEBUG(printf("Num: %d\n", num));
 
@@ -54,11 +54,11 @@ int stack_sub()
 
 int scan(char *s)
 {
-       char *p = s;
-       char *t;
+       unsigned char *p = (unsigned char*)s;
+       unsigned char *t;
        int res = 0;
        
-#define YYCTYPE         char
+#define YYCTYPE         unsigned char
 #define YYCURSOR        p
        
        while(!res)
@@ -93,7 +93,7 @@ int scan(char *s)
                        yych = *YYCURSOR;
                        goto yy21;
 yy3:
-#line 105 "calc_006.s.re"
+#line 107 "calc_006.s.re"
                        { continue; }
 #line 99 "<stdout>"
 yy4:
@@ -101,7 +101,7 @@ yy4:
                        if((yych = *YYCURSOR) <= '/') goto yy5;
                        if(yych <= '9') goto yy17;
 yy5:
-#line 107 "calc_006.s.re"
+#line 109 "calc_006.s.re"
                        { res = push_num(t, p, 10); continue; }
 #line 107 "<stdout>"
 yy6:
@@ -109,22 +109,22 @@ yy6:
                        goto yy16;
 yy7:
                        ++YYCURSOR;
-#line 108 "calc_006.s.re"
+#line 110 "calc_006.s.re"
                        { res = stack_add();            continue; }
 #line 115 "<stdout>"
 yy9:
                        ++YYCURSOR;
-#line 109 "calc_006.s.re"
+#line 111 "calc_006.s.re"
                        { res = stack_sub();            continue; }
 #line 120 "<stdout>"
 yy11:
                        ++YYCURSOR;
-#line 110 "calc_006.s.re"
-                       { res = depth == 1 ? 0 : 2;     continue; }
+#line 112 "calc_006.s.re"
+                       { res = depth == 1 ? 0 : 2;     break; }
 #line 125 "<stdout>"
 yy13:
                        ++YYCURSOR;
-#line 111 "calc_006.s.re"
+#line 113 "calc_006.s.re"
                        { res = 1;                                      continue; }
 #line 130 "<stdout>"
 yy15:
@@ -140,7 +140,7 @@ yy17:
                        if(yych <= '/') goto yy19;
                        if(yych <= '9') goto yy17;
 yy19:
-#line 106 "calc_006.s.re"
+#line 108 "calc_006.s.re"
                        { res = push_num(t, p, 8);      continue; }
 #line 146 "<stdout>"
 yy20:
@@ -151,7 +151,7 @@ yy21:
                        if(yych == ' ') goto yy20;
                        goto yy3;
                }
-#line 112 "calc_006.s.re"
+#line 114 "calc_006.s.re"
 
        }
        return res;
@@ -166,14 +166,15 @@ int main(int argc, char **argv)
                
                while(!res && ++argp < argc)
                {
-                       inp = argv[argp];
+                       inp = strdup(argv[argp]);
                        len = strlen(inp);
                        if (inp[0] == '\"' && inp[len-1] == '\"')
                        {
+                               inp[len - 1] = '\0';
                                ++inp;
-                               len -=2;
                        }
                        res = scan(inp);
+                       free(inp);
                }
                switch(res)
                {
index 6790bee495010bfd885141e3372fdae99e14002f..d73b6b94f507931731dc3200ccc6c1139062ab3e 100755 (executable)
@@ -29,7 +29,9 @@
 
 - optimizing the generated code by using -s command line switch of re2c
   . This tells re2c to generate code that uses if statements rather 
-    then endless switch/case expressions where appropriate. 
+    then endless switch/case expressions where appropriate. Note that the 
+    generated code now requires the input to be unsigned char rather than char
+    due to the way comparisons are generated.
 */
 
 #include <stdlib.h>
@@ -41,7 +43,7 @@
 int  stack[4];
 int  depth = 0;
 
-int push_num(const char *t, const char *l, int radix)
+int push_num(const unsigned char *t, const unsigned char *l, int radix)
 {
        int num = 0;
        
@@ -53,7 +55,7 @@ int push_num(const char *t, const char *l, int radix)
        --t;
        while(++t < l)
        {
-               num = num * radix + (*t - '0');
+               num = num * radix + (*t - (unsigned char)'0');
        }
        DEBUG(printf("Num: %d\n", num));
 
@@ -83,11 +85,11 @@ int stack_sub()
 
 int scan(char *s)
 {
-       char *p = s;
-       char *t;
+       unsigned char *p = (unsigned char*)s;
+       unsigned char *t;
        int res = 0;
        
-#define YYCTYPE         char
+#define YYCTYPE         unsigned char
 #define YYCURSOR        p
        
        while(!res)
@@ -107,7 +109,7 @@ int scan(char *s)
        INT             { res = push_num(t, p, 10); continue; }
        "+"             { res = stack_add();            continue; }
        "-"             { res = stack_sub();            continue; }
-       "\000"  { res = depth == 1 ? 0 : 2;     continue; }
+       "\000"  { res = depth == 1 ? 0 : 2;     break; }
        [^]             { res = 1;                                      continue; }
 */
        }
@@ -123,14 +125,15 @@ int main(int argc, char **argv)
                
                while(!res && ++argp < argc)
                {
-                       inp = argv[argp];
+                       inp = strdup(argv[argp]);
                        len = strlen(inp);
                        if (inp[0] == '\"' && inp[len-1] == '\"')
                        {
+                               inp[len - 1] = '\0';
                                ++inp;
-                               len -=2;
                        }
                        res = scan(inp);
+                       free(inp);
                }
                switch(res)
                {
index b2b269a7dea5155f774c4e079ddb8e9d2dc004bd..f7bccc0cf5c014b3d86f3d170f3ec8a244e4eb5f 100755 (executable)
@@ -1,7 +1,7 @@
 /* Generated by re2c */
 #line 1 "calc_007.b.re"
 /* re2c lesson 001_upn_calculator, calc_007, (c) M. Boerger 2006 */
-#line 8 "calc_007.b.re"
+#line 9 "calc_007.b.re"
 
 #include <stdlib.h>
 #include <stdio.h>
@@ -12,7 +12,7 @@
 int  stack[4];
 int  depth = 0;
 
-int push_num(const char *t, const char *l, int radix)
+int push_num(const unsigned char *t, const unsigned char *l, int radix)
 {
        int num = 0;
        
@@ -24,7 +24,7 @@ int push_num(const char *t, const char *l, int radix)
        --t;
        while(++t < l)
        {
-               num = num * radix + (*t - '0');
+               num = num * radix + (*t - (unsigned char)'0');
        }
        DEBUG(printf("Num: %d\n", num));
 
@@ -54,11 +54,11 @@ int stack_sub()
 
 int scan(char *s)
 {
-       char *p = s;
-       char *t;
+       unsigned char *p = (unsigned char*)s;
+       unsigned char *t;
        int res = 0;
        
-#define YYCTYPE         char
+#define YYCTYPE         unsigned char
 #define YYCURSOR        p
        
        while(!res)
@@ -128,7 +128,7 @@ int scan(char *s)
                                yych = *YYCURSOR;
                                goto yy21;
 yy3:
-#line 79 "calc_007.b.re"
+#line 80 "calc_007.b.re"
                                { continue; }
 #line 134 "<stdout>"
 yy4:
@@ -136,7 +136,7 @@ yy4:
                                if((yych = *YYCURSOR) <= '/') goto yy5;
                                if(yych <= '9') goto yy17;
 yy5:
-#line 81 "calc_007.b.re"
+#line 82 "calc_007.b.re"
                                { res = push_num(t, p, 10); continue; }
 #line 142 "<stdout>"
 yy6:
@@ -144,22 +144,22 @@ yy6:
                                goto yy16;
 yy7:
                                ++YYCURSOR;
-#line 82 "calc_007.b.re"
+#line 83 "calc_007.b.re"
                                { res = stack_add();            continue; }
 #line 150 "<stdout>"
 yy9:
                                ++YYCURSOR;
-#line 83 "calc_007.b.re"
+#line 84 "calc_007.b.re"
                                { res = stack_sub();            continue; }
 #line 155 "<stdout>"
 yy11:
                                ++YYCURSOR;
-#line 84 "calc_007.b.re"
-                               { res = depth == 1 ? 0 : 2;     continue; }
+#line 85 "calc_007.b.re"
+                               { res = depth == 1 ? 0 : 2;     break; }
 #line 160 "<stdout>"
 yy13:
                                ++YYCURSOR;
-#line 85 "calc_007.b.re"
+#line 86 "calc_007.b.re"
                                { res = 1;                                      continue; }
 #line 165 "<stdout>"
 yy15:
@@ -176,7 +176,7 @@ yy17:
                                if(yych <= '/') goto yy19;
                                if(yych <= '9') goto yy17;
 yy19:
-#line 80 "calc_007.b.re"
+#line 81 "calc_007.b.re"
                                { res = push_num(t, p, 8);      continue; }
 #line 182 "<stdout>"
 yy20:
@@ -189,7 +189,7 @@ yy21:
                                goto yy3;
                        }
                }
-#line 86 "calc_007.b.re"
+#line 87 "calc_007.b.re"
 
        }
        return res;
@@ -204,14 +204,15 @@ int main(int argc, char **argv)
                
                while(!res && ++argp < argc)
                {
-                       inp = argv[argp];
+                       inp = strdup(argv[argp]);
                        len = strlen(inp);
                        if (inp[0] == '\"' && inp[len-1] == '\"')
                        {
+                               inp[len - 1] = '\0';
                                ++inp;
-                               len -=2;
                        }
                        res = scan(inp);
+                       free(inp);
                }
                switch(res)
                {
index 3ebc919943ed33505409a7faaa010f59597c4db7..9d268fdf4cbec05968b3b71085b9d75f6aaff789 100755 (executable)
@@ -3,7 +3,8 @@
 
 - optimizing the generated code by using -b command line switch of re2c
   . This tells re2c to generate code that uses a decision table. The -b switch
-    also contains the -s behavior. 
+    also contains the -s behavior. And -b also requires the input to be 
+    unsigned chars.
 */
 
 #include <stdlib.h>
@@ -15,7 +16,7 @@
 int  stack[4];
 int  depth = 0;
 
-int push_num(const char *t, const char *l, int radix)
+int push_num(const unsigned char *t, const unsigned char *l, int radix)
 {
        int num = 0;
        
@@ -27,7 +28,7 @@ int push_num(const char *t, const char *l, int radix)
        --t;
        while(++t < l)
        {
-               num = num * radix + (*t - '0');
+               num = num * radix + (*t - (unsigned char)'0');
        }
        DEBUG(printf("Num: %d\n", num));
 
@@ -57,11 +58,11 @@ int stack_sub()
 
 int scan(char *s)
 {
-       char *p = s;
-       char *t;
+       unsigned char *p = (unsigned char*)s;
+       unsigned char *t;
        int res = 0;
        
-#define YYCTYPE         char
+#define YYCTYPE         unsigned char
 #define YYCURSOR        p
        
        while(!res)
@@ -81,7 +82,7 @@ int scan(char *s)
        INT             { res = push_num(t, p, 10); continue; }
        "+"             { res = stack_add();            continue; }
        "-"             { res = stack_sub();            continue; }
-       "\000"  { res = depth == 1 ? 0 : 2;     continue; }
+       "\000"  { res = depth == 1 ? 0 : 2;     break; }
        [^]             { res = 1;                                      continue; }
 */
        }
@@ -97,14 +98,15 @@ int main(int argc, char **argv)
                
                while(!res && ++argp < argc)
                {
-                       inp = argv[argp];
+                       inp = strdup(argv[argp]);
                        len = strlen(inp);
                        if (inp[0] == '\"' && inp[len-1] == '\"')
                        {
+                               inp[len - 1] = '\0';
                                ++inp;
-                               len -=2;
                        }
                        res = scan(inp);
+                       free(inp);
                }
                switch(res)
                {
index 24f3ec3178e72403eb2ab6b00b2753c89fc1642a..d544bee0436bcaadadeff86c48caa5e5339d4baf 100755 (executable)
@@ -40,9 +40,9 @@ void InitHiResTimerAndVerifyWorking(void)
  */
 int ScanFullSpeed(char *pzStrToScan, size_t lenStrToScan)
 {
-       char *pzCurScanPos = pzStrToScan;
-       char *pzBacktrackInfo = 0;
-#define YYCTYPE         char
+       unsigned char *pzCurScanPos = (unsigned char*)pzStrToScan;
+       unsigned char *pzBacktrackInfo = 0;
+#define YYCTYPE         unsigned char
 #define YYCURSOR        pzCurScanPos
 #define YYLIMIT         (pzStrToScan+lenStrToScan)
 #define YYMARKER        pzBacktrackInfo
@@ -169,9 +169,9 @@ yy17:
  */
 int scan(char *pzStrToScan, size_t lenStrToScan)
 {
-       char *pzCurScanPos = pzStrToScan;
-       char *pzBacktrackInfo = 0;
-#define YYCTYPE         char
+       unsigned char *pzCurScanPos = (unsigned char*)pzStrToScan;
+       unsigned char *pzBacktrackInfo = 0;
+#define YYCTYPE         unsigned char
 #define YYCURSOR        pzCurScanPos
 #define YYLIMIT         (pzStrToScan+lenStrToScan)
 #define YYMARKER        pzBacktrackInfo
index 3e2abf80ae4af9cc2ae50ab9b5bc065423b4cbf6..1600b83d228e826f3cd076f6d51b58b216fd846c 100755 (executable)
@@ -78,9 +78,9 @@ void InitHiResTimerAndVerifyWorking(void)
  */
 int ScanFullSpeed(char *pzStrToScan, size_t lenStrToScan)
 {
-       char *pzCurScanPos = pzStrToScan;
-       char *pzBacktrackInfo = 0;
-#define YYCTYPE         char
+       unsigned char *pzCurScanPos = (unsigned char*)pzStrToScan;
+       unsigned char *pzBacktrackInfo = 0;
+#define YYCTYPE         unsigned char
 #define YYCURSOR        pzCurScanPos
 #define YYLIMIT         (pzStrToScan+lenStrToScan)
 #define YYMARKER        pzBacktrackInfo
@@ -105,9 +105,9 @@ int ScanFullSpeed(char *pzStrToScan, size_t lenStrToScan)
  */
 int scan(char *pzStrToScan, size_t lenStrToScan)
 {
-       char *pzCurScanPos = pzStrToScan;
-       char *pzBacktrackInfo = 0;
-#define YYCTYPE         char
+       unsigned char *pzCurScanPos = (unsigned char*)pzStrToScan;
+       unsigned char *pzBacktrackInfo = 0;
+#define YYCTYPE         unsigned char
 #define YYCURSOR        pzCurScanPos
 #define YYLIMIT         (pzStrToScan+lenStrToScan)
 #define YYMARKER        pzBacktrackInfo
index c4ff92748c9bcfafc9a17dde3af341f7f26b9bb1..540c792ad5c75e05989837172c44ac8369e7a449 100755 (executable)
 # error BSIZE must be greater YYMAXFILL
 #endif
 
-#define        YYCTYPE         char
+#define        YYCTYPE         unsigned char
 #define        YYCURSOR        s.cur
 #define        YYLIMIT         s.lim
 #define        YYFILL(n)       { if ((res = fill(&s, n)) >= 0) break; }
 
 typedef struct Scanner
 {
-       FILE            *fp;
-       char            *cur, *tok, *lim, *eof;
-       char            buffer[BSIZE];
+       FILE                    *fp;
+       unsigned char   *cur, *tok, *lim, *eof;
+       unsigned char   buffer[BSIZE];
 } Scanner;
 
 int fill(Scanner *s, int len)
 {
        if (!len)
        {
-               s->tok = s->cur = buf = s->lim = s->buffer
+               s->cur = s->tok = s->lim = s->buffer;
                s->eof = 0;
        }
        if (!s->eof)
index 5466fcea46fb69bae228d36b9ec2ba45a0a96193..05185be23f8cc1ae14cfbe22beb01b58f0681bcc 100755 (executable)
 # error BSIZE must be greater YYMAXFILL
 #endif
 
-#define        YYCTYPE         char
+#define        YYCTYPE         unsigned char
 #define        YYCURSOR        s.cur
 #define        YYLIMIT         s.lim
 #define        YYFILL(n)       { if ((res = fill(&s, n)) >= 0) break; }
 
 typedef struct Scanner
 {
-       FILE            *fp;
-       char            *cur, *tok, *lim, *eof;
-       char            buffer[BSIZE];
+       FILE                    *fp;
+       unsigned char   *cur, *tok, *lim, *eof;
+       unsigned char   buffer[BSIZE];
 } Scanner;
 
 int fill(Scanner *s, int len)
 {
        if (!len)
        {
-               s->tok = s->cur = buf = s->lim = s->buffer
+               s->cur = s->tok = s->lim = s->buffer;
                s->eof = 0;
        }
        if (!s->eof)
index ad14ef040e516c40a59c5ace3661e812b398402d..0d76453022cd599f290dadfc5667a425f957e23f 100755 (executable)
@@ -14,7 +14,7 @@
 # error BSIZE must be greater YYMAXFILL
 #endif
 
-#define        YYCTYPE         char
+#define        YYCTYPE         unsigned char
 #define        YYCURSOR        s.cur
 #define        YYLIMIT         s.lim
 #define YYMARKER       s.mrk
 
 typedef struct Scanner
 {
-       FILE            *fp;
-       char            *cur, *tok, *lim, *eof, *mrk;
-       char            buffer[BSIZE];
+       FILE                    *fp;
+       unsigned char   *cur, *tok, *lim, *eof, *mrk;
+       unsigned char   buffer[BSIZE];
 } Scanner;
 
 int fill(Scanner *s, int len)
 {
        if (!len)
        {
-               s->tok = s->cur = s->lim = s->mrk = s->buffer;
+               s->cur = s->tok = s->lim = s->mrk = s->buffer;
                s->eof = 0;
        }
        if (!s->eof)
index c18d378455ea28b968feec3474d5b06ad7773ae9..abf83af82a6846ba1a132603c90b3fbe4dcf7b1e 100755 (executable)
@@ -41,7 +41,7 @@
 # error BSIZE must be greater YYMAXFILL
 #endif
 
-#define        YYCTYPE         char
+#define        YYCTYPE         unsigned char
 #define        YYCURSOR        s.cur
 #define        YYLIMIT         s.lim
 #define YYMARKER       s.mrk
 
 typedef struct Scanner
 {
-       FILE            *fp;
-       char            *cur, *tok, *lim, *eof, *mrk;
-       char            buffer[BSIZE];
+       FILE                    *fp;
+       unsigned char   *cur, *tok, *lim, *eof, *mrk;
+       unsigned char   buffer[BSIZE];
 } Scanner;
 
 int fill(Scanner *s, int len)
 {
        if (!len)
        {
-               s->tok = s->cur = s->lim = s->mrk = s->buffer;
+               s->cur = s->tok = s->lim = s->mrk = s->buffer;
                s->eof = 0;
        }
        if (!s->eof)
index eb166021a6d02f07436a5f77ba95356b3dabbaac..af71c6e54cf667765218fa6a551f6b0d888dade5 100755 (executable)
@@ -14,7 +14,7 @@
 # error BSIZE must be greater YYMAXFILL
 #endif
 
-#define        YYCTYPE         char
+#define        YYCTYPE         unsigned char
 #define        YYCURSOR        s.cur
 #define        YYLIMIT         s.lim
 #define YYMARKER       s.mrk
 
 typedef struct Scanner
 {
-       FILE            *fp;
-       char            *cur, *tok, *lim, *eof, *ctx, *mrk;
-       char            buffer[BSIZE];
+       FILE                    *fp;
+       unsigned char   *cur, *tok, *lim, *eof, *ctx, *mrk;
+       unsigned char   buffer[BSIZE];
 } Scanner;
 
 int fill(Scanner *s, int len)
 {
        if (!len)
        {
-               s->tok = s->cur = s->lim = s->mrk = s->buffer;
+               s->cur = s->tok = s->lim = s->mrk = s->buffer;
                s->eof = 0;
        }
        if (!s->eof)
index b53cee05f1e5424f94bdc6eae74d5d7b3b2fbdbc..e150e34ca228591be7b5bc06aa1d31c3cd73100c 100755 (executable)
@@ -46,7 +46,7 @@
 # error BSIZE must be greater YYMAXFILL
 #endif
 
-#define        YYCTYPE         char
+#define        YYCTYPE         unsigned char
 #define        YYCURSOR        s.cur
 #define        YYLIMIT         s.lim
 #define YYMARKER       s.mrk
 
 typedef struct Scanner
 {
-       FILE            *fp;
-       char            *cur, *tok, *lim, *eof, *ctx, *mrk;
-       char            buffer[BSIZE];
+       FILE                    *fp;
+       unsigned char   *cur, *tok, *lim, *eof, *ctx, *mrk;
+       unsigned char   buffer[BSIZE];
 } Scanner;
 
 int fill(Scanner *s, int len)
 {
        if (!len)
        {
-               s->tok = s->cur = s->lim = s->mrk = s->buffer;
+               s->cur = s->tok = s->lim = s->mrk = s->buffer;
                s->eof = 0;
        }
        if (!s->eof)