From: helly Date: Sun, 16 Apr 2006 11:22:45 +0000 (+0000) Subject: - Update lesson 1 X-Git-Tag: 0.13.6~387 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=125fae3767f050dcc32cc62195d9501d93aa8f21;p=re2c - Update lesson 1 --- diff --git a/lessons/001_upn_calculator/calc_001.re b/lessons/001_upn_calculator/calc_001.re index 0e5e0461..911f89bf 100755 --- a/lessons/001_upn_calculator/calc_001.re +++ b/lessons/001_upn_calculator/calc_001.re @@ -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 diff --git a/lessons/001_upn_calculator/calc_006.s.c b/lessons/001_upn_calculator/calc_006.s.c index 720c4cef..f383e3e2 100755 --- a/lessons/001_upn_calculator/calc_006.s.c +++ b/lessons/001_upn_calculator/calc_006.s.c @@ -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 "" 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 "" 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 "" yy9: ++YYCURSOR; -#line 118 "calc_006.s.re" +#line 109 "calc_006.s.re" { res = stack_sub(); continue; } #line 119 "" yy11: ++YYCURSOR; -#line 119 "calc_006.s.re" +#line 110 "calc_006.s.re" { res = depth == 1 ? 0 : 2; continue; } #line 124 "" yy13: ++YYCURSOR; -#line 120 "calc_006.s.re" +#line 111 "calc_006.s.re" { res = 1; continue; } #line 129 "" 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 "" 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) { diff --git a/lessons/001_upn_calculator/calc_006.s.re b/lessons/001_upn_calculator/calc_006.s.re index d7947ccf..830af3dd 100755 --- a/lessons/001_upn_calculator/calc_006.s.re +++ b/lessons/001_upn_calculator/calc_006.s.re @@ -14,27 +14,18 @@ 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) { diff --git a/lessons/001_upn_calculator/calc_007.b.c b/lessons/001_upn_calculator/calc_007.b.c index c3bd70f7..3e47b860 100755 --- a/lessons/001_upn_calculator/calc_007.b.c +++ b/lessons/001_upn_calculator/calc_007.b.c @@ -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 #include @@ -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) { diff --git a/lessons/001_upn_calculator/calc_007.b.re b/lessons/001_upn_calculator/calc_007.b.re index aabd7e9f..469ae5e1 100755 --- a/lessons/001_upn_calculator/calc_007.b.re +++ b/lessons/001_upn_calculator/calc_007.b.re @@ -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) {