summaryrefslogtreecommitdiff
path: root/lex.l
diff options
context:
space:
mode:
Diffstat (limited to 'lex.l')
-rw-r--r--lex.l47
1 files changed, 47 insertions, 0 deletions
diff --git a/lex.l b/lex.l
new file mode 100644
index 0000000..2f79474
--- /dev/null
+++ b/lex.l
@@ -0,0 +1,47 @@
+%{
+#include "grammar.tab.h"
+%}
+
+%%
+"+" { return OPERATOR_PLUS; }
+"-" { return OPERATOR_MINUS; }
+"=" { return ASSIGN; }
+
+"function" { return FUNCTION; }
+"if" { return IF; }
+"else" { return ELSE; }
+"return" { return RETURN; }
+"const" { return CONST; }
+"var" { return VAR; }
+
+"(" { return PARENTHESIS_LEFT; }
+")" { return PARENTHESIS_RIGHT; }
+"{" { return BRACE_LEFT; }
+"}" { return BRACE_RIGHT; }
+"[" { return BRACKET_LEFT; }
+"]" { return BRACKET_RIGHT; }
+
+"," { return COMMA; }
+
+[0-9]+ { yylval = atoi(yytext); return NUMBER; }
+[a-zA-Z]+[a-zA-Z0-9]* { return IDENTIFIER; }
+"//".* { return COMMENT; }
+
+\n { return END_OF_LINE; }
+[ \t] {}
+
+. { printf("undefined: %c\n", *yytext); }
+%%
+
+void henshin_lex(int argc, char **argv)
+{
+ int tok;
+
+ while (tok = yylex()) {
+ printf("%d", tok);
+ if (tok == NUMBER) {
+ printf(" = %d", yylval);
+ }
+ printf("\n");
+ }
+}