scanner: erster versuch (ohne tests)
[uebersetzerbau-ss10.git] / scanner / scanner.lex
diff --git a/scanner/scanner.lex b/scanner/scanner.lex
new file mode 100644 (file)
index 0000000..ebf5f8f
--- /dev/null
@@ -0,0 +1,53 @@
+       #include <stdio.h>
+       #include <stdlib.h>
+       #include <string.h>
+
+KEYWORD struct|end|method|var|if|then|else|while|do|return|not|or|this
+SPECIAL_CHAR \;|\(|\)|\:=|\.|\-|\*|\<|\=|\.
+IDENTIFIER [a-zA-Z_][0-9a-zA-Z_]*
+NUMBER_HEX 0x[0-9A-Fa-f]+
+NUMBER_DEC [0-9]+
+WHITESPACE [\t\n\r ]
+COMMENT_START \/\*
+COMMENT_END    \*\/
+
+/* x ist unsere zustandsvariable! */
+%x COMMENT
+%option yylineno
+%%
+
+{COMMENT_START}        BEGIN(COMMENT);
+
+<COMMENT>{COMMENT_END} BEGIN(INITIAL);
+
+<COMMENT><<EOF>> { (void) fprintf(stderr, "kommentar nicht geschlossen\n"); exit(1); }
+
+<COMMENT>. /* alles im kommentar wird ignoriert */
+
+{KEYWORD} printf("%s\n", yytext);
+
+{SPECIAL_CHAR} printf("%s\n", yytext);
+
+{IDENTIFIER} printf("ident %s\n", yytext);
+
+{NUMBER_DEC} printf("num %x\n", strtol(yytext, (char **)NULL, 10));
+
+{NUMBER_HEX} {
+#if 0
+       char *copy=strdup(yytext);
+       copy[strlen(copy)-1]='\0';
+#endif
+       printf("num %x\n", strtol(yytext, (char **)NULL, 16));
+}
+
+{WHITESPACE} /* ignorieren */
+
+. { (void) fprintf(stderr, "Lexikalischer fehler auf Zeile %i\n", yylineno); exit(1); }
+
+%%
+
+int main(int argc, char **argv) {
+       yylex();
+       return 0;
+}
+