summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2024-08-17 08:00:00 +0800
committerRunxi Yu <me@runxiyu.org>2024-08-17 08:00:00 +0800
commit1af1d033ab87cadb9bb9107774c21277ffc5b910 (patch)
tree57503e844fc6d3e1fea451f0ee345c439ff7f1ab
parentVaxis test (diff)
downloadproground-master.tar.gz
proground-master.zip
More lexer stuffHEADmaster
-rw-r--r--yacclex/calc/calc.l22
-rw-r--r--yacclex/calclex/calc.l22
-rw-r--r--yacclex/calclexret/calc.l39
3 files changed, 61 insertions, 22 deletions
diff --git a/yacclex/calc/calc.l b/yacclex/calc/calc.l
deleted file mode 100644
index d6c6014..0000000
--- a/yacclex/calc/calc.l
+++ /dev/null
@@ -1,22 +0,0 @@
-%{
-%}
-
-%%
-"+" { printf("PLUS\n"); }
-"-" { printf("MINUS\n"); }
-"*" { printf("TIMES\n"); }
-"/" { printf("DIVIDE\n"); }
-"|" { printf("ABS\n"); }
-[0-9]+ { printf("NUMBER %s\n", yytext); }
-\n { printf("NEWLINE\n"); }
-[ \t] { }
-. { printf("Mystery character %s\n", yytext); }
-%%
-
-int main (void) {
- yylex();
-}
-
-int yywrap() {
- return 1;
-}
diff --git a/yacclex/calclex/calc.l b/yacclex/calclex/calc.l
new file mode 100644
index 0000000..056980e
--- /dev/null
+++ b/yacclex/calclex/calc.l
@@ -0,0 +1,22 @@
+%{
+%}
+
+%%
+"+" { printf("PLUS\n"); }
+"-" { printf("MINUS\n"); }
+"*" { printf("TIMES\n"); }
+"/" { printf("DIVIDE\n"); }
+"|" { printf("ABS\n"); }
+[0-9]+ { printf("NUMBER %s\n", yytext); }
+\n { printf("NEWLINE\n"); }
+[ \t] { }
+. { printf("Mystery character %s\n", yytext); }
+%%
+
+int main (void) {
+ yylex();
+}
+
+int yywrap() {
+ return 1;
+}
diff --git a/yacclex/calclexret/calc.l b/yacclex/calclexret/calc.l
new file mode 100644
index 0000000..f681fa5
--- /dev/null
+++ b/yacclex/calclexret/calc.l
@@ -0,0 +1,39 @@
+%{
+enum yytokentype {
+ NUMBER = 258,
+ ADD = 259,
+ SUB = 260,
+ MUL = 261,
+ DIV = 262,
+ ABS = 263,
+ EOL = 264
+};
+
+int yylval;
+%}
+
+%%
+"+" { return ADD; }
+"-" { return SUB; }
+"*" { return MUL; }
+"/" { return DIV; }
+"|" { return ABS; }
+[0-9]+ { yylval = atoi(yytext); return NUMBER; }
+\n { return EOL; }
+[ \t] { /* ignore whitespace */ }
+. { printf("Mystery character %c\n", *yytext); }
+%%
+
+int yywrap() {
+ return 1;
+}
+
+int main(void) {
+ int tok;
+ while((tok = yylex())) {
+ printf("%d", tok);
+ if(tok == NUMBER)
+ printf(" = %d\n", yylval);
+ else printf("\n");
+ }
+}