aboutsummaryrefslogtreecommitdiff
path: root/mktpl/mktpl.lex
diff options
context:
space:
mode:
Diffstat (limited to 'mktpl/mktpl.lex')
-rw-r--r--mktpl/mktpl.lex86
1 files changed, 86 insertions, 0 deletions
diff --git a/mktpl/mktpl.lex b/mktpl/mktpl.lex
new file mode 100644
index 0000000..e530e45
--- /dev/null
+++ b/mktpl/mktpl.lex
@@ -0,0 +1,86 @@
+%x PRNT
+%x EVAL
+
+ #include <err.h>
+ #include <stdio.h>
+ #include <stdlib.h>
+ #include <string.h>
+
+ void apnd();
+ void eval();
+ void text();
+ void prnt();
+
+ char *buf;
+ int len;
+ int sz;
+
+%%
+(.|\n) apnd();
+\<%= text(); BEGIN(PRNT);
+\<%[^=] text(); BEGIN(EVAL);
+
+<PRNT>. apnd();
+<PRNT>%\> prnt(); BEGIN(0);
+
+<EVAL>(.|\n) apnd();
+<EVAL>%\> eval(); BEGIN(0);
+
+<PRNT><<EOF>> prnt(); yyterminate();
+<EVAL><<EOF>> eval(); yyterminate();
+<<EOF>> text(); yyterminate();
+%%
+
+int
+main(int argc, char *argv[])
+{
+ len = 0;
+ sz = 512;
+ if(!(buf = malloc(sz)))
+ err(1, "malloc");
+
+ yylex();
+}
+
+void
+apnd()
+{
+ if(len+1 > sz){
+ sz += 512;
+ if(!(buf = realloc(buf, sz)))
+ err(1, "realloc");
+ }
+
+ buf[len++] = *yytext;
+ buf[len+1] = 0;
+}
+
+void
+eval()
+{
+ printf("%s\n", buf);
+ len = 0;
+}
+
+void
+text()
+{
+ char *t;
+
+ printf("printf(\"");
+ for(t = buf; *t; t++){
+ if(*t == '\\') printf("\\\\");
+ else if(*t == '"') printf("\\\"");
+ else if(*t == '\n') printf("\\n");
+ else printf("%c", *t);
+ }
+ printf("\");\n");
+ len = 0;
+}
+
+void
+prnt()
+{
+ printf("printf(\"%%s\", %s);\n", buf);
+ len = 0;
+} \ No newline at end of file