Skip to content

Commit

Permalink
pass all case except error
Browse files Browse the repository at this point in the history
  • Loading branch information
tutuwel committed Oct 25, 2023
1 parent f8f5c66 commit 07f5089
Show file tree
Hide file tree
Showing 17 changed files with 650 additions and 900 deletions.
7 changes: 3 additions & 4 deletions phase1/Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
CC=gcc
CC=g++
FLEX=flex
BISON=bison

# Dependency Tree
splc: syntax.tab.* lex.yy.c
@mkdir -p bin
$(CC) syntax.tab.c -ll -o bin/splc
$(CC) syntax.tab.c tree.cpp -ll -o bin/splc
@chmod +x bin/splc
lex.yy.c: lex.l
$(FLEX) lex.l
Expand All @@ -17,4 +16,4 @@ lexer: syntax.tab.* lex.yy.c # For debugging lex.l

.PHONY: clean
clean:
@rm -rf lex.yy.c syntax.tab.c syntax.tab.h lexer bin
@rm -rf lex.yy.c syntax.tab.c syntax.tab.h lexer bin
193 changes: 153 additions & 40 deletions phase1/lex.l
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
%{
%{
#define EXIT_OK 0
#define EXIT_FAIL 1
#define YYSTYPE char *
#include "string.h"
#include "syntax.tab.h"

/* Rewrite the internal macro in Flex to update the location */
%}
%{
int yycolno = 1;
#define YY_USER_ACTION \
yylloc.first_line = yylineno; \
Expand All @@ -16,7 +15,7 @@
%}

%option yylineno

%option debug

valid_decimal_int 0|[1-9][0-9]*
valid_hex_int [xX](0|[1-9a-f][0-9a-f]*)
Expand All @@ -26,50 +25,164 @@ valid_hex_int [xX](0|[1-9a-f][0-9a-f]*)
/*TODO: handle string*/

"//".* {}

"/*"((("*"[^/])?)|[^*])*"*/" {}
int|float|char { yylval = strdup(yytext); return TYPE; }
struct { yylval = "STRUCT"; return STRUCT; }
if { yylval = "IF"; return IF; }
else { yylval = "ELSE"; return ELSE; }
while { yylval = "WHILE"; return WHILE; }
return { yylval = "RETURN"; return RETURN; }
\\x[^0-9a-fA-F]* {
printf("Error type A at Line %d: unknown lexeme %s\n", yylineno, yytext);

int|float|char {
yylval.node = new Node("TYPE",strdup(yytext));
return TYPE; }

\'(\\x{valid_hex_int}{2}|.)\' {
yylval.node = new Node("CHAR",strdup(yytext));
return CHAR;
}

"struct" {
yylval.node = new Node("STRUCT");
return STRUCT; }

"if" {
yylval.node = new Node("IF");
return IF;
}

"else" {
yylval.node = new Node("ELSE");
return ELSE;
}

"while" {
yylval.node = new Node("WHILE");
return WHILE;
}

"return" {
yylval.node = new Node("RETURN");
return RETURN;
}

";" {
yylval.node = new Node("SEMI");
return SEMI;
}
"," {
yylval.node = new Node("COMMA");
return COMMA;
}
"==" {
yylval.node = new Node("EQ");
return EQ;
}
"<=" {
yylval.node = new Node("LE");
return LE;
}
">=" {
yylval.node = new Node("GE");
return GE;
}
"!=" {
yylval.node = new Node("NE");
return NE;
}
"=" {
yylval.node = new Node("ASSIGN");
return ASSIGN;
}
'([^']|\\{valid_hex_int})' { yylval = yytext; return CHAR; }
";" { yylval = "SEMI"; return SEMI; }
"," { yylval = "COMMA"; return COMMA; }
"==" { yylval = "EQ"; return EQ; }
"<=" { yylval = "LE"; return LE; }
">=" { yylval = "GE"; return GE; }
"!=" { yylval = "NE"; return NE; }
"=" { yylval = "ASSIGN"; return ASSIGN; }
"!" { yylval = "NOT"; return NOT; }
"<" { yylval = "LT"; return LT; }
">" { yylval = "GT"; return GT; }
"+" { yylval = "PLUS"; return PLUS; }
"-" { yylval = "MINUS"; return MINUS; }
"*" { yylval = "MUL"; return MUL; }
"/" { yylval = "DIV"; return DIV; }
"&&" { yylval = "AND"; return AND; }
"||" { yylval = "OR"; return OR; }
"(" { yylval = "LP"; return LP; }
")" { yylval = "RP"; return RP; }
"[" { yylval = "LB"; return LB; }
"]" { yylval = "RB"; return RB; }
"{" { yylval = "LC"; return LC; }
"}" { yylval = "RC"; return RC; }
[_a-zA-Z][_0-9a-zA-Z]* { yylval = strdup(yytext); return ID; }
({valid_decimal_int})\.[0-9]+ { yylval = strdup(yytext); return FLOAT; }
{valid_decimal_int}|0{valid_hex_int} { yylval = strdup(yytext); return INT; /* not include minus number */ }
"!" {
yylval.node = new Node("NOT");
return NOT;
}
"<" {
yylval.node = new Node("LT");
return LT;
}
">" {
yylval.node = new Node("GT");
return GT;
}
"+" {
yylval.node = new Node("PLUS");
return PLUS;
}
"-" {
yylval.node = new Node("MINUS");
return MINUS;
}
"*" {
yylval.node = new Node("MUL");
return MUL;
}
"/" {
yylval.node = new Node("DIV");
return DIV;
}
"&&" {
yylval.node = new Node("AND");
return AND;
}
"||" {
yylval.node = new Node("OR");
return OR;
}
"(" {
yylval.node = new Node("LP");
return LP;
}
")" {
yylval.node = new Node("RP");
return RP;
}
"[" {
yylval.node = new Node("LB");
return LB;
}
"]" {
yylval.node = new Node("RB");
return RB;
}
"{" {
yylval.node = new Node("LC");
return LC;
}
"}" {
yylval.node = new Node("RC");
return RC;
}

[_a-zA-Z][_0-9a-zA-Z]* {
yylval.node = new Node("ID",strdup(yytext));
return ID; }

({valid_decimal_int})\.[0-9]+ {
yylval.node = new Node("FLOAT", (float)atof(yytext));
return FLOAT; }

{valid_decimal_int}|0{valid_hex_int} {
yylval.node = new Node("INT", atoi(yytext));
return INT; /* not include minus number */ }

"." {
yylval.node = new Node("DOT");
return DOT; }

\\x(0|[1-9a-f][0-9a-f]*)[^0-9a-fA-F] {
printf("Error type A at Line %d: unknown lexeme %s\n", yylineno, strdup(yytext));
}
"." { yylval = "DOT"; return DOT; }

0[Xx]([^0-9a-fA-F]|([1-9a-fA-F][0-9a-fA-F]*[^0-9a-fA-F]+)) {
printf("Error type A at Line %d: unknown lexeme %s\n", yylineno, strdup(yytext));
}
[$@] {
printf("Error type A at Line %d: unknown lexeme %s\n", yylineno, strdup(yytext));
}
\\x[^0-9a-fA-F]* {
printf("Error type A at Line %d: unknown lexeme %s\n", yylineno, yytext);
}

\n { yycolno = 1;}

" " {}
%%


131 changes: 66 additions & 65 deletions phase1/myOutput/output1.txt
Original file line number Diff line number Diff line change
@@ -1,65 +1,66 @@
Program (1)
ExtDefList (1)
ExtDef (1)
Specifier (1)
TYPE: int
FunDec (1)
ID: test_1_r01
LP
VarList (1)
ParamDec (1)
Specifier (1)
TYPE: int
VarDec (1)
ID: a
COMMA
VarList (1)
ParamDec (1)
Specifier (1)
TYPE: int
VarDec (1)
ID: b
RP
CompSt (2)
LC
StmtList (3)
Stmt (3)
Exp (3)
Exp (3)
ID: c
ASSIGN
Exp (3)
CHAR: 'c'
SEMI
Stmt (4)
IF
LP
Exp (4)
Exp (4)
ID: a
GT
Exp (4)
ID: b
RP
Stmt (5)
CompSt (5)
LC
StmtList (6)
Stmt (6)
RETURN
Exp (6)
ID: a
SEMI
RC
ELSE
Stmt (9)
CompSt (9)
LC
StmtList (10)
Stmt (10)
RETURN
Exp (10)
ID: b
SEMI
RC
RC
Program (1)
ExtDefList (1)
ExtDef (1)
Specifier (1)
TYPE: int
FunDec (1)
ID: test_1_r01
LP
VarList (1)
ParamDec (1)
Specifier (1)
TYPE: int
VarDec (1)
ID: a
COMMA
VarList (1)
ParamDec (1)
Specifier (1)
TYPE: int
VarDec (1)
ID: b
RP
CompSt (2)
LC
StmtList (3)
Stmt (3)
Exp (3)
Exp (3)
ID: c
ASSIGN
Exp (3)
CHAR: 'c'
SEMI
StmtList (4)
Stmt (4)
IF
LP
Exp (4)
Exp (4)
ID: a
GT
Exp (4)
ID: b
RP
Stmt (5)
CompSt (5)
LC
StmtList (6)
Stmt (6)
RETURN
Exp (6)
ID: a
SEMI
RC
ELSE
Stmt (9)
CompSt (9)
LC
StmtList (10)
Stmt (10)
RETURN
Exp (10)
ID: b
SEMI
RC
RC
1 change: 0 additions & 1 deletion phase1/myOutput/output10.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
Error type B at Line 5: Missing semicolon int
7 changes: 3 additions & 4 deletions phase1/myOutput/output11.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Error type A at Line 4: unknown lexeme 0x77G;
Error type A at Line 4: unknown lexeme 0x77G;
int _wrong_h
Error type A at Line 5: unknown lexeme 0xCS;
Error type A at Line 5: unknown lexeme 0xCS;
int _right_h
Error type A at Line 6: unknown lexeme 0xdeadbeef;
Error type A at Line 6: unknown lexeme 0xdeadbeef;

Error type B at Line 6: Missing semicolon char
3 changes: 1 addition & 2 deletions phase1/myOutput/output12.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
Error type A at Line 5: unknown lexeme 0xFFF;
Error type A at Line 5: unknown lexeme 0xFFF;

Error type B at Line 5: Missing semicolon char
Loading

0 comments on commit 07f5089

Please sign in to comment.