option noyywrap bison-bridge bison-locations include stdio include std

  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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
%option noyywrap bison-bridge bison-locations
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TAG_STRING 1
#define TAG_NUMBER 2
#define TAG_IDENT 3
char *tag_names[] =
{
"END_OF_PROGRAM", "STRING", "NUMBER", "IDENT"
};
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));
}
union Token {
char *ident;
float num;
char *st2;
};
typedef union Token YYSTYPE;
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; \
}
void init_scanner(char *program)
{
continued = 0;
cur.line = 1;
cur.pos =1;
cur.index = 0;
yy_scan_string(program);
}
void err(char *msg)
{
printf("Error ");
print_pos(&cur);
printf(": %s\n",msg);
}
%}
SPACE [\n\t ]+
STRING \'([^']|\'{2})+\'
DIGIT (\-)?[0-9]+(\.[0-9]+)?
IDENT [a-zA-Z]([a-zA-Z]*(\.)*[0-9]*)+
%x STRINGG STRINGG_IN
%%
{SPACE}
{DIGIT} {
yylval->num = atof(yytext);
return TAG_NUMBER;
}
{IDENT} {
yylval->ident = yytext;
return TAG_IDENT;
}
\' {BEGIN(STRINGG); continued = 1;yylval->st2 = (char*) malloc (1000);}
<STRINGG>\\\n err ("unexpected \n, expected ' ");
<STRINGG>[^']* {
continued = 1;
yylval->st2 = strcat (yylval->st2, yytext);
}
<STRINGG>\'\' {
continued = 1;
yylval->st2 = strcat (yylval->st2, yytext);
}
<STRINGG>\' {
yylval->st2 = strcat (yylval->st2, yytext);
BEGIN(0);
return TAG_STRING;
}
<STRINGG><<EOF>> {
err("end of program found, ' expected");
return 0;
}
. err("unexpected character");
%%
#define PROGRAM "'sfg''df' % dg435..2 -1.3"
int main ()
{
int tag;
YYSTYPE value;
YYLTYPE coords;
init_scanner (PROGRAM);
do
{
tag = yylex (&value ,&coords);
if (tag != 0)
{
print_frag(&coords);
if (tag == 1) {
printf(" %s \'%s\n", tag_names[tag], value.st2);
free (value.st2);
} else {
if (tag == 2 ) {
printf(" %s %f\n", tag_names[tag], value.num);
} else {
printf(" %s %s\n", tag_names[tag], value.ident);
}
}
}
}
while ( tag != 0);
return 0;
}