1// re2c $INPUT -o $OUTPUT
2/* re2c lesson 001_upn_calculator, calc_004, (c) M. Boerger 2006 - 2007 */
3/*!ignore:re2c
4
5- making use of definitions
6  . We provide complex rules as definitions. We can even have definitions made
7    up from other definitions. And we could also use definitions as part of
8    rules and not only as full rules as shown in this lesson.
9
10- showing the tokens
11  . re2c does not store the beginning of a token on its own but we can easily
12    do this by providing variable, in our case t, that is set to YYCURSOR on
13    every loop. If we were not using a loop here the token, we could have used
14    s instead of a new variable instead.
15  . As we use the token for an output function that requires a terminating zero
16    we copy the token. Alternatively we could store the end of the token, then
17    replace it with a zero character and replace it after the token has been
18    used. However that approach is not always acceptable.
19
20*/
21
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25
26char * tokendup(const char *t, const char *l)
27{
28	size_t n = l -t + 1;
29	char *r = (char*)malloc(n);
30
31	memmove(r, t, n-1);
32	r[n] = '\0';
33	return r;
34}
35
36int scan(char *s, int l)
37{
38	char *p = s;
39	char *q = 0;
40	char *t;
41#define YYCTYPE         char
42#define YYCURSOR        p
43#define YYLIMIT         (s+l+2)
44#define YYMARKER        q
45#define YYFILL(n)		{ printf("OOD\n"); return 2; }
46
47	for(;;)
48	{
49		t = p;
50/*!re2c
51	re2c:indent:top = 2;
52
53	DIGIT	= [0-9] ;
54	OCT		= "0" DIGIT+ ;
55	INT		= "0" | ( [1-9] DIGIT* ) ;
56
57	OCT			{ t = tokendup(t, p); printf("Oct: %s\n", t); free(t); continue; }
58	INT			{ t = tokendup(t, p); printf("Num: %s\n", t); free(t); continue; }
59	"+"			{ printf("+\n");	continue; }
60	"-"			{ printf("+\n");	continue; }
61	"\000"		{ printf("EOF\n");	return 0; }
62	[^]			{ printf("ERR\n");	return 1; }
63*/
64	}
65	return 0;
66}
67
68int main(int argc, char **argv)
69{
70	if (argc > 1)
71	{
72		return scan(argv[1], strlen(argv[1]));
73	}
74	else
75	{
76		fprintf(stderr, "%s <expr>\n", argv[0]);
77		return 0;
78	}
79}
80