1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "dm.h"
#define CTS(X) {if (!dm_current_tok)token_s = p; printf("%s ",#X);if (!dm_current_tok) dm_current_tok = TOK_##X;}
#define CTE() {token_e = p;}
#define TADD(T,S,E) {tl_add_tok(tl,T,S,E);}
char *new_string( const char *start, const char *end )
{
int str_s = end-start+1;
char *new_str=malloc( str_s+1 );
memcpy( new_str, start, str_s );
if ( new_str != NULL )
new_str[str_s]=0x0;
return new_str;
}
int print_token( char *s, char *e, int tok)
{
char *p = new_string( s, e );
printf("t=%d,p=%s\n",tok,p);
free( p );
return 0;
}
%%{
machine dm;
hex = '0x' [a-fA-F0-9]+;
int = [0-9]+;
newline = '\n' >{CTS(NL)} @{CTE()};
if = 'if';
lbracket = '(';
rbracket = ')';
and = '&';
range = '-';
string = ('"' ([a-zA-Z0-9' '])+ '"');
sp = (' ' | '\t' | '\n');
main := |*
hex {/*printf("hex = ");fwrite(ts,1,te-ts,stdout);printf("\n");*/TADD(TOK_HEX,ts,te);};
sp {};
string {/*printf("str = ");fwrite(ts,1,te-ts,stdout);printf("\n");*/TADD(TOK_STR,ts,te);};
int {/*printf("int = ");fwrite(ts,1,te-ts,stdout);printf("\n");*/TADD(TOK_INT,ts,te);};
if {/*printf("if = ");fwrite(ts,1,te-ts,stdout);printf("\n");*/TADD(TOK_IF,ts,te);};
lbracket {/*printf("lbr = ");fwrite(ts,1,te-ts,stdout);printf("\n");*/TADD(TOK_LBR,ts,te);};
rbracket {/*printf("rbr = ");fwrite(ts,1,te-ts,stdout);printf("\n");*/TADD(TOK_RBR,ts,te);};
and {/*printf("and = ");fwrite(ts,1,te-ts,stdout);printf("\n");*/TADD(TOK_EAND,ts,te);};
range {/*printf("range = ");fwrite(ts,1,te-ts,stdout);printf("\n");*/TADD(TOK_RANGE,ts,te);};
*|;
write data;
}%%
int parse_dm( token_list *tl, const char *str )
{
static uint8_t cs;
const int stacksize = 10;
int res=0, *stack=NULL, act=0;
stack = malloc( sizeof(stack)*stacksize );
char *p=(char *)str, *pe = (char *)str + strlen( str ), *eof=NULL;
char *ts, *te = 0;
/*
variables used in state machine
*/
%%write init;
%%write exec;
if ( cs == dm_error )
{
printf("ERR state [%d] pos[%d]:[%s]\n", res, p-str, p);
res = -1;
}
return res;
}
|