Skip to content
Snippets Groups Projects
Commit 4430289f authored by zhaochaoyi's avatar zhaochaoyi
Browse files

fix bug AND and OR %left in IF

parent a3fcd93a
No related branches found
No related tags found
No related merge requests found
Showing
with 470 additions and 181 deletions
Program (1)
ExtDefList (1)
ExtDef (1)
Specifier (1)
TYPE: int
FunDec (1)
ID: main
LP
RP
CompSt (1)
LC
DefList (2)
Def (2)
Specifier (2)
TYPE: int
DecList (2)
Dec (2)
VarDec (2)
ID: a
COMMA
DecList (2)
Dec (2)
VarDec (2)
ID: b
COMMA
DecList (2)
Dec (2)
VarDec (2)
ID: c
SEMI
StmtList (3)
Stmt (3)
IF
LP
Exp (3)
Exp (3)
Exp (3)
Exp (3)
ID: a
RELOP
Exp (3)
INT: 1
OR
Exp (3)
Exp (3)
ID: b
RELOP
Exp (3)
INT: 2
AND
Exp (3)
Exp (3)
ID: c
RELOP
Exp (3)
INT: 3
RP
Stmt (3)
CompSt (3)
LC
StmtList (4)
Stmt (4)
RETURN
Exp (4)
INT: 0
SEMI
RC
StmtList (6)
Stmt (6)
IF
LP
Exp (6)
Exp (6)
Exp (6)
Exp (6)
ID: a
RELOP
Exp (6)
INT: 1
AND
Exp (6)
Exp (6)
ID: b
RELOP
Exp (6)
INT: 2
OR
Exp (6)
Exp (6)
ID: c
RELOP
Exp (6)
INT: 3
RP
Stmt (6)
CompSt (6)
LC
StmtList (7)
Stmt (7)
RETURN
Exp (7)
INT: 0
SEMI
RC
RC
\ No newline at end of file
......@@ -50,13 +50,16 @@
#define Treedebug(format, ...) {;}
#endif
#define panic_on(A,expr) \
do { \
if(!(expr)) { break;} \
Log(A); \
assert(0); \
} while(1)
#ifndef FINAL
#define panic_on(A,expr) \
do { \
if(!(expr)) { break;} \
Log(A); \
assert(0); \
} while(1)
#else
#define panic_on(A,expr) (expr);
#endif
#define panic(A) panic_on(A,1)
......
......@@ -32,7 +32,7 @@ int main(int argc,char *argv[]) {
fclose(f);
semantic_check->init();
if (syntax == 0) {
//tree->traverse(tree->root,0);
tree->traverse(tree->root,0);
semantic_check->main(tree->root);
//test->main();
......@@ -55,6 +55,7 @@ static void free_Traverse(Node_t * cur) {
}
static void end_free() {
#ifndef FINAL
//yylex bison自己会申请空间,但是不释放,共计3次 16458 bytes
symbol_stack->pop();//先pop
for(int i = 0;i < symbol_table->table_size;i++) {
......@@ -65,4 +66,5 @@ static void end_free() {
free(symbol_table->table);
free_Traverse(tree->root);
panic_on("Error",symbol_table->cnt != 0);
#endif
}
No preview for this file type
......@@ -127,13 +127,13 @@ static unit_t * Semantic_Check_Creat_Node(char * name,Type * type,int line) {//
static int Semantic_Check_Insert_Node(unit_t * cur) {
unit_t * find = symbol_table->find(cur->name);
if(nodeop->IsStructDef(cur)) {
cur->deep = 1;
if(find) {
// cur->deep = 1;
if(find && find->deep == symbol_stack->stack_size) {
ErrorHandling(16,cur->line,cur->name);
nodeop->delete(cur,INFONODE);
return 0;
} else {
symbol_table->insert_struct(cur);
symbol_table->insert(cur);
return 1;
}
} else if(find && find->deep == symbol_stack->stack_size) {
......@@ -195,7 +195,7 @@ static Type * Semantic_Check_Specifier(Node_t * root) {
} else if(type(root->lchild->lchild,"STRUCT")) {
if(type(root->lchild->rchild,"RC")) {//是结构体的定义
ret = Semantic_Check_StructSpecifier(root->lchild);
panic_on("Wrong struct",ret != NULL && ret->kind != STRUCTURE);
//panic_on("Wrong struct",ret != NULL && ret->kind != STRUCTURE);
} else {
unit_t * temp = symbol_table->find(root->lchild->lchild->right->lchild->text);
if(temp != NULL && nodeop->IsStructDef(temp)) {
......@@ -255,6 +255,13 @@ static void Semantic_Check_ExtDef(Node_t * root) {
while (temp) {
unit_t * var = Semantic_Check_Creat_Node(temp->name,type_ops->type_copy(temp->type),temp->line);
Semantic_Check_Insert_Node(var);
if(temp->type->kind == STRUCTURE) {
unit_t * find_struct = symbol_table->find(temp->type->u.structure->name);
if(!find_struct) {
unit_t * type_struct = Semantic_Check_Creat_Node(temp->type->u.structure->name,type_ops->type_copy(temp->type),temp->line);
Semantic_Check_Insert_Node(type_struct);
}
}
temp = temp->tail;
}//将函数参数加入符号表
Semantic_Check_CompSt(root->rchild);
......@@ -510,8 +517,7 @@ static void Semantic_Check_Exp(Node_t * root) {
if((!type_ops->type_equal(left_type,&Int_Type) && !type_ops->type_equal(left_type,&Float_Type))
|| (!type_ops->type_equal(right_type,&Int_Type) && !type_ops->type_equal(right_type,&Float_Type))) {
ErrorHandling(7,mid->line,empty);
}
if(!type_ops->type_equal(right_type,left_type)) {
} else if(!type_ops->type_equal(right_type,left_type)) {
ErrorHandling(7,mid->line,empty);
}
ret = left_type;
......
This diff is collapsed.
......@@ -61,7 +61,8 @@
%right ASSIGNOP
%left OR AND
%left OR
%left AND
%left RELOP
%left PLUS MINUS
......
// test compst
int main(){
int i;
i = 1;
while(i == 1){
float i;
i = 0.5;
while(i == 5){ // error 7
int i[4][3];
while(i[4][3] == 3){
struct{
int i;
}i;
while(i.i == 0.5){ // error 7
int i;
}
}
}
}i = 0.5; // error 5
{
int i;{
float i;
{
struct i{
int i;
}j;
}
}
}
{
float i;
}
}int i;
\ No newline at end of file
Error type 7 at Line 8: Type mismatched for operands "=="
Error type 7 at Line 14: Type mismatched for operands "=="
Error type 5 at Line 19: Type mismatched for assignment "="
int a, b;
struct A{
int a;
int b;
};
int func1(int a);
int func2(int b);
int func3(int a); // error 18
int func4(struct A b);
int func5(int a); // error 18
int func6(int a); // error 18
int func7(int b); // error 18
int func10(struct A{int a, b;} c){
int d;
}
int func11(struct A{int a[9], b;} c){
d = 1; // error 1;
}
int main(){
struct A c;
func1(a);
func8(a); // error 2
func4(c);
func5(b);
func6(c); // error 9;
func10(c);
func11(c); // error 9;
}
int func1(int a){
int b; int c;
}
int func2(int b){
int a; int c;
}
int func3(struct A a){ // error 19
int c;
}
int func4(struct A a){
int c;
}
int func5(int a);
int func5(int a);
int func8(int a){
int a, b; //error 3
}
int func9(int a){
int c, d;
}
Error type 1 at Line 19: Undefined variable "d"
Error type 5 at Line 19: Type mismatched for assignment "="
Error type 2 at Line 25: Undefined function "func8"
Error type 9 at Line 28: Function is not applicable for arguments ""
Error type 9 at Line 30: Function is not applicable for arguments ""
Error type 19 at Line 41: Inconsistent declaration of function "func3"
Error type 3 at Line 54: Redefined variable "a"
Error type 18 at Line 14: Undefined function "func7"
Error type 18 at Line 13: Undefined function "func6"
Error type 18 at Line 12: Undefined function "func5"
Error type 18 at Line 10: Undefined function "func3"
// test array;
int func(int a[10][10][10][10]){
int b[2][50];
a[10][10] = b;
a[10][10][10] = b; // error 5;
}
struct A{
int b[10][10][10][10];
};
int func2(struct A{int b[10][10][10][10];} a){
return 2;
}
int main(){
struct A a;
int b[20][5][10][10];
int c[20][5][10];
int d[20][5][5][5][5];
int e[2][10][10][10][10];
func(b);
func(b[10]); // error 9;
c + b; // error 7;
c + b[20]; // error 7;
c[1][1][1] + b[1][1][1][1];
func(d[1]); // error 9;
func(e[0]);
func(a.b);
func(a.b[0]); // error 9;
func2(a);
}
\ No newline at end of file
Error type 5 at Line 5: Type mismatched for assignment "="
Error type 9 at Line 23: Function is not applicable for arguments ""
Error type 7 at Line 24: Type mismatched for operands ""
Error type 7 at Line 25: Type mismatched for operands ""
Error type 9 at Line 30: Function is not applicable for arguments ""
int main()
{
int i = 0;
j = i + 1;
}
\ No newline at end of file
Error type 1 at Line 4: Undefined variable "j"
Error type 5 at Line 4: Type mismatched for assignment "="
int main()
{
int i;
i[0];
}
\ No newline at end of file
Error type 10 at Line 4: Variable is not an array "["
int main()
{
int i;
i(10);
}
\ No newline at end of file
Error type 11 at Line 4: Not a function "i"
int main()
{
int i[10];
i[1.5] = 10;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment