lab6-70-73

 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
%option noyywrap bison-bridge bison-locations
%{
#include < stdio .h >
#include < stdlib .h >
#define TAG_IDENT 1
#define TAG_NUMBER 2
#define TAG_CHAR 3
#define TAG_LPAREN 4
#define TAG_RPAREN 5
#define TAG_PLUS 6
#define TAG_MINUS 7
#define TAG_MULTIPLY 8
#define TAG_DIVIDE 9
char *tag_names[] =
{
"END_OF_PRO GRAM" , "IDENT" , "NUMBER" ,
"CHAR" , "LPAREN" , "RPAREN" , "PLUS" ,
"MINUS" , "MULTIPLY" , "DIVIDE"
};
struct Position
{
int line , pos , index ;
};
void print_pos ( struct Position *p )
{
printf("(%d,%d)" ,p->line, p->pos);
}
struct Fragment
{
struct Position starting , following ;
};
typedef struct Fragment YYLTYPE ;
void print_frag ( struct Fragment *f )
{
print_pos (&(f->starting));
printf ("-");
print_pos(&(f->following));
}
int continued;
struct Position cur;
#define YY_USER_ACTION
{
int i;
if (! continued)
yylloc->starting = cur;
continued = 0;
for (i = 0; i < yyleng; i++)
{
if (yytext[i] == ’\n)
{
cur.line++;
cur.pos = 1;
}
else
cur.pos++;
cur.index++;
}
yylloc->following = cur;
}