Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
%{
/* 
 * Plein de #include et #define ... que je ne copie pas
 */
char string_buf[MAX_STR_CONST]; /* to assemble string constants */
char *string_buf_ptr=&(string_buf[0]);
 
 
/* ERROR STRINGS */
const char EOFINCOMMENT[] = "EOF in comment";
const char STRTOOLONG[] = "Const strings max size is 1024";
const char EOFINSTR[] = "EOF in string";
const char COMMENTOUT[] = "'*)' out of comment";
const char NLINSTR[] = "NLINSTR";
 
/* Gestion des erreurs*/
 
void report_error(const char* Error_String)
{
        cool_yylval.error_msg = (char *) malloc(sizeof(char) * (strlen(Error_String)+1));
        strcpy(cool_yylval.error_msg,Error_String);
}
 
/* Fonctions de gestion du buffer */
 
void init_string_buf()
{
        *string_buf_ptr = 'a';
        string_buf_ptr = &(string_buf[0]);
        string_buf[MAX_STR_CONST-1]='\0';
}
 
int append_string_buf(const char c)
{
        if (*string_buf_ptr) { *string_buf_ptr=c; string_buf_ptr++; return 1; }
        else { return 0;}
}
 
%}
 
ESCCHAR         [ntbf]
NL                     \n
 
%x STRMODE STROVERFLOW
%%
 
/* Je passe une grande partie du plat de spaghetti... */
 
/*
  * Entering STRMODE
  */
 
\" { init_string_buf(); BEGIN(STRMODE);}
 
/* Gestion des chaines de caracteres */
<STRMODE>\\{NOESCCHAR} { if(append_string_buf(yytext[1])!=1) { report_error(STRTOOLONG) ; BEGIN(STROVERFLOW); return (ERROR);}}
 
<STRMODE>\\n {  if(append_string_buf(10)!=1) { report_error(STRTOOLONG) ; BEGIN(STROVERFLOW); return (ERROR);} }
 
<STRMODE>\\t {  if(append_string_buf(9)!=1) { report_error(STRTOOLONG) ; BEGIN(STROVERFLOW); return (ERROR);} }
 
<STRMODE>\\b {  if(append_string_buf(8)!=1) { report_error(STRTOOLONG) ; BEGIN(STROVERFLOW); }return (ERROR); }
 
<STRMODE>\\f {  if(append_string_buf(12)!=1) { report_error(STRTOOLONG) ; BEGIN(STROVERFLOW); return (ERROR);} }
 
<STRMODE>\\{NL} {  curr_lineno++; if(append_string_buf(10)!=1) { report_error(STRTOOLONG) ; BEGIN(STROVERFLOW);  return (ERROR);} }
 
 
 
<STRMODE,STROVERFLOW>\"|\n {    *string_buf_ptr = '\0' ;
                cool_yylval.symbol = stringtable.add_string(string_buf);
                BEGIN(INITIAL); return STR_CONST; }
<STRMODE><<EOF>> { report_error(EOFINSTR); return (ERROR);}
<STRMODE>{NL} { report_error(NLINSTR); }
<STRMODE>.  {  if(append_string_buf(yytext[0])!=1) { report_error(STRTOOLONG) ; BEGIN(STROVERFLOW); return (ERROR);}}
 
<STROVERFLOW>[^\"{NL}] {}
Je compile et teste sur les lignes 5 et 6 ci dessous :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
 
"str";
"str \h \k \m \" ";
Et j obtiens :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
 
#5 ERROR "Const strings max size is 1024"
#5 STR_CONST "s"
#5 ';'
#6 ERROR "Const strings max size is 1024"
#6 STR_CONST "st"
#6 STR_CONST ";"
Bref, ca ne marche pas tout a fait comme attendu
Si quelqu un a une idée, je suis preneur.