summaryrefslogtreecommitdiff
path: root/misc-test/compilateur_c_en_c/compil.lex
blob: 32ddfe5c142fce1a5a21f1750ab685408c9208cd (plain)
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
%{
	#include "y.tab.h"
	#include "reloc.h" /* Pour MAX_LABEL_LEN */
	
	#include <string.h>

%}

%option nounput


CHIFFRE	[0-9]
BLANC [ \t\n]
IDENTIFICATEUR [a-zA-Z][a-zA-Z0-9_]*

%x MLCOMMENT 
	/*	%s : inclusive start condition (inclus aussi les règles sans <cond>)
		%x : exclusive start condition (règles sans <cond> inactives)
	*/

%%

"/*"			BEGIN(MLCOMMENT);
<MLCOMMENT>\n		yylineno++;;
<MLCOMMENT>"*/"		BEGIN(INITIAL);
<MLCOMMENT>.		;

"//"[^\n]*\n		yylineno++; /* Single line Comment*/

if 			return IF;
else 			return ELSE;
while 			return WHILE;
do			return DO;

main{BLANC}*\({BLANC}*\) 	return MAIN;
printf		 	return PRINTF;
\{ 			return '{';
\} 			return '}';
const 			return CONST;
int 			return INT;
"*" 			return '*';
"/" 			return '/';
"+" 			return '+';
"-" 			return '-';
">"			return '>';
"<"			return '<';
"=" 			return AFF;
"=="			return EGALE;
"!="			return DIFFERENT;
\( 			return '(';
\) 			return ')';
{IDENTIFICATEUR}	{
				int lg = strlen(yytext);
				if ( lg > MAX_LABEL_LEN ) {
					return ERROR;
				} else {
					yylval.str = malloc(sizeof(char)*lg);
					strcpy(yylval.str, yytext);
					return IDENTIFICATEUR;
				}
			}
{CHIFFRE}+		{
				yylval.nb = atoi(yytext);
				return ENTIER;
			}

","			return ',';
";"			return ';';
\n			yylineno++; /*printf("\n");*/
{BLANC}			/*printf("BLANC");*/

[^ \t\n]		fprintf(stderr, "LEX : ERROR : unknown char '%c'", yytext[0]);   // TODO standardiser le msg d'erreur
	/*[ \t] ;*/
%%