diff --git a/examples/error_tests/const_test.java b/examples/error_tests/const_test.java index 50bc2c4..bf42b18 100644 --- a/examples/error_tests/const_test.java +++ b/examples/error_tests/const_test.java @@ -1,11 +1,7 @@ public class Main { public static final int x; - - - public static void funbar(int num1, char num2) { int d=x+1; x=2; } - } diff --git a/examples/error_tests/scope_error.java b/examples/error_tests/scope_error.java index 5b5c56f..4144fa3 100644 --- a/examples/error_tests/scope_error.java +++ b/examples/error_tests/scope_error.java @@ -1,13 +1,14 @@ public class Main { public static int x; - public static void funbar(int num1, char num2) { - int x; - } -public static void funbar2(int d) { - int c=d+2; - } private static void bar() { int c=x+1; c=d+2 ; + if(100>10){ + int d; + if(true){ + int g; + } + d=x+g; + } } } diff --git a/examples/modules/comments_test.java b/examples/modules/comments_test.java index fe061c9..88f0cc3 100644 --- a/examples/modules/comments_test.java +++ b/examples/modules/comments_test.java @@ -1,10 +1,11 @@ +// comment out public class Main { public static void main(String[] args) { // inline comment - /* multiple lines comment */ } } +//comment out 2 \ No newline at end of file diff --git a/examples/modules/input_test.java b/examples/modules/input_test.java new file mode 100644 index 0000000..3bc38f4 --- /dev/null +++ b/examples/modules/input_test.java @@ -0,0 +1,5 @@ +public class Main { + public static void main(String[] args) { + Scanner my_input = new Scanner(System.in); + } +} diff --git a/examples/modules/multiple_declaration.java b/examples/modules/multiple_declaration.java index c5f4628..71b6eba 100644 --- a/examples/modules/multiple_declaration.java +++ b/examples/modules/multiple_declaration.java @@ -3,8 +3,8 @@ public class Main { public static void funbar(int d) { int x; - // int d; - // int x; + int d; + int x; } } \ No newline at end of file diff --git a/examples/modules/nested_for_test.java b/examples/modules/nested_for_test.java new file mode 100644 index 0000000..c4ca3bd --- /dev/null +++ b/examples/modules/nested_for_test.java @@ -0,0 +1,10 @@ +public class Main { + public static void main(String[] args) { + for (int i = 0; i < 5; i++) { + for (int i = 0; i < 5; i++) { + // System.out.println(i); + } + } + + } +} \ No newline at end of file diff --git a/examples/modules/print_test.java b/examples/modules/print_test.java index 973e5ad..500ff84 100644 --- a/examples/modules/print_test.java +++ b/examples/modules/print_test.java @@ -1,6 +1,9 @@ public class Main { public static void main(String[] args) { - System.out.println("something"); - System.out.print("something"); + System.out.print("hello"); + System.out.println(" world"); + + System.out.println("hello" + " world"); + System.out.print("hello" + " world"); } } diff --git a/examples/modules/syntax_error_test.java b/examples/modules/syntax_error_test.java index fdccc6c..1a5ff34 100644 --- a/examples/modules/syntax_error_test.java +++ b/examples/modules/syntax_error_test.java @@ -1,14 +1,13 @@ public class Main { public static void main(String[] args) { int a =3+6 - int b = a+2; - int c = 100; + int b = 2; + int c = 100+b; if(1=1){ // do something int s = } - int z - boolean b; + int i; for(i=0, i<90, i++){ //something } diff --git a/examples/modules/types_test.java b/examples/modules/types_test.java index b427485..1257953 100644 --- a/examples/modules/types_test.java +++ b/examples/modules/types_test.java @@ -1,4 +1,5 @@ public class Main { + public static final int x; public static void main(String[] args) { int var1; double var2; diff --git a/java2cpp.y b/java2cpp.y index 7bb01ec..fc9964e 100644 --- a/java2cpp.y +++ b/java2cpp.y @@ -136,7 +136,7 @@ char var_name[MAX_NAME_LEN]; %% -program : { fp_aux = fopen(AUXFILE,"w"); print_init();} HAS_COMMENT MAIN_CLASS LC {push_scope("global"); write_to_file("\n/* start Main Class */\n\n"); } STATEMENTS RC {pop_scope(); write_to_file("\n/* end Main Class */\n"); verify_fun_table(); fclose(fp_aux); merge_files();}{fp_aux = fopen(CFILE,"a");}HAS_COMMENT{fclose(fp_aux); console_msg(); exit(0); } +program : { fp_aux = fopen(AUXFILE,"w"); } HAS_COMMENT MAIN_CLASS LC {push_scope("global"); write_to_file("\n/* start Main Class */\n\n"); } STATEMENTS RC {pop_scope(); write_to_file("\n/* end Main Class */\n"); verify_fun_table(); fclose(fp_aux); merge_files();}{fp_aux = fopen(CFILE,"a");}HAS_COMMENT{fclose(fp_aux); console_msg(); exit(0); } | /* Empty file */ { write_to_file("\n"); exit(2); } ; @@ -354,7 +354,7 @@ SEMICOLON_NOT_COMA : SEMICOLON { write_to_file(";"); } MUST_EXPRESSION : EXPRESION | VAR ASSIGNMENT { write_to_file(yylval.var_name); write_to_file("="); } EXPRESION { } - | EXPRESION ASSIGNMENT { write_to_file("="); } EXPRESION { strcat(syntax_errors,"Syntax error: expected '==' operator\n"); } + | EXPRESION ASSIGNMENT { write_to_file("="); } EXPRESION { strcat(syntax_errors,"Syntax error: cannot assign to an expression. Expected '==' operator\n"); } | /*empty*/ { strcat(syntax_errors,"Syntax error: expected expression\n"); } ; @@ -385,9 +385,10 @@ int check_constant(char var[MAX_NAME_LEN]){ } if(!is_correct){ char formatted_str[256]; - sprintf(formatted_str,"\nVariable %s was declared as a const \n",var); - write_to_file(formatted_str); - yyerror(""); + sprintf(formatted_str,"Variable %s was declared as a const \n",var); + //write_to_file(formatted_str); + //yyerror(""); + yyerror(formatted_str); } } @@ -409,10 +410,11 @@ int verify_scope(char var[MAX_NAME_LEN]){ if(!found){ char formatted_str[256]; - sprintf(formatted_str,"\nVariable %s was not declared in the scope \n",var); - write_to_file(formatted_str); + sprintf(formatted_str,"Variable %s was not declared in the scope \n",var); + //write_to_file(formatted_str); //print_table_symbols(); - yyerror(""); + //yyerror(""); + yyerror(formatted_str); //exit(0); } @@ -443,9 +445,10 @@ void insert_to_table(char var[MAX_NAME_LEN], int type) } else { char formatted_str[256]; - sprintf(formatted_str,"\nMultiple declaration of variable %s \n",var); - write_to_file(formatted_str); - yyerror(""); + sprintf(formatted_str,"Multiple declaration of variable %s \n",var); + //write_to_file(formatted_str); + //yyerror(""); + yyerror(formatted_str); //exit(0); } } @@ -464,9 +467,10 @@ void insert_argument_var(char var[MAX_NAME_LEN]){ insert_type_param(type); }else{ char formatted_str[256]; - sprintf(formatted_str,"\nVariable not declare %s \n",var); - write_to_file(formatted_str); - yyerror(""); + sprintf(formatted_str,"Variable not declare %s \n",var); + //write_to_file(formatted_str); + //yyerror(""); + yyerror(formatted_str); } } void insert_type_param(int type){ @@ -525,7 +529,8 @@ void create_scope_name_and_push_it(){ void push_scope(char var[MAX_NAME_LEN] ){ if(stack_scope_counter == MAX_SCOPE){ printf("SCOPE STACK IS FULL"); - yyerror(""); + //yyerror(""); + yyerror("SCOPE STACK IS FULL"); exit(0); } strcpy(stack_scope[++stack_scope_counter],var); @@ -594,7 +599,7 @@ void print_type_error_warning(){ yyerror(type_cast_str_error); strcpy(type_cast_str_error,"\0"); }else if(left_val_type!=right_val_type && !casting_table.implicit[right_val_type][left_val_type]){ - printf("\nl: %d, r: %d\n", left_val_type, right_val_type); + //printf("\nl: %d, r: %d\n", left_val_type, right_val_type); char aux2[512]; char *sty1 = type_to_str(48+right_val_type); char * sty2 = type_to_str(48+left_val_type); @@ -932,9 +937,9 @@ int merge_files(){ void print_error_counter(){ if(error_counter>0){ - printf("\nErrors found: %d\n\nTRANSLATION FAILED !!!\n",error_counter); + printf("\nErrors found: %d\n\nCheck the translation filefor more details: %s\n\nTRANSLATION FAILED !!!\n",error_counter,CFILE); }else{ - printf("\nErrors found: %d\n\nTRANSLATION SUCCESSFUL !!!\n",error_counter); + printf("\nErrors found: %d\n\nCheck the translation file: %s\n\nTRANSLATION SUCCESSFUL !!!\n",error_counter,CFILE); } } @@ -964,7 +969,6 @@ int console_msg(){ fclose(fp); - printf("Check the translation file: %s\n",CFILE); print_error_counter(); return 0; @@ -972,5 +976,5 @@ int console_msg(){ void write_to_file(char *s){ fputs(s,fp_aux); - printf("%s",s); + //printf("%s",s); } \ No newline at end of file