Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commit0b26fa027ca143f086a64d412aa40a15be0e5cd7 (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-10-19 00:01:43 +0300
Committer
 Syndamia <kamen@syndamia.com>
2023-10-19 00:01:43 +0300
Files722994a64362eb9528f98d43702f697d25e27121
uni-projects-0b26fa027ca143f086a64d412aa40a15be0e5cd7.tar
uni-projects-0b26fa027ca143f086a64d412aa40a15be0e5cd7.tar.gz
uni-projects-0b26fa027ca143f086a64d412aa40a15be0e5cd7.zip
Parent4c603cf7417c83d38a517430b3feca6ba039a197 (diff)
[CI/PA1] Implemented all tokens, with esception to error and comments aren't perfect
1 files changed, 59 insertions, 12 deletions
-rw-r--r--Year 3/Compilers, Introduction/coolc-spim/my-code/coursework/pa1/cool.flex71
diff --git a/Year 3/Compilers, Introduction/coolc-spim/my-code/coursework/pa1/cool.flex b/Year 3/Compilers, Introduction/coolc-spim/my-code/coursework/pa1/cool.flex
index e7e0674..9f86243 100644
--- a/Year 3/Compilers, Introduction/coolc-spim/my-code/coursework/pa1/cool.flex
+++ b/Year 3/Compilers, Introduction/coolc-spim/my-code/coursework/pa1/cool.flex
@@ -43,6 +43,28 @@ extern YYSTYPE cool_yylval;
* Add Your own definitions here
*/
+char* escapeString(char* str) {
+ unsigned escapes = 0, length = 0;
+ for (char* i = str; *i != 0; i++) {
+ if (*i == '\\') escapes++;
+ length++;
+ }
+ char* out = new char[length -= 2];
+ char* currout = out;
+ bool skipnext = false;
+ while (length > 0 && *(++str + 1) != 0) {
+ if (*str != '\\') {
+ *(currout++) = *str;
+ }
+ else if (str[1] == 'b') { *(currout++) = '\b'; str++; }
+ else if (str[1] == 't') { *(currout++) = '\t'; str++; }
+ else if (str[1] == 'n') { *(currout++) = '\n'; str++; }
+ else if (str[1] == 'f') { *(currout++) = '\f'; str++; }
+ else if (str[1] == '\\') { *(currout++) = '\\'; str++; }
+ }
+ return out;
+}
+
%}
/*
@@ -52,7 +74,8 @@ extern YYSTYPE cool_yylval;
/*
Info from /usr/class/cs143/cool/include/PA2/cool-parse.h
Token:
-CLASS ELSE FI IF IN INHERITS LET LOOP POOL THEN WHILE CASE ESAC OF DARROW NEW ISVOID STR_CONST INT_CONST BOOL_CONST TYPEID OBJECTID ASSIGN NOT LE ERROR
+STR_CONST CLASS ELSE FI IF IN INHERITS ISVOID LET LOOP POOL THEN WHILE CASE ESAC NEW OF NOT DARROW TYPEID OBJECTID INT_CONST BOOL_CONST ASSIGN LE
+ERROR
yylval types:
Boolean boolean;
Symbol symbol;
@@ -70,18 +93,42 @@ yylval types:
char *error_msg;
*/
-OCBRACE \{
-CCBRACE \}
-ORBRACE \(
-CRBRACE \)
-COLUMN \:
-SEMICOLUMN \;
-
%%
-"=>" { return (DARROW); }
-"class" { return (CLASS); }
-[A-Z][a-z]* { cool_yylval.symbol = inttable.add_string(yytext); return (TYPEID); }
-\{|\}|\(|\)|:|; { return *yytext; }
+\"(\\\"|[^"])*\" { cool_yylval.symbol = inttable.add_string(escapeString(yytext)); return (STR_CONST); }
+--[^\n\r]* { }
+\(\*.*\*\) { }
+
+(?i:class) { return (CLASS); }
+(?i:else) { return (ELSE); }
+(?i:fi) { return (FI); }
+(?i:if) { return (IF); }
+(?i:in) { return (IN); }
+(?i:inherits) { return (INHERITS); }
+(?i:isvoid) { return (ISVOID); }
+(?i:let) { return (LET); }
+(?i:loop) { return (LOOP); }
+(?i:pool) { return (POOL); }
+(?i:then) { return (THEN); }
+(?i:while) { return (WHILE); }
+(?i:case) { return (CASE); }
+(?i:esac) { return (ESAC); }
+(?i:new) { return (NEW); }
+(?i:of) { return (OF); }
+(?i:not) { return (NOT); }
+
+"=>" { return (DARROW); }
+"<=" { return (LE); }
+"<-" { return (ASSIGN); }
+(t(?i:rue)|f(?i:alse)) { cool_yylval.boolean = *yytext == 't'; return (BOOL_CONST); }
+
+[a-z][A-Za-z0-9_]* { cool_yylval.symbol = inttable.add_string(yytext); return (OBJECTID); }
+[0-9]+ { cool_yylval.symbol = inttable.add_string(yytext); return (INT_CONST); }
+[A-Z][A-Za-z0-9_]* { cool_yylval.symbol = inttable.add_string(yytext); return (TYPEID); }
+
+\{|\}|\(|\)|:|;|,|@|\.|\+|-|\*|\/|~|<|<=|= { return *yytext; }
+
+[\n\r] { curr_lineno++; }
+[ \f\t\v] { }
%%