diff options
| Commit | 387ec42070ea17cd57658d3386b0529ca9be1782 (patch) | |
|---|---|---|
| Author | 2022-11-16 21:56:36 +0200 | |
| Committer | 2022-11-16 21:56:36 +0200 | |
| Files | b908a2c0f087e0cf41ca4ab9e982fae2b96e055a | |
| uni-projects-387ec42070ea17cd57658d3386b0529ca9be1782.tar uni-projects-387ec42070ea17cd57658d3386b0529ca9be1782.tar.gz uni-projects-387ec42070ea17cd57658d3386b0529ca9be1782.zip | ||
| Parent | 9b9cac4c0037b3c397842143f85c08e13d2bc91e (diff) | |
[IPL/HW02] Added tokens for all types of parsed data
2 files changed, 28 insertions, 21 deletions
| -rw-r--r-- | Year 2/Implementation of Programming Languages/Homework 02/codefold.y | 21 | ||||
| -rw-r--r-- | Year 2/Implementation of Programming Languages/Homework 02/commonlisp2html.l | 28 |
diff --git a/Year 2/Implementation of Programming Languages/Homework 02/codefold.y b/Year 2/Implementation of Programming Languages/Homework 02/codefold.y index 980f684..b3cf152 100644 --- a/Year 2/Implementation of Programming Languages/Homework 02/codefold.y +++ b/Year 2/Implementation of Programming Languages/Homework 02/codefold.y @@ -15,16 +15,16 @@ char* concatperm2(char *first, char *second); char* concatperm3(char *first, char *second, char *third); %} -%token LBRACKET RBRACKET OTHER NEWLINE SPACE TAB +%token LBRACKET RBRACKET OTHER +%token NEWLINE SPACE TAB +%token STR NUM COMMENT PARAMETER_TYPE KEYWORD CONSTANT +%token SPECIAL_FORM FUNCTION MACRO VARIABLE %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); } @@ -40,8 +40,17 @@ list: terminal | list stlist { $$ = concatperm2($1, $2); } ; -terminal: OTHER - /* | terminal OTHER { $$ = concatperm2($1, span("test", $2)); free($2); } */ +terminal: STR { $$ = span("string", $1); free($1); } + | NUM { $$ = span("number", $1); free($1); } + | COMMENT { $$ = span("comment", $1); free($1); } + | PARAMETER_TYPE { $$ = span("parameter-type", $1); free($1); } + | KEYWORD { $$ = span("keyword", $1); free($1); } + | CONSTANT { $$ = span("constant", $1); free($1); } + | SPECIAL_FORM { $$ = span("special-operator", $1); free($1); } + | FUNCTION { $$ = span("function", $1); free($1); } + | MACRO { $$ = span("macro", $1); free($1); } + | VARIABLE { $$ = span("variable", $1); free($1); } + | OTHER { $$ = span("", $1); free($1); } ; %% 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 01b69d5..cbb226f 100644 --- a/Year 2/Implementation of Programming Languages/Homework 02/commonlisp2html.l +++ b/Year 2/Implementation of Programming Languages/Homework 02/commonlisp2html.l @@ -37,27 +37,25 @@ CONST_VAR \+([0-9A-z.?-_])+\+ %% -{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); } -{PARAMETER_TYPE} { printf("<span class=\"parameter-type\">%s</span>", yytext); } -{KEYWORD} { printf("<span class=\"keyword\">%s</span>", yytext); } -{CONSTANTS} { printf("<span class=\"constant\">%s</span>", yytext); } +{LEFTBRACKET} { return LBRACKET; } +{RIGHTBRACKET} { return RBRACKET; } +{STR} { yylval = strdup(yytext); return STR; } +{NUMBERS} { yylval = strdup(yytext); return NUM; } +{COMMENT} { yylval = strdup(yytext); return COMMENT;} +{PARAMETER_TYPE} { yylval = strdup(yytext); return PARAMETER_TYPE; } +{KEYWORD} { yylval = strdup(yytext); return KEYWORD; } +{CONSTANTS} { yylval = strdup(yytext); return CONSTANT; } -{SPECIAL_FORM} { printf("<span class=\"special-operator\">%s</span>", yytext); } -{F_ARITHMETIC}|{F_NUM_COMPARISON}|{F_TRIGONOMETRIC}|{F_TRIGONOMETRIC}|{F_OTHER_MATH}|{F_LOGICAL}|{F_LISTS}|{F_OTHER} { printf("<span class=\"function\">%s</span>", yytext); } -{M_ARITHMETIC}|{M_LOGICAL}|{M_CONDITIONAL}|{M_DECLARE} { printf("<span class=\"macro\">%s</span>", yytext); } +{SPECIAL_FORM} { yylval = strdup(yytext); return SPECIAL_FORM; } +{F_ARITHMETIC}|{F_NUM_COMPARISON}|{F_TRIGONOMETRIC}|{F_TRIGONOMETRIC}|{F_OTHER_MATH}|{F_LOGICAL}|{F_LISTS}|{F_OTHER} { yylval = strdup(yytext); return FUNCTION; } +{M_ARITHMETIC}|{M_LOGICAL}|{M_CONDITIONAL}|{M_DECLARE} { yylval = strdup(yytext); return MACRO; } -{GLOBAL_VAR}|{CONST_VAR} { printf("<span class=\"variable\">%s</span>", yytext); } +{GLOBAL_VAR}|{CONST_VAR} { yylval = strdup(yytext); return VARIABLE; } - /* " " { 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]+ { yylval = strdup(yytext); return OTHER; } +[A-z]+|" "|\t { yylval = strdup(yytext); return OTHER; } %% |
