Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions examples/error_tests/const_test.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
13 changes: 7 additions & 6 deletions examples/error_tests/scope_error.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
3 changes: 2 additions & 1 deletion examples/modules/comments_test.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// comment out
public class Main {
public static void main(String[] args) {
// inline comment

/*
multiple lines
comment
*/
}
}
//comment out 2
5 changes: 5 additions & 0 deletions examples/modules/input_test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class Main {
public static void main(String[] args) {
Scanner my_input = new Scanner(System.in);
}
}
4 changes: 2 additions & 2 deletions examples/modules/multiple_declaration.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ public class Main {

public static void funbar(int d) {
int x;
// int d;
// int x;
int d;
int x;
}

}
10 changes: 10 additions & 0 deletions examples/modules/nested_for_test.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

}
}
7 changes: 5 additions & 2 deletions examples/modules/print_test.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
7 changes: 3 additions & 4 deletions examples/modules/syntax_error_test.java
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
1 change: 1 addition & 0 deletions examples/modules/types_test.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
public class Main {
public static final int x;
public static void main(String[] args) {
int var1;
double var2;
Expand Down
44 changes: 24 additions & 20 deletions java2cpp.y
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
;

Expand Down Expand Up @@ -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"); }
;

Expand Down Expand Up @@ -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);
}

}
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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){
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -964,13 +969,12 @@ int console_msg(){

fclose(fp);

printf("Check the translation file: %s\n",CFILE);
print_error_counter();

return 0;
}

void write_to_file(char *s){
fputs(s,fp_aux);
printf("%s",s);
//printf("%s",s);
}