Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commit9b9cac4c0037b3c397842143f85c08e13d2bc91e (patch)
Author
 Syndamia <kamen@syndamia.com>
2022-11-16 21:27:36 +0200
Committer
 Syndamia <kamen@syndamia.com>
2022-11-16 21:27:36 +0200
Files94d8bc3231a0e9657649f8557071e487cac79b76
uni-projects-9b9cac4c0037b3c397842143f85c08e13d2bc91e.tar
uni-projects-9b9cac4c0037b3c397842143f85c08e13d2bc91e.tar.gz
uni-projects-9b9cac4c0037b3c397842143f85c08e13d2bc91e.zip
Parenta2f93b2b17a2a101c849636ea10f31058cef6660 (diff)
[IPL/HW02] Implemented semi-working bison grammar and updated parts of flex regex
diff --git a/Year 2/Implementation of Programming Languages/Homework 02/codefold.y b/Year 2/Implementation of Programming Languages/Homework 02/codefold.y
new file mode 100644
index 0000000..980f684
--- /dev/null
+++ b/Year 2/Implementation of Programming Languages/Homework 02/codefold.y
@@ -0,0 +1,91 @@
+%{
+#include <stdio.h>
+#include <string.h>
+
+int yylex();
+int yyerror(char *s);
+
+#define LBRACKET_SPAN "<span class=\"bracket\">(</span>"
+#define RBRACKET_SPAN "<span class=\"bracket\">)</span>"
+
+char* span(char *c, char *s);
+char* detailsfr(char *s);
+char* lrbrackets();
+char* concatperm2(char *first, char *second);
+char* concatperm3(char *first, char *second, char *third);
+%}
+
+%token LBRACKET RBRACKET OTHER NEWLINE SPACE TAB
+
+%define api.value.type {char*}
+
+%%
+
+start:
+ | NEWLINE start { printf("\n"); }
+ | SPACE start { printf(" "); }
+ | TAB start { printf("\t"); }
+ | stlist start { printf("%s", $1); free($1); }
+ | terminal start { printf("%s", $1); free($1); }
+
+stlist: LBRACKET RBRACKET { $$ = lrbrackets(); }
+ | LBRACKET list RBRACKET { $$ = concatperm3(LBRACKET_SPAN, $2, RBRACKET_SPAN); }
+ | LBRACKET NEWLINE list RBRACKET { $$ = detailsfr(concatperm3(LBRACKET_SPAN, $3, RBRACKET_SPAN)); }
+ | LBRACKET list NEWLINE RBRACKET { $$ = detailsfr(concatperm3(LBRACKET_SPAN, $2, RBRACKET_SPAN)); }
+ ;
+
+list: terminal
+ | list terminal { $$ = concatperm2($1, $2); }
+ | stlist
+ | list stlist { $$ = concatperm2($1, $2); }
+ ;
+
+terminal: OTHER
+ /* | terminal OTHER { $$ = concatperm2($1, span("test", $2)); free($2); } */
+ ;
+
+%%
+
+
+char* span(char *c, char *s) {
+ char* str = (char*)malloc(strlen(c) + strlen(s) + 1 + 22);
+ sprintf(str, "<span class=\"%s\">%s</span>", c, s);
+ return str;
+}
+char* detailsfr(char *s) {
+ char* str = (char*)malloc(strlen(s) + 1 + 19);
+ sprintf(str, "<details>%s</details>", s);
+ free(s);
+ return str;
+}
+char* lrbrackets() {
+ char* str = (char*)malloc(strlen(LBRACKET_SPAN) + strlen(RBRACKET_SPAN) + 1);
+ sprintf(str, "%s%s", LBRACKET_SPAN, RBRACKET_SPAN);
+ return str;
+}
+
+char* concatperm2(char *first, char *second) {
+ char* concat = (char*)malloc(strlen(first) + strlen(second) + 1);
+ sprintf(concat, "%s%s", first, second);
+ free(first); free(second);
+ return concat;
+}
+char* concatperm3(char *first, char *second, char *third) {
+ char* concat = (char*)malloc(strlen(first) + strlen(second) + strlen(third) + 1);
+ sprintf(concat, "%s%s%s", first, second, third);
+ free(second);
+ return concat;
+}
+
+
+int yyerror(char *s) {
+ printf("Error: %s\n", s);
+ return 0;
+}
+
+int main(int argc, char **argv) {
+/* #ifdef YYDEBUG */
+/* yydebug = 1; */
+/* #endif */
+ yyparse();
+}
diff --git a/Year 2/Implementation of Programming Languages/Homework 02/commonlisp2html.l b/Year 2/Implementation of Programming Languages/Homework 02/commonlisp2html.l
index 46447b1..01b69d5 100644
--- a/Year 2/Implementation of Programming Languages/Homework 02/commonlisp2html.l
+++ b/Year 2/Implementation of Programming Languages/Homework 02/commonlisp2html.l
@@ -1,9 +1,10 @@
%{
-
+#include "codefold.tab.h"
%}
DIGIT [0-9]
-BRACKET \(|\)
+LEFTBRACKET \(
+RIGHTBRACKET \)
STR \"([^\"]|\\\")*([^\\]|\\\\)\"
NUMBERS [^A-z](\+|-)?{DIGIT}+(\.{DIGIT}+(e{DIGIT}+|d{DIGIT}+|e-{DIGIT}+)?|\/{DIGIT}+)?
@@ -36,7 +37,8 @@ CONST_VAR \+([0-9A-z.?-_])+\+
%%
-{BRACKET} { printf("<span class=\"bracket\">%s</span>", yytext); }
+{LEFTBRACKET} { return LBRACKET; }
+{RIGHTBRACKET} { return RBRACKET; }
{STR} { printf("<span class=\"string\">%s</span>", yytext); }
{NUMBERS} { printf("<span class=\"number\">%s</span>", yytext); }
{COMMENT} { printf("<span class=\"comment\">%s</span>", yytext); }
@@ -50,7 +52,12 @@ CONST_VAR \+([0-9A-z.?-_])+\+
{GLOBAL_VAR}|{CONST_VAR} { printf("<span class=\"variable\">%s</span>", yytext); }
+ /* " " { return SPACE; } */
+\t { return TAB; }
+\n { return NEWLINE; }
+
/* flex catches the longest possibility, so if some variable name contains a known word (function, macro, etc.), this will prevent it from highlighting */
-[A-z]+ { printf("%s", yytext); }
+[A-z]+ { yylval = strdup(yytext); return OTHER; }
+
%%