Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 35.90 KB | None | 0 0
  1. package com.example.compiler;
  2.  
  3. import analysis.DepthFirstAdapter;
  4. import node.*;
  5.  
  6. import java.util.*;
  7.  
  8. public class SemanticAnalyzer extends DepthFirstAdapter {
  9.     class data{
  10.         String identifier;    // name of identifier
  11.         int value;            // value of identifier
  12.         String type;          // type of identifier(int, char)
  13.         String id_type;       // identifier type (var, fun, array)!!!
  14.         String funarg;        // if variable is function argument or not
  15.         String refer;         // reference variable
  16.         int scope;            // scope variable
  17.         List<String> function_parameters;    // keeps the type of a fuction's parameters
  18.         List<Integer> array_dimensions;          // krataei to megethos kathe diastashs tou pinaka
  19.  
  20.         public data(String id, int val, String type, String id_type, String func, String ref, int scope, List<String> params, List<Integer> array_d){
  21.             this.identifier = id;   // name of identifier
  22.             this.value = val;       // value of identifier
  23.             this.type = type;       // type of identifier(int, char)
  24.             this.id_type = id_type; // identifier type (var, fun)!!!
  25.             this.funarg = func;     // if variable is function argument or not
  26.             this.refer = ref;       // reference variable
  27.             this.scope = scope;     // scope variable
  28.             if(params != null) {
  29.                 this.function_parameters = new ArrayList<>();
  30.                 for(String temp : params){
  31.                     function_parameters.add(temp);
  32.                 }
  33.             }
  34.             if(array_d != null) {
  35.                 this.array_dimensions = new ArrayList<>();
  36.                 for(int temp : array_d){
  37.                     this.array_dimensions.add(temp);
  38.                 }
  39.             }
  40.         }
  41.         public int get_scope(){ return this.scope; }
  42.         public String get(){
  43.             return this.identifier;
  44.         }
  45.         public int geti(){
  46.             return this.value;
  47.         }
  48.         public String get_type(){
  49.             if( this.type.equals("int [ ]") || this.type.equals("int []") || this.type.equals("int[]")){
  50.                 return "int";
  51.             }
  52.             else if(this.type.equals("char [ ]") || this.type.equals("char []") || this.type.equals("char[]")){
  53.                 return "char";
  54.             }
  55.             return this.type;
  56.         }
  57.         public String get_id_type(){
  58.             return this.id_type;
  59.         }
  60.         public List<String> get_function_parameters() {
  61.             List<String> temp = new ArrayList<>();
  62.             if( this.function_parameters!= null ) {
  63.                 for (String temp1 : this.function_parameters) {
  64.                     String[] temp2 = temp1.split("\\[");
  65.                     temp.add(temp2[0].trim());
  66.                 }
  67.                 return temp;
  68.             }
  69.             return this.function_parameters;
  70.         }
  71.         public List<Integer> get_dimensions(){
  72.             return this.array_dimensions;
  73.         }
  74.  
  75.         @Override
  76.         public String toString() {
  77.             //return ("ID :"+ this.get() +" VALUE: "+ this.geti() +" TYPE: "+ this.get_type() +" ID_TYPE: "+ get_id_type()+ "\n");
  78.             if (this.id_type.equals("var")) {
  79.                 return ("Variable :"+ this.get() +" TYPE: "+ this.get_type() +" SCOPE "+this.get_scope()+"\n");
  80.             }
  81.             else if(this.id_type.equals("fun")){
  82.                 return ("Function :"+ this.get() +" TYPE: "+ this.get_type() +" SCOPE "+this.get_scope()+" Parameters "+this.get_function_parameters()+"\n");
  83.             }
  84.             else if(this.id_type.equals("array")){
  85.                 return ("Array :"+ this.get() +" TYPE: "+ this.get_type() +" SCOPE "+this.get_scope()+" Array Dimensions "+this.get_dimensions()+"\n");
  86.             }
  87.             return ("ID :"+ this.get() +" TYPE: "+ this.get_type() +" SCOPE "+this.get_scope()+" Parameters "+this.get_function_parameters()+" Array Dimensions "+this.get_dimensions()+"\n");
  88.         }
  89.  
  90.         @Override
  91.         public boolean equals(Object object){
  92.  
  93.             boolean s = false;
  94.  
  95.             if((identifier.trim()).equals( ((data)object).identifier) && variable_def == true && scope == ((data)object).scope) {
  96.                 s = true;
  97.                 return s;
  98.             }
  99.             else if((identifier.trim()).equals( ((data)object).identifier) && variable_def == true && scope != ((data)object).scope){
  100.                 return s;
  101.             }
  102.             else if((identifier.trim()).equals( ((data)object).identifier) && function_def == true && parameters_flag==true && scope == ((data)object).scope) {
  103.                 s = true;
  104.                 return s;
  105.             }
  106.             else if((identifier.trim()).equals( ((data)object).identifier) && function_def == true && parameters_flag==true && scope != ((data)object).scope){
  107.                 return s;
  108.             }
  109.             else if((identifier.trim()).equals( ((data)object).identifier) && scope >= ((data)object).scope){
  110.                 s = true;
  111.                 return s;
  112.             }
  113.  
  114.             return s;
  115.         }
  116.  
  117.     }
  118.  
  119.     List<data> symbol_table1 = new ArrayList(); // create new table
  120.  
  121.  
  122.     private List<data> paramTypes;
  123.     private List<String> fuction_params_type;
  124.     private List<String> exp_types;
  125.     private int idscope = 0;
  126.  
  127.     boolean scope_flag = false;
  128.     boolean function_flag = false;
  129.     boolean fun_decl_flag = false;
  130.     boolean ass_flag = false;
  131.  
  132.     boolean comp1_flag = false;
  133.     private List<String> comp1_exp_types;
  134.  
  135.     boolean comp2_flag = false;
  136.     private List<String> comp2_exp_types;
  137.  
  138.     boolean comp3_flag = false;
  139.     private List<String> comp3_exp_types;
  140.  
  141.     boolean comp4_flag = false;
  142.     private List<String> comp4_exp_types;
  143.  
  144.     boolean comp5_flag = false;
  145.     private List<String> comp5_exp_types;
  146.  
  147.     boolean comp6_flag = false;
  148.     private List<String> comp6_exp_types;
  149.  
  150.     // typoi parametrwn sthn klhsh synarthsewn
  151.     boolean fuction_call_param_flag = false ;       // klhsh sunarthshs
  152.     private List<String> fuction_call_param;
  153.  
  154.  
  155.     boolean function_def = false; // an brisketai se fun definiton ( elegxos sto equals tou data )
  156.     boolean variable_def = false; // an brisketai se variable definition ( elegxos sto equals tou data )
  157.  
  158.     boolean l_value1_flag = false ; // array ws metablhth sunarthshs
  159.  
  160.     private List<String> simple_expr_list;
  161.     private List<String> simple_expr_list_in_lvalue1;
  162.  
  163.     private List<String> exp_com_list;
  164.     boolean com_expr_flag = false;
  165.  
  166.     boolean parameters_flag = false ;
  167.  
  168.     boolean rigth_part_of_assigment_flag = false; // true an ola ta stoixeia sta de3ia tou assigment einai idiou type kai xrhsh ths meta gia an einai sumabata me to aristero meros
  169.  
  170.     public SemanticAnalyzer(){
  171.         List<String> list = new ArrayList<>();
  172.         list.add("int");
  173.         data puti = new data("puti",0,"nothing","fun","no",null,-1,list,null);
  174.         list.clear();
  175.         list.add("char");
  176.         data putc = new data("putc",0,"nothing","fun","no",null,-1,list,null);
  177.         list.clear();
  178.         list.add("char[]");
  179.         data puts = new data("puts",0,"nothing","fun","no",null,-1,list,null);
  180.         list.clear();
  181.  
  182.         symbol_table1.add(puti);
  183.         symbol_table1.add(putc);
  184.         symbol_table1.add(puts);
  185.  
  186.         data geti = new data("geti",0,"int","fun","no",null,-1, list,null);
  187.         data getc = new data("getc",0,"char","fun","no",null,-1,list,null);
  188.         list.add("int");
  189.         list.add("char[]");
  190.         data gets = new data("gets",0,"nothing","fun","no",null,-1,list,null);
  191.         list.clear();
  192.  
  193.         symbol_table1.add(geti);
  194.         symbol_table1.add(getc);
  195.         symbol_table1.add(gets);
  196.  
  197.         list.add("int");
  198.         data abs = new data("abs",0,"int","fun","no",null,-1,list, null);
  199.         list.clear();
  200.         list.add("char");
  201.         data ord = new data("ord",0,"int","fun","no",null,-1, list,null);
  202.         list.clear();
  203.         list.add("int");
  204.         data chr = new data("chr",0,"char","fun","no",null,-1, list,null);
  205.         list.clear();
  206.  
  207.         symbol_table1.add(abs);
  208.         symbol_table1.add(ord);
  209.         symbol_table1.add(chr);
  210.  
  211.         list.add("char[]");
  212.         data strlen = new data("strlen",0,"int","fun","no",null,-1, list,null);
  213.         list.clear();
  214.         list.add("char[]");
  215.         list.add("char[]");
  216.         data strcmp = new data("strcmp",0,"int","fun","no",null,-1,list,null);
  217.         data strcpy = new data("strcpy",0,"nothing","fun","no",null,-1,list,null);
  218.         data strcat = new data("strcat",0,"nothing","fun","no",null,-1,list,null);
  219.         list.clear();
  220.  
  221.         symbol_table1.add(strlen);
  222.         symbol_table1.add(strcmp);
  223.         symbol_table1.add(strcpy);
  224.         symbol_table1.add(strcat);
  225.  
  226.     }
  227. /******************************************/
  228. //         Function Definition            //
  229. /******************************************/
  230.     @Override
  231.     public void inAFuncDef(AFuncDef node){
  232.         //Scope
  233.         scope_flag = true;
  234.         function_def = true;
  235.     }
  236.  
  237.     @Override
  238.     public void outAFuncDef(AFuncDef node){
  239.         //Scope
  240.         Iterator<data> iter = symbol_table1.iterator();
  241.         Print();
  242.         while (iter.hasNext()) {
  243.             data str = iter.next();
  244.  
  245.             if (idscope == str.get_scope())
  246.                 iter.remove();
  247.         }
  248.         Print();
  249.         idscope--;
  250.         function_def = false;
  251.     }
  252. /******************************************/
  253. //         Function Declaretion           //
  254. /******************************************/
  255.     @Override
  256.     public void inAFuncDecl(AFuncDecl node){
  257.         fun_decl_flag = true;
  258.     }
  259.  
  260.     @Override
  261.     public void outAFuncDecl(AFuncDecl node){
  262.  
  263.     }
  264. /******************************************/
  265. //                ExpCalc                 //
  266. /******************************************/
  267.     @Override
  268.     public void inACalcExpr(ACalcExpr node){
  269.         simple_expr_list = new ArrayList<>();
  270.         simple_expr_list_in_lvalue1 = new ArrayList<>();     //lista me tous index twn pinakwn se assigment gia elegxo tou tupou
  271.     }
  272.     @Override
  273.     public void outACalcExpr(ACalcExpr node){
  274.         if(l_value1_flag == true){
  275.             String temp = simple_expr_list_in_lvalue1.get(0);
  276.  
  277.             for (String str : simple_expr_list_in_lvalue1) {
  278.                 if (temp.equals(str)) {
  279.                     //System.out.println("ok");
  280.                 }
  281.                 else {
  282.                     System.out.println("ERROR -" + temp + "-" + str + "-      CalcExpr   " + simple_expr_list_in_lvalue1);
  283.                     //System.exit(0);
  284.                 }
  285.             }
  286.             if(!temp.equals("int")){
  287.                 System.out.println("ERROR : Index of array must be int");
  288.             }
  289.         }
  290.         else {
  291.             String temp = simple_expr_list.get(0);
  292.  
  293.             for (String str : simple_expr_list) {
  294.                 if (temp.equals(str)) {
  295.                     rigth_part_of_assigment_flag = true;
  296.                     //System.out.println("ok");
  297.                 } else {
  298.                     rigth_part_of_assigment_flag = false;
  299.  
  300.                     System.out.println("ERROR -" + temp + "-" + str + "-      CalcExpr   " + simple_expr_list);
  301.                     break;
  302.                     //System.exit(0);
  303.                 }
  304.             }
  305.             /// gia tis sunarthseis
  306.             if (com_expr_flag == true && temp != null) {
  307.                 if (l_value1_flag != true) {
  308.                     exp_com_list.add(temp);
  309.  
  310.                 }
  311.             }
  312.         }
  313.         System.out.println("   --   >  "+simple_expr_list);
  314.     }
  315. /******************************************/
  316. //                ExpCom                  //
  317. /******************************************/
  318.     @Override
  319.     public void inAExpcom(AExpcom node){
  320.         com_expr_flag = true;
  321.         exp_com_list = new ArrayList<>();
  322.     }
  323.     @Override
  324.     public void outAExpcom(AExpcom node){
  325.         com_expr_flag = false;
  326.     }
  327. /******************************************/
  328. //         Function Call                  //
  329. /******************************************/
  330.     @Override
  331.     public void inAFuncCall(AFuncCall node) {
  332.         //System.out.println("FUNC_CALL : " + node.toString());
  333.         fuction_call_param_flag = true;
  334.         fuction_call_param = new ArrayList<>();
  335.  
  336.  
  337.         if(function_flag==true){
  338.  
  339.             TId id = node.getId();
  340.             String key = id.getText();
  341.             data temp = new data(key.trim(),0,"-","fun","no",null,idscope,null,null);
  342.             if(symbol_table1.contains(temp)){
  343.                 int index = symbol_table1.indexOf(temp);
  344.                 data temp1 = symbol_table1.get(index);
  345.                 String type = temp1.get_type();
  346.                 if(l_value1_flag == true){
  347.                     simple_expr_list_in_lvalue1.add(type.trim());
  348.                 }
  349.                 else {
  350.                     simple_expr_list.add(type.trim());
  351.                 }
  352.  
  353.                 int temp_scope = temp1.get_scope();
  354.                 if(temp_scope > idscope){
  355.                     System.out.println("ERROR : Function "+key.trim()+" out of scope");
  356.                     //System.exit(0);
  357.                 }
  358.                 if(ass_flag == true) {
  359.                     function_flag = false;
  360.                 }
  361.  
  362.                 if(comp1_flag == true){
  363.                     comp1_exp_types.add(type.trim());
  364.                 }
  365.                 if(comp2_flag == true){
  366.                     comp2_exp_types.add(type.trim());
  367.                 }
  368.                 if(comp3_flag == true){
  369.                     comp3_exp_types.add(type.trim());
  370.                 }
  371.                 if(comp4_flag == true){
  372.                     comp4_exp_types.add(type.trim());
  373.                 }
  374.                 if(comp5_flag == true){
  375.                     comp5_exp_types.add(type.trim());
  376.                 }
  377.                 if(comp6_flag == true){
  378.                     comp6_exp_types.add(type.trim());
  379.                 }
  380.             }
  381.         }
  382.  
  383.     }
  384.     @Override
  385.     public void outAFuncCall(AFuncCall node) {
  386.         TId ident = node.getId();
  387.         String key = ident.getText();
  388.         data temp = new data(key.trim(),5, "fun", "hg", "-", null, idscope,null,null);
  389.  
  390.         if (symbol_table1.contains(temp)) {
  391.             //System.out.println("ERROR : Identifier already defined - Function Call");
  392.             //System.exit(0);
  393.         }
  394.         else {
  395.             System.out.println("ERROR : Function "+key.trim()+" is not defined");
  396.             //System.exit(0);
  397.         }
  398.         data temp1 = new data(key,0,"-","fun","no",null,idscope,null,null);
  399.         int index = symbol_table1.indexOf(temp1);
  400.         if( index != -1) {
  401.             data temp2 = symbol_table1.get(index);
  402.             List<String> temp_func_list = temp2.get_function_parameters();
  403.             if (exp_com_list != null && temp_func_list != null) {
  404.                 if (exp_com_list.size() < temp_func_list.size()) {
  405.                     System.out.println("ERROR : Less parameters were given in function " + key + " call : " + exp_com_list.size() + " - " + temp_func_list.size());
  406.                     //System.exit(0);
  407.                 } else if (exp_com_list.size() > temp_func_list.size()) {
  408.                     System.out.println("ERROR : More parameters were given in function " + key + " call : " + exp_com_list.size() + " - " + temp_func_list.size());
  409.                     //System.exit(0);
  410.                 } else {
  411.                     if (!(temp_func_list.equals(exp_com_list))) {
  412.                         System.out.println("ERROR : Incompatible type of arguments were given in function " + key);
  413.                         //System.exit(0);
  414.                     }
  415.                 }
  416.             }
  417.         }
  418.         fuction_call_param_flag = false;
  419.     }
  420. /******************************************/
  421. //        Variable Definiton              //
  422. /******************************************/
  423.     @Override
  424.     public void inAVarDef(AVarDef node) {
  425.         //System.out.println("Var def: " + node.toString());
  426.         variable_def = true;
  427.     }
  428.     @Override
  429.     public void outAVarDef(AVarDef node) {
  430.         // type of variable
  431.         PType type = node.getType();
  432.         String t = type.toString();
  433.         String[] new_type = t.split("\\[");
  434.  
  435.         String temp_id_type = "var";
  436.         List<Integer> temp_dimensions_list = null;
  437.         System.out.println(temp_dimensions_list );
  438.         if(new_type.length>1) {
  439.             temp_dimensions_list = new ArrayList<>();
  440.             temp_id_type = "array";
  441.         }
  442.         for(int i=1;i < new_type.length;i++) {
  443.             new_type[i] = new_type[i].replaceAll("\\D+", "");
  444.             Integer x = Integer.valueOf(new_type[i]);
  445.             System.out.println("dimension "+i+" megethos "+x);
  446.             temp_dimensions_list.add(x);
  447.         }
  448.  
  449.         //System.out.println(" -> "+new_type.length);
  450.         t = new_type[0];
  451.         // name of variable
  452.         TId ident = node.getId();
  453.         String key = ident.getText();
  454.  
  455.         data temp = new data(key.trim(), 0, t.trim(), temp_id_type, "no", "no", idscope, null,temp_dimensions_list);
  456.         if (symbol_table1.contains(temp)) {
  457.             System.out.println("ERROR : Identifier already defined - Variable Definition");
  458.             //System.exit(0);
  459.         }
  460.         else{
  461.             symbol_table1.add(temp);
  462.         }
  463.         for (PComid comid : node.getComid()) {
  464.             key = comid.toString();
  465.             temp = new data(key.trim(),0, t.trim(), temp_id_type, "no", "no", idscope,null,temp_dimensions_list);
  466.             if (symbol_table1.contains(temp)) { // report an error
  467.                 System.out.println("ERROR : Identifier already defined - Variable Definition");
  468.                 //System.exit(0);
  469.             }
  470.             symbol_table1.add(temp);
  471.         }
  472.         variable_def = false;
  473.     }
  474. /******************************************/
  475. //             Parameters                 //
  476. /******************************************/
  477.     @Override
  478.     public void inAFparDef(AFparDef node) {
  479.         parameters_flag = true;
  480.     }
  481.     @Override
  482.     public void outAFparDef(AFparDef node) {
  483.         if(fun_decl_flag == true){ //(fun_decl_param_flag == true){
  484.             //System.out.println("Function Declaration Parameters");
  485.             fun_decl_flag = false; //fun_decl_param_flag = false;
  486.         }
  487.         else{
  488.             PFparType type = node.getFparType();
  489.             String t = type.toString();
  490.  
  491.             TId id = node.getId();
  492.             String key = id.getText();
  493.             data temp = new data(key.trim(),0, t.trim(), "var", "yes", null, idscope, null,null);
  494.             if(symbol_table1.contains(temp)){
  495.                 System.out.println("ERROR : Identifier "+key.trim()+" already defined - Function Parameters");
  496.                 //System.exit(0);
  497.             }
  498.             else{
  499.                 symbol_table1.add(temp);
  500.                 paramTypes.add(temp);
  501.                 fuction_params_type.add(t.trim());
  502.             }
  503.             for (PComid comid : node.getComid()) {
  504.                 key = comid.toString();
  505.                 temp = new data(key.trim(), 5, t.trim(), "var", "yes", null, idscope,null,null);
  506.                 if (symbol_table1.contains(temp)) {
  507.                     System.out.println("ERROR : Identifier " + key.trim() + " already defined - Function Parameters"+idscope);
  508.                     //System.exit(0);
  509.                 } else {
  510.                     symbol_table1.add(temp);
  511.                     paramTypes.add(temp);
  512.                     fuction_params_type.add(t.trim());
  513.                 }
  514.             }
  515.         }
  516.         parameters_flag = false;
  517.     }
  518. /******************************************/
  519. //                Header                  //
  520. /******************************************/
  521.     @Override
  522.     public void inAHeader(AHeader node) {
  523.         //System.out.println("Header : " + node.toString());
  524.         paramTypes = new ArrayList<>();
  525.         fuction_params_type = new ArrayList<>();
  526.  
  527.         if(scope_flag==true){
  528.             idscope++;
  529.             scope_flag = false;
  530.         }
  531.     }
  532.  
  533.     @Override
  534.     public void outAHeader(AHeader node) {
  535.         TId ident = node.getId();
  536.         String key = ident.getText();
  537.  
  538.         List<String> temp_l = new ArrayList();
  539.         if(!(paramTypes.equals(temp_l)) && (idscope-1)==0){
  540.             System.out.println("ERROR : Function "+key+" cannot have arguments");
  541.             //System.exit(0);
  542.         }
  543.  
  544.  
  545.         PRetType type = node.getRetType();
  546.         String type_str = type.toString();
  547.  
  548.         if(!(type_str.trim().equals("nothing")) && (idscope-1)==0){
  549.             System.out.println("ERROR : Function "+key+" must have returning type nothing");
  550.             //System.exit(0);
  551.         }
  552.         data temp = new data(key.trim(),0, type_str.trim(), "fun", "no", null, idscope-1, fuction_params_type,null);
  553.         if(symbol_table1.contains(temp)) {
  554.             System.out.println("ERROR : Identifier "+key.trim()+" already defined.");
  555.             //System.exit(0);
  556.         }
  557.         else{
  558.             symbol_table1.add(temp);
  559.         }
  560.     }
  561. /******************************************/
  562. //              Assigment                 //
  563. /******************************************/
  564.     @Override
  565.     public void inALValueStmt(ALValueStmt node) {
  566.         System.out.println("assigment : " + node.toString());
  567.         exp_types = new ArrayList<>();
  568.         ass_flag = true;
  569.     }
  570.     @Override
  571.     public void outALValueStmt(ALValueStmt node) {
  572.         PLValue val = node.getLValue();
  573.         String key = val.toString();
  574.         String[] new_key = key.split("\\[");
  575.         key = new_key[0];
  576.  
  577.         data temp = new data(key.trim(),0,null,null, "-", null, idscope, null,null);
  578.         if (symbol_table1.contains(temp)) {
  579.             int index = symbol_table1.indexOf(temp);
  580.             data temp1 = symbol_table1.get(index);
  581.             String type = temp1.get_type();
  582.             // elegxos tupwn
  583.             for(String str : exp_types){
  584.                 System.out.println("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd "+exp_types);
  585.                 if(type.equals(str)){
  586.                     //System.out.println("ok");
  587.                 }
  588.                 else{
  589.                     System.out.println("ERROR : In L_value statement :"+type+"-"+str);
  590.                     //System.exit(0);
  591.                 }
  592.             }
  593.             //System.out.println(new_key.length);
  594.  
  595.             if(new_key.length>1 || temp1.get_id_type().equals("array")){
  596.                 List<Integer> temp_dim_list = new ArrayList(temp1.get_dimensions());
  597.                 if(new_key.length-1 != temp_dim_list.size()){
  598.                     System.out.println("ERROR : Array "+key+"dimensions mismatch");
  599.                 }
  600.                 else {
  601.                     //System.out.println("------------------- "+new_key[1]);
  602.                     for (int i = 1; i < new_key.length; i++) {
  603.                         new_key[i] = new_key[i].replace("]", "").replace("[", "");
  604.                         new_key[i] = new_key[i].trim();
  605.  
  606.                         Boolean array_index = new_key[i].matches(".*\\d+.*");    //true for int const , false for identifier
  607.                         //System.out.println("/////////////////////" + array_index);
  608.  
  609.                         if (array_index == true) {
  610.                             Integer temp_dim = Integer.valueOf(new_key[i]);
  611.                             if(temp_dim >= temp_dim_list.get(i-1)){
  612.                                 System.out.println("ERROR : Array "+key+"out of bounds");
  613.                             }
  614.                         }
  615.                         else{
  616.                             //System.out.println(new_key[i]);
  617.                             data temp2 = new data(new_key[i].trim(),0,null,null, "-", null, idscope, null,null);
  618.                             if (symbol_table1.contains(temp2)) {
  619.                                 int index1 = symbol_table1.indexOf(temp2);
  620.                                 data temp3 = symbol_table1.get(index1);
  621.                                 String type1 = temp3.get_type();
  622.                                 if(!type1.equals("int") || !temp3.get_id_type().equals("var")){
  623.                                     //System.out.println("---------- > "+temp3.get_id_type()+"--jjjj");
  624.                                     System.out.println("Error") ;
  625.                                 }
  626.                             }
  627.                             else{
  628.                                 System.out.println("ERROR");
  629.                             }
  630.                         }
  631.                     }
  632.                 }
  633.             }
  634.         }
  635.         else{
  636.             System.out.println("ERROR : Variable "+key.trim()+" is not defined - 1");
  637.             //System.exit(0);
  638.         }
  639.         ass_flag = false;
  640.     }
  641. /******************************************/
  642. //            Condition Comp1             //
  643. /******************************************/
  644.     @Override
  645.     public void inAComp1Cond(AComp1Cond node) {
  646.         //System.out.println("Comp1 : " + node.toString());
  647.         comp1_flag = true;
  648.         comp1_exp_types = new ArrayList<>();
  649.     }
  650.     @Override
  651.     public void outAComp1Cond(AComp1Cond node) {
  652.         for(String str : comp1_exp_types){
  653.             if(str.equals(comp1_exp_types.get(0))){
  654.                 //System.out.println("ok");
  655.             }
  656.             else{
  657.                 System.out.println("ERROR - Condition1 with incompatible types");
  658.                 //System.exit(0);
  659.             }
  660.         }
  661.         comp1_flag = false;
  662.     }
  663.  
  664. /******************************************/
  665. //            Condition Comp2             //
  666. /******************************************/
  667.     @Override
  668.     public void inAComp2Cond(AComp2Cond node) {
  669.         //System.out.println("Comp2 : " + node.toString());
  670.         comp2_flag = true;
  671.         comp2_exp_types = new ArrayList<>();
  672.     }
  673.     @Override
  674.     public void outAComp2Cond(AComp2Cond node) {
  675.         for(String str : comp2_exp_types){
  676.             if(str.equals(comp2_exp_types.get(0))){
  677.                 //System.out.println("ok");
  678.             }
  679.             else{
  680.                 System.out.println("ERROR - Condition2 with incompatible types");
  681.                 //System.exit(0);
  682.             }
  683.         }
  684.         comp2_flag = false;
  685.     }
  686.  
  687. /******************************************/
  688. //            Condition Comp3             //
  689. /******************************************/
  690.     @Override
  691.     public void inAComp3Cond(AComp3Cond node) {
  692.         //System.out.println("Comp3 : " + node.toString());
  693.         comp3_flag = true;
  694.         comp3_exp_types = new ArrayList<>();
  695.     }
  696.     @Override
  697.     public void outAComp3Cond(AComp3Cond node) {
  698.         for(String str : comp3_exp_types){
  699.             if(str.equals(comp3_exp_types.get(0))){
  700.                 //System.out.println("ok");
  701.             }
  702.             else{
  703.                 System.out.println("ERROR - Condition3 with incompatible types");
  704.                 //System.exit(0);
  705.             }
  706.         }
  707.         comp3_flag = false;
  708.     }
  709.  
  710. /******************************************/
  711. //            Condition Comp4             //
  712. /******************************************/
  713.     @Override
  714.     public void inAComp4Cond(AComp4Cond node) {
  715.         //System.out.println("Comp4 : " + node.toString());
  716.         comp4_flag = true;
  717.         comp4_exp_types = new ArrayList<>();
  718.     }
  719.     @Override
  720.     public void outAComp4Cond(AComp4Cond node) {
  721.         for(String str : comp4_exp_types){
  722.             if(str.equals(comp4_exp_types.get(0))){
  723.                 //System.out.println("ok");
  724.             }
  725.             else{
  726.                 System.out.println("ERROR - Condition4 with incompatible types");
  727.                 //System.exit(0);
  728.             }
  729.         }
  730.         comp4_flag = false;
  731.     }
  732.  
  733. /******************************************/
  734. //            Condition Comp5             //
  735. /******************************************/
  736.     @Override
  737.     public void inAComp5Cond(AComp5Cond node) {
  738.         //System.out.println("Comp5 : " + node.toString());
  739.         comp5_flag = true;
  740.         comp5_exp_types = new ArrayList<>();
  741.     }
  742.     @Override
  743.     public void outAComp5Cond(AComp5Cond node) {
  744.         for(String str : comp5_exp_types){
  745.             if(str.equals(comp5_exp_types.get(0))){
  746.                 //System.out.println("ok");
  747.             }
  748.             else{
  749.                 System.out.println("ERROR - Condition5 with incompatible types");
  750.                 //System.exit(0);
  751.             }
  752.         }
  753.         comp5_flag = false;
  754.     }
  755.  
  756. /******************************************/
  757. //            Condition Comp6             //
  758. /******************************************/
  759.     @Override
  760.     public void inAComp6Cond(AComp6Cond node) {
  761.         //System.out.println("Comp6 : " + node.toString());
  762.         comp6_flag = true;
  763.         comp6_exp_types = new ArrayList<>();
  764.     }
  765.     @Override
  766.     public void outAComp6Cond(AComp6Cond node) {
  767.         for(String str : comp6_exp_types){
  768.             if(str.equals(comp6_exp_types.get(0))){
  769.                 //System.out.println("ok");
  770.             }
  771.             else{
  772.                 System.out.println("ERROR - Condition6 with incompatible types");
  773.                 //System.exit(0);
  774.             }
  775.         }
  776.         comp6_flag = false;
  777.     }
  778.  
  779.  
  780. /******************************************/
  781. //         Expression int_const           //
  782. /******************************************/
  783.     @Override
  784.     public void inAExpr5aExpr(AExpr5aExpr node) {
  785.         //System.out.println("expr5a : " + node.toString());
  786.     }
  787.     @Override
  788.     public void outAExpr5aExpr(AExpr5aExpr node) {
  789.         if(l_value1_flag == true){
  790.             simple_expr_list_in_lvalue1.add("int");
  791.         }
  792.         else {
  793.             simple_expr_list.add("int");
  794.         }
  795.  
  796.         if(comp1_flag == true){
  797.             comp1_exp_types.add("int");
  798.         }
  799.         if(comp2_flag == true){
  800.             comp2_exp_types.add("int");
  801.         }
  802.         if(comp3_flag == true){
  803.             comp3_exp_types.add("int");
  804.         }
  805.         if(comp4_flag == true){
  806.             comp4_exp_types.add("int");
  807.         }
  808.         if(comp5_flag == true){
  809.             comp5_exp_types.add("int");
  810.         }
  811.         if(comp6_flag == true){
  812.             comp6_exp_types.add("int");
  813.         }
  814.  
  815.         if( fuction_call_param_flag == true ){
  816.             if(l_value1_flag == false) {
  817.                 fuction_call_param.add("int");
  818.             }
  819.         }
  820.     }
  821. /******************************************/
  822. //         Expression char_const          //
  823. /******************************************/
  824.     @Override
  825.     public void inAExpr5bExpr(AExpr5bExpr node) {
  826.         //System.out.println("expr5b : " + node.toString());
  827.     }
  828.     @Override
  829.     public void outAExpr5bExpr(AExpr5bExpr node) {
  830.         if(l_value1_flag == true){
  831.            simple_expr_list_in_lvalue1.add("char");
  832.         }
  833.         else {
  834.             simple_expr_list.add("char");
  835.         }
  836.  
  837.         if(comp1_flag == true){
  838.             comp1_exp_types.add("char");
  839.         }
  840.         if(comp2_flag == true){
  841.             comp2_exp_types.add("char");
  842.         }
  843.         if(comp3_flag == true){
  844.             comp3_exp_types.add("char");
  845.         }
  846.         if(comp4_flag == true){
  847.             comp4_exp_types.add("char");
  848.         }
  849.         if(comp5_flag == true){
  850.             comp5_exp_types.add("char");
  851.         }
  852.         if(comp6_flag == true){
  853.             comp6_exp_types.add("char");
  854.         }
  855.  
  856.         if( fuction_call_param_flag == true ){
  857.             fuction_call_param.add("char");
  858.         }
  859.     }
  860. /******************************************/
  861. //                L_value                 //
  862. /******************************************/
  863.     @Override
  864.     public void inALValue1LValue(ALValue1LValue node) {
  865.         //System.out.println("l_value : " + node.toString());
  866.         if(fuction_call_param_flag == true ){
  867.             PLValue temp = node.getLValue();
  868.             String id = temp.toString();
  869.             data temp1 = new data(id.trim(),0,"-","fun","no",null,idscope,null,null);
  870.             if(symbol_table1.contains(temp1)) {
  871.                 int index = symbol_table1.indexOf(temp1);
  872.                 data temp2 = symbol_table1.get(index);
  873.                 String type = temp2.get_type();
  874.             }
  875.             else{
  876.                 System.out.println("ERROR : Array "+id.trim()+" is not defined");
  877.             }
  878.         }
  879.         l_value1_flag = true;
  880.     }
  881.     @Override
  882.     public void outALValue1LValue(ALValue1LValue node) {
  883.         l_value1_flag = false;
  884.     }
  885. /******************************************/
  886. //          Expression l_value            //
  887. /******************************************/
  888.     @Override
  889.     public void inAExpr5cExpr(AExpr5cExpr node) {
  890.         //System.out.println("expr5c : " + node.toString());
  891.     }
  892.     @Override
  893.     public void outAExpr5cExpr(AExpr5cExpr node) {
  894.         String key = node.toString();
  895.         if(key.contains("\"")){
  896.             simple_expr_list.add("char");
  897.             fuction_call_param.add("char");
  898.             return;
  899.         }
  900.         String[] result = key.split(" ");
  901.         String first = result[0];
  902.         key = first;
  903.         data temp = new data(key.trim(),5, "int", "g", "-", null, idscope, null,null);
  904.         if (symbol_table1.contains(temp)) { // report an error
  905.             int index = symbol_table1.indexOf(temp);
  906.             data temp1 = symbol_table1.get(index);
  907.             String type = temp1.get_type();
  908.             if(l_value1_flag == true){
  909.                 simple_expr_list_in_lvalue1.add(type.trim());
  910.             }
  911.             else {
  912.                 simple_expr_list.add(type.trim());
  913.             }
  914.             int temp_scope = temp1.get_scope();
  915.             if(temp_scope > idscope){
  916.                 System.out.println("ERROR : Variable "+key.trim()+" out of scope");
  917.                 //System.exit(0);
  918.             }
  919.             if(ass_flag == true && fuction_call_param_flag == false) {
  920.                 //System.out.println("***************************** "+key);
  921.                 //exp_types.add(type.trim());
  922.             }
  923.  
  924.             if(comp1_flag == true){
  925.                 comp1_exp_types.add(type.trim());
  926.             }
  927.             if(comp2_flag == true){
  928.                 comp2_exp_types.add(type.trim());
  929.             }
  930.             if(comp3_flag == true){
  931.                 comp3_exp_types.add(type.trim());
  932.             }
  933.             if(comp4_flag == true){
  934.                 comp4_exp_types.add(type.trim());
  935.             }
  936.             if(comp5_flag == true){
  937.                 comp5_exp_types.add(type.trim());
  938.             }
  939.             if(comp6_flag == true){
  940.                 comp6_exp_types.add(type.trim());
  941.             }
  942.  
  943.             if( fuction_call_param_flag == true ){
  944.                 if(l_value1_flag == false ) {
  945.                     fuction_call_param.add(type.trim());
  946.                 }
  947.             }
  948.         }
  949.         else{
  950.             System.out.println("ERROR : Variable "+key.trim()+" is not defined - EXPR5");
  951.             //System.exit(0);
  952.         }
  953.     }
  954. /******************************************/
  955. //          Expression fun_call           //
  956. /******************************************/
  957.     @Override
  958.     public void inAExpr5dExpr(AExpr5dExpr node) {
  959.         //System.out.println("expr5d : " + node.toString());
  960.         function_flag = true;
  961.     }
  962.     @Override
  963.     public void outAExpr5dExpr(AExpr5dExpr node) {}
  964.  
  965.     public void Print() {
  966.         System.out.println("List" + symbol_table1);
  967.     }
  968.  
  969. /******************************************/
  970. //                  Program               //
  971. /******************************************/
  972.     @Override
  973.     public void inAProgram(AProgram node) {}
  974.     @Override
  975.     public void outAProgram(AProgram node) {
  976.         Print();
  977.         System.out.println("END !");
  978.     }
  979. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement