Skip to content

Commit

Permalink
finish macro
Browse files Browse the repository at this point in the history
  • Loading branch information
tutuwel committed Oct 28, 2023
1 parent ccc8f8f commit 696af1f
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 349 deletions.
36 changes: 17 additions & 19 deletions phase1/lex.l
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,27 @@
valid_decimal_int 0|[1-9][0-9]*
valid_hex_int [xX](0|[1-9a-f][0-9a-f]*)
id [_a-zA-Z][_0-9a-zA-Z]*

%x macro
%%
/*TODO: handle comment*/
/*TODO: handle string*/

"//".* {}
"/*"((("*"[^/])?)|[^*])*"*/" {}
"#define" {yylval=strdup("DEFINE\n"); return DEFINE;}
"#include" {yylval=strdup("INCLUDE\n"); return INCLUDE;}
"#ifdef" {yylval=strdup("IFDEF\n"); return IFDEF;}
"#else" {yylval=strdup("MACROELSE\n"); return MACROELSE;}
"#endif" {yylval=strdup("ENDIF\n"); return ENDIF;}
"#define" {yylval=strdup("DEFINE\n"); BEGIN(macro); return DEFINE;}
"#include" {yylval=strdup("INCLUDE\n"); BEGIN(macro); return INCLUDE;}
"#ifdef" {yylval=strdup("IFDEF\n"); BEGIN(macro); return IFDEF;}
"#else" {yylval=strdup("MACROELSE\n"); BEGIN(macro); return MACROELSE;}
"#endif" {yylval=strdup("ENDIF\n"); BEGIN(macro); return ENDIF;}
<macro>{
"," {yylval=strdup("COMMA\n");return COMMA;}
"<" {return LT;}
">" {return GT;}
\" {return DQUOT;}
\n {BEGIN(INITIAL);}
[ \t]+ /*ignore word splits*/{}
[^," "<>"\n]+ {asprintf(&yylval,"%s\n",yytext);return MACRO;}
}
int|float|char {asprintf(&yylval,"TYPE: %s\n",yytext); return TYPE;}
for {yylval=strdup("FOR\n"); return FOR;}
struct {yylval=strdup("STRUCT\n"); return STRUCT;}
Expand Down Expand Up @@ -86,20 +95,9 @@ return {yylval=strdup("RETURN\n"); return RETURN;}
}
"." {yylval=strdup("DOT\n"); return DOT;}
\n {yycolno=1;}
[ \t]+ /*ignore word splits*/
[ \r\t]+ /*ignore word splits*/
. {lexical_error(yytext, yylineno);}

\<[^" "\n]*\> | \"[^" "\n]*\" {yylval = strndup(yytext + 1, strlen(yytext) - 2); return INFILE;}

" "*[^" "\n]* {string inputString(yytext);
regex pattern("\\s*");
yylval=strdup(yytext);
regex_replace(inputString, pattern, "");
return FOLLOW_DEFINE;}
," "*[^" "\n]* {string inputString(yytext);
regex pattern(",\\s*");
yylval=strdup(yytext);
regex_replace(inputString, pattern, "");
return MAYFOLLOW_DEFINE;}

. {lexical_error(yytext, yylineno);}
%%
66 changes: 0 additions & 66 deletions phase1/myOutput/output1.txt

This file was deleted.

83 changes: 0 additions & 83 deletions phase1/myOutput/output2.txt

This file was deleted.

2 changes: 0 additions & 2 deletions phase1/myOutput/output3.txt

This file was deleted.

77 changes: 0 additions & 77 deletions phase1/myOutput/output5.txt

This file was deleted.

97 changes: 0 additions & 97 deletions phase1/myOutput/output9.txt

This file was deleted.

Loading

0 comments on commit 696af1f

Please sign in to comment.