b86baaf732824b3f43bb0529f92c461d331ce960
[uebersetzerbau-ss10.git] / scanner / scanner.lex
1         #include <stdio.h>
2         #include <stdlib.h>
3         #include <string.h>
4
5 KEYWORD struct|end|method|var|if|then|else|while|do|return|not|or|this
6 SPECIAL_CHAR \;|\(|\)|\:=|\.|\-|\*|\<|\=|\.
7 IDENTIFIER [a-zA-Z_][0-9a-zA-Z_]*
8 NUMBER_HEX 0x[0-9A-Fa-f]+
9 NUMBER_DEC [0-9]+
10 WHITESPACE [\t\n\r ]
11 COMMENT_START \/\*
12 COMMENT_END     \*\/
13
14 /* x ist unsere zustandsvariable! */
15 %x COMMENT
16 %option yylineno
17 %%
18
19 {COMMENT_START} BEGIN(COMMENT);
20
21 <COMMENT>{COMMENT_END} BEGIN(INITIAL);
22
23 <COMMENT><<EOF>> { (void) fprintf(stderr, "kommentar nicht geschlossen\n"); exit(1); }
24
25 <COMMENT>{WHITESPACE} /* ignorieren */
26
27 <COMMENT>. /* alles im kommentar wird ignoriert */
28
29 {KEYWORD} printf("%s\n", yytext);
30
31 {SPECIAL_CHAR} printf("%s\n", yytext);
32
33 {IDENTIFIER} printf("ident %s\n", yytext);
34
35 {NUMBER_DEC} printf("num %lx\n", strtol(yytext, (char **)NULL, 10));
36
37 {NUMBER_HEX} {
38 #if 0
39         char *copy=strdup(yytext);
40         copy[strlen(copy)-1]='\0';
41 #endif
42         printf("num %lx\n", strtol(yytext, (char **)NULL, 16));
43 }
44
45 {WHITESPACE} /* ignorieren */
46
47 . { (void) fprintf(stderr, "Lexikalischer fehler auf Zeile %i\n", yylineno); exit(1); }
48
49 %%
50
51 int main(int argc, char **argv) {
52         yylex();
53         return 0;
54 }
55