1// re2c $INPUT -o $OUTPUT
2/* re2c lesson 001_upn_calculator, calc_002, (c) M. Boerger 2006 - 2007 */
3/*!ignore:re2c
4
5- making use of YYFILL
6
7  . Here we modified the scanner to not require strlen() on the call. Instead
8    we compute limit on the fly. That is whenever more input is needed we
9    search for the terminating \0 in the next n chars the scanner needs.
10  . If there is not enough input we quit the scanner.
11  . Note that in lesson_001 YYLIMIT was a character pointer computed only once.
12    Here is of course also of type YYCTYPE but a variable that gets reevaluated
13    by YYFILL().
14  . To make the code smaller we take advantage of the fact that our loop has no
15    break so far. This allows us to use break here and have the code that is
16    used for YYFILL() not contain the printf in every occurence. That way the
17    generated code gets smaller.
18
19*/
20
21#include <stdlib.h>
22#include <stdio.h>
23#include <string.h>
24
25int fill(char *p, int n, char **l)
26{
27	while (*++p && n--) ;
28	* l = p;
29	return n <= 0;
30}
31
32int scan(char *s)
33{
34	char *p = s;
35	char *l = s;
36	char *q = 0;
37#define YYCTYPE         char
38#define YYCURSOR        p
39#define YYLIMIT         l
40#define YYMARKER        q
41#define YYFILL(n)		{ if (!fill(p, n, &l)) break; }
42
43	for(;;)
44	{
45/*!re2c
46	re2c:indent:top = 2;
47	"0"[0-9]+	{ printf("Oct\n");	continue; }
48	[1-9][0-9]*	{ printf("Num\n");	continue; }
49	"0"			{ printf("Num\n");	continue; }
50	"+"			{ printf("+\n");	continue; }
51	"-"			{ printf("+\n");	continue; }
52	"\000"		{ printf("EOF\n");	return 0; }
53	[^]			{ printf("ERR\n");	return 1; }
54*/
55	}
56	printf("OOD\n"); return 2;
57}
58
59int main(int argc, char **argv)
60{
61	if (argc > 1)
62	{
63		return scan(argv[1]);
64	}
65	else
66	{
67		fprintf(stderr, "%s <expr>\n", argv[0]);
68		return 0;
69	}
70}
71