Advertisement
Guest User

Untitled

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