bits_scan.l
2.01 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* bits_scan.l - Tokenizer for bit description routine */
%p 150
%a 100
%o 1200
digit [0-9]
hexdigit [0-9a-fA-F]
bindigit [0-1]
tick [']
colon [:]
comma [,]
lbrk [\[]
rbrk [\]]
lbrace [\{]
rbrace [\}]
hex {tick}[h]{hexdigit}+
bin {tick}[b]{bindigit}+
number {digit}+
endofline [\n]
skip [ \t]
backslash [\\]
%%
{number} { install_number(yytext); return (tNUMBER); }
{hex} { install_hex(yytext); return (tHEX); }
{bin} { install_bin(yytext); return (tBIN); }
{colon} { return (tCOLON); }
{comma} { return (tCOMMA); }
{lbrk} { return (tLBRACK); }
{rbrk} { return (tRBRACK); }
{lbrace} { return (tLBRACE); }
{rbrace} { return (tRBRACE); }
quit { return (tQUIT); }
{backslash} { int c; while ( (c = input()) != '\n' &&
c != EOF )
; /* eat up input til next line */
}
{endofline} { return (tEOL); }
{skip} ;
. { return (tJUNK); }
%%
#undef input
#undef unput
static struct {
int line_no;
int left;
char *pbuf;
char buf[512];
} cmd_buf;
int yywrap()
{
return(1);
}
void install_number(char *str)
{
yylval.ival = strtol(str, NULL, 10);
}
void install_hex(char *str)
{
yylval.ival = strtol(&str[2], NULL, 16);
}
void install_bin(char *str)
{
yylval.ival = strtol(&str[2], NULL, 2);
}
void install(char *str)
{
(void) strncpy(yylval.name, str, TOKENSIZ-1);
yylval.name[TOKENSIZ-1] = '\0';
}
/*
* Parse GlobString once
*/
int input()
{
static int first_time = 1;
if(cmd_buf.left < 1 && first_time) {
first_time = 0;
cmd_buf.line_no++;
strcpy(cmd_buf.buf, GlobString);
cmd_buf.left = strlen(cmd_buf.buf);
if(cmd_buf.left < 0)
return 0;
cmd_buf.pbuf = cmd_buf.buf;
}
else if(cmd_buf.left < 1 && !first_time)
{
/* clean up for next call */
first_time = 1;
}
cmd_buf.left--;
return(*cmd_buf.pbuf++);
}
int unput(int c)
{
if (cmd_buf.pbuf > cmd_buf.buf)
{
cmd_buf.pbuf--;
cmd_buf.left++;
cmd_buf.pbuf[0] = c;
}
}
void
yyerror(char *str)
{
printf("line %d: %s\n", cmd_buf.line_no, str);
}