b20091006473beb26dbb60233aeabefe7117c162
[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>(.|\n) /* alles im kommentar wird ignoriert */
26
27 {KEYWORD} printf("%s\n", yytext);
28
29 {SPECIAL_CHAR} printf("%s\n", yytext);
30
31 {IDENTIFIER} printf("ident %s\n", yytext);
32
33 {NUMBER_DEC} printf("num %lx\n", strtol(yytext, (char **)NULL, 10));
34
35 {NUMBER_HEX} {
36 #if 0
37         char *copy=strdup(yytext);
38         copy[strlen(copy)-1]='\0';
39 #endif
40         printf("num %lx\n", strtol(yytext, (char **)NULL, 16));
41 }
42
43 {WHITESPACE} /* ignorieren */
44
45 . { (void) fprintf(stderr, "Lexikalischer fehler auf Zeile %i\n", yylineno); exit(1); }
46
47 %%
48
49 int main(int argc, char **argv) {
50         yylex();
51         return 0;
52 }
53