Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.compiler;
- import analysis.DepthFirstAdapter;
- import node.*;
- import java.util.*;
- public class SemanticAnalyzer extends DepthFirstAdapter {
- class data{
- String identifier; // name of identifier
- int value; // value of identifier
- String type; // type of identifier(int, char)
- String id_type; // identifier type (var, fun, array)!!!
- String funarg; // if variable is function argument or not
- String refer; // reference variable
- int scope; // scope variable
- List<String> function_parameters; // keeps the type of a fuction's parameters
- List<Integer> array_dimensions; // krataei to megethos kathe diastashs tou pinaka
- public data(String id, int val, String type, String id_type, String func, String ref, int scope, List<String> params, List<Integer> array_d){
- this.identifier = id; // name of identifier
- this.value = val; // value of identifier
- this.type = type; // type of identifier(int, char)
- this.id_type = id_type; // identifier type (var, fun)!!!
- this.funarg = func; // if variable is function argument or not
- this.refer = ref; // reference variable
- this.scope = scope; // scope variable
- if(params != null) {
- this.function_parameters = new ArrayList<>();
- for(String temp : params){
- function_parameters.add(temp);
- }
- }
- if(array_d != null) {
- this.array_dimensions = new ArrayList<>();
- for(int temp : array_d){
- this.array_dimensions.add(temp);
- }
- }
- }
- public int get_scope(){ return this.scope; }
- public String get(){
- return this.identifier;
- }
- public int geti(){
- return this.value;
- }
- public String get_type(){
- if( this.type.equals("int [ ]") || this.type.equals("int []") || this.type.equals("int[]")){
- return "int";
- }
- else if(this.type.equals("char [ ]") || this.type.equals("char []") || this.type.equals("char[]")){
- return "char";
- }
- return this.type;
- }
- public String get_id_type(){
- return this.id_type;
- }
- public List<String> get_function_parameters() {
- List<String> temp = new ArrayList<>();
- if( this.function_parameters!= null ) {
- for (String temp1 : this.function_parameters) {
- String[] temp2 = temp1.split("\\[");
- temp.add(temp2[0].trim());
- }
- return temp;
- }
- return this.function_parameters;
- }
- public List<Integer> get_dimensions(){
- return this.array_dimensions;
- }
- @Override
- public String toString() {
- //return ("ID :"+ this.get() +" VALUE: "+ this.geti() +" TYPE: "+ this.get_type() +" ID_TYPE: "+ get_id_type()+ "\n");
- if (this.id_type.equals("var")) {
- return ("Variable :"+ this.get() +" TYPE: "+ this.get_type() +" SCOPE "+this.get_scope()+"\n");
- }
- else if(this.id_type.equals("fun")){
- return ("Function :"+ this.get() +" TYPE: "+ this.get_type() +" SCOPE "+this.get_scope()+" Parameters "+this.get_function_parameters()+"\n");
- }
- else if(this.id_type.equals("array")){
- return ("Array :"+ this.get() +" TYPE: "+ this.get_type() +" SCOPE "+this.get_scope()+" Array Dimensions "+this.get_dimensions()+"\n");
- }
- return ("ID :"+ this.get() +" TYPE: "+ this.get_type() +" SCOPE "+this.get_scope()+" Parameters "+this.get_function_parameters()+" Array Dimensions "+this.get_dimensions()+"\n");
- }
- @Override
- public boolean equals(Object object){
- boolean s = false;
- if((identifier.trim()).equals( ((data)object).identifier) && variable_def == true && scope == ((data)object).scope) {
- s = true;
- return s;
- }
- else if((identifier.trim()).equals( ((data)object).identifier) && variable_def == true && scope != ((data)object).scope){
- return s;
- }
- else if((identifier.trim()).equals( ((data)object).identifier) && function_def == true && parameters_flag==true && scope == ((data)object).scope) {
- s = true;
- return s;
- }
- else if((identifier.trim()).equals( ((data)object).identifier) && function_def == true && parameters_flag==true && scope != ((data)object).scope){
- return s;
- }
- else if((identifier.trim()).equals( ((data)object).identifier) && scope >= ((data)object).scope){
- s = true;
- return s;
- }
- return s;
- }
- }
- List<data> symbol_table1 = new ArrayList(); // create new table
- private List<data> paramTypes;
- private List<String> fuction_params_type;
- private List<String> exp_types;
- private int idscope = 0;
- boolean scope_flag = false;
- boolean function_flag = false;
- boolean fun_decl_flag = false;
- boolean ass_flag = false;
- boolean comp1_flag = false;
- private List<String> comp1_exp_types;
- boolean comp2_flag = false;
- private List<String> comp2_exp_types;
- boolean comp3_flag = false;
- private List<String> comp3_exp_types;
- boolean comp4_flag = false;
- private List<String> comp4_exp_types;
- boolean comp5_flag = false;
- private List<String> comp5_exp_types;
- boolean comp6_flag = false;
- private List<String> comp6_exp_types;
- // typoi parametrwn sthn klhsh synarthsewn
- boolean fuction_call_param_flag = false ; // klhsh sunarthshs
- private List<String> fuction_call_param;
- boolean function_def = false; // an brisketai se fun definiton ( elegxos sto equals tou data )
- boolean variable_def = false; // an brisketai se variable definition ( elegxos sto equals tou data )
- boolean l_value1_flag = false ; // array ws metablhth sunarthshs
- private List<String> simple_expr_list;
- private List<String> simple_expr_list_in_lvalue1;
- private List<String> exp_com_list;
- boolean com_expr_flag = false;
- boolean parameters_flag = false ;
- 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
- public SemanticAnalyzer(){
- List<String> list = new ArrayList<>();
- list.add("int");
- data puti = new data("puti",0,"nothing","fun","no",null,-1,list,null);
- list.clear();
- list.add("char");
- data putc = new data("putc",0,"nothing","fun","no",null,-1,list,null);
- list.clear();
- list.add("char[]");
- data puts = new data("puts",0,"nothing","fun","no",null,-1,list,null);
- list.clear();
- symbol_table1.add(puti);
- symbol_table1.add(putc);
- symbol_table1.add(puts);
- data geti = new data("geti",0,"int","fun","no",null,-1, list,null);
- data getc = new data("getc",0,"char","fun","no",null,-1,list,null);
- list.add("int");
- list.add("char[]");
- data gets = new data("gets",0,"nothing","fun","no",null,-1,list,null);
- list.clear();
- symbol_table1.add(geti);
- symbol_table1.add(getc);
- symbol_table1.add(gets);
- list.add("int");
- data abs = new data("abs",0,"int","fun","no",null,-1,list, null);
- list.clear();
- list.add("char");
- data ord = new data("ord",0,"int","fun","no",null,-1, list,null);
- list.clear();
- list.add("int");
- data chr = new data("chr",0,"char","fun","no",null,-1, list,null);
- list.clear();
- symbol_table1.add(abs);
- symbol_table1.add(ord);
- symbol_table1.add(chr);
- list.add("char[]");
- data strlen = new data("strlen",0,"int","fun","no",null,-1, list,null);
- list.clear();
- list.add("char[]");
- list.add("char[]");
- data strcmp = new data("strcmp",0,"int","fun","no",null,-1,list,null);
- data strcpy = new data("strcpy",0,"nothing","fun","no",null,-1,list,null);
- data strcat = new data("strcat",0,"nothing","fun","no",null,-1,list,null);
- list.clear();
- symbol_table1.add(strlen);
- symbol_table1.add(strcmp);
- symbol_table1.add(strcpy);
- symbol_table1.add(strcat);
- }
- /******************************************/
- // Function Definition //
- /******************************************/
- @Override
- public void inAFuncDef(AFuncDef node){
- //Scope
- scope_flag = true;
- function_def = true;
- }
- @Override
- public void outAFuncDef(AFuncDef node){
- //Scope
- Iterator<data> iter = symbol_table1.iterator();
- Print();
- while (iter.hasNext()) {
- data str = iter.next();
- if (idscope == str.get_scope())
- iter.remove();
- }
- Print();
- idscope--;
- function_def = false;
- }
- /******************************************/
- // Function Declaretion //
- /******************************************/
- @Override
- public void inAFuncDecl(AFuncDecl node){
- fun_decl_flag = true;
- }
- @Override
- public void outAFuncDecl(AFuncDecl node){
- }
- /******************************************/
- // ExpCalc //
- /******************************************/
- @Override
- public void inACalcExpr(ACalcExpr node){
- simple_expr_list = new ArrayList<>();
- simple_expr_list_in_lvalue1 = new ArrayList<>(); //lista me tous index twn pinakwn se assigment gia elegxo tou tupou
- }
- @Override
- public void outACalcExpr(ACalcExpr node){
- if(l_value1_flag == true){
- String temp = simple_expr_list_in_lvalue1.get(0);
- for (String str : simple_expr_list_in_lvalue1) {
- if (temp.equals(str)) {
- //System.out.println("ok");
- }
- else {
- System.out.println("ERROR -" + temp + "-" + str + "- CalcExpr " + simple_expr_list_in_lvalue1);
- //System.exit(0);
- }
- }
- if(!temp.equals("int")){
- System.out.println("ERROR : Index of array must be int");
- }
- }
- else {
- String temp = simple_expr_list.get(0);
- for (String str : simple_expr_list) {
- if (temp.equals(str)) {
- rigth_part_of_assigment_flag = true;
- //System.out.println("ok");
- } else {
- rigth_part_of_assigment_flag = false;
- System.out.println("ERROR -" + temp + "-" + str + "- CalcExpr " + simple_expr_list);
- break;
- //System.exit(0);
- }
- }
- /// gia tis sunarthseis
- if (com_expr_flag == true && temp != null) {
- if (l_value1_flag != true) {
- exp_com_list.add(temp);
- }
- }
- }
- System.out.println(" -- > "+simple_expr_list);
- }
- /******************************************/
- // ExpCom //
- /******************************************/
- @Override
- public void inAExpcom(AExpcom node){
- com_expr_flag = true;
- exp_com_list = new ArrayList<>();
- }
- @Override
- public void outAExpcom(AExpcom node){
- com_expr_flag = false;
- }
- /******************************************/
- // Function Call //
- /******************************************/
- @Override
- public void inAFuncCall(AFuncCall node) {
- //System.out.println("FUNC_CALL : " + node.toString());
- fuction_call_param_flag = true;
- fuction_call_param = new ArrayList<>();
- if(function_flag==true){
- TId id = node.getId();
- String key = id.getText();
- data temp = new data(key.trim(),0,"-","fun","no",null,idscope,null,null);
- if(symbol_table1.contains(temp)){
- int index = symbol_table1.indexOf(temp);
- data temp1 = symbol_table1.get(index);
- String type = temp1.get_type();
- if(l_value1_flag == true){
- simple_expr_list_in_lvalue1.add(type.trim());
- }
- else {
- simple_expr_list.add(type.trim());
- }
- int temp_scope = temp1.get_scope();
- if(temp_scope > idscope){
- System.out.println("ERROR : Function "+key.trim()+" out of scope");
- //System.exit(0);
- }
- if(ass_flag == true) {
- function_flag = false;
- }
- if(comp1_flag == true){
- comp1_exp_types.add(type.trim());
- }
- if(comp2_flag == true){
- comp2_exp_types.add(type.trim());
- }
- if(comp3_flag == true){
- comp3_exp_types.add(type.trim());
- }
- if(comp4_flag == true){
- comp4_exp_types.add(type.trim());
- }
- if(comp5_flag == true){
- comp5_exp_types.add(type.trim());
- }
- if(comp6_flag == true){
- comp6_exp_types.add(type.trim());
- }
- }
- }
- }
- @Override
- public void outAFuncCall(AFuncCall node) {
- TId ident = node.getId();
- String key = ident.getText();
- data temp = new data(key.trim(),5, "fun", "hg", "-", null, idscope,null,null);
- if (symbol_table1.contains(temp)) {
- //System.out.println("ERROR : Identifier already defined - Function Call");
- //System.exit(0);
- }
- else {
- System.out.println("ERROR : Function "+key.trim()+" is not defined");
- //System.exit(0);
- }
- data temp1 = new data(key,0,"-","fun","no",null,idscope,null,null);
- int index = symbol_table1.indexOf(temp1);
- if( index != -1) {
- data temp2 = symbol_table1.get(index);
- List<String> temp_func_list = temp2.get_function_parameters();
- if (exp_com_list != null && temp_func_list != null) {
- if (exp_com_list.size() < temp_func_list.size()) {
- System.out.println("ERROR : Less parameters were given in function " + key + " call : " + exp_com_list.size() + " - " + temp_func_list.size());
- //System.exit(0);
- } else if (exp_com_list.size() > temp_func_list.size()) {
- System.out.println("ERROR : More parameters were given in function " + key + " call : " + exp_com_list.size() + " - " + temp_func_list.size());
- //System.exit(0);
- } else {
- if (!(temp_func_list.equals(exp_com_list))) {
- System.out.println("ERROR : Incompatible type of arguments were given in function " + key);
- //System.exit(0);
- }
- }
- }
- }
- fuction_call_param_flag = false;
- }
- /******************************************/
- // Variable Definiton //
- /******************************************/
- @Override
- public void inAVarDef(AVarDef node) {
- //System.out.println("Var def: " + node.toString());
- variable_def = true;
- }
- @Override
- public void outAVarDef(AVarDef node) {
- // type of variable
- PType type = node.getType();
- String t = type.toString();
- String[] new_type = t.split("\\[");
- String temp_id_type = "var";
- List<Integer> temp_dimensions_list = null;
- System.out.println(temp_dimensions_list );
- if(new_type.length>1) {
- temp_dimensions_list = new ArrayList<>();
- temp_id_type = "array";
- }
- for(int i=1;i < new_type.length;i++) {
- new_type[i] = new_type[i].replaceAll("\\D+", "");
- Integer x = Integer.valueOf(new_type[i]);
- System.out.println("dimension "+i+" megethos "+x);
- temp_dimensions_list.add(x);
- }
- //System.out.println(" -> "+new_type.length);
- t = new_type[0];
- // name of variable
- TId ident = node.getId();
- String key = ident.getText();
- data temp = new data(key.trim(), 0, t.trim(), temp_id_type, "no", "no", idscope, null,temp_dimensions_list);
- if (symbol_table1.contains(temp)) {
- System.out.println("ERROR : Identifier already defined - Variable Definition");
- //System.exit(0);
- }
- else{
- symbol_table1.add(temp);
- }
- for (PComid comid : node.getComid()) {
- key = comid.toString();
- temp = new data(key.trim(),0, t.trim(), temp_id_type, "no", "no", idscope,null,temp_dimensions_list);
- if (symbol_table1.contains(temp)) { // report an error
- System.out.println("ERROR : Identifier already defined - Variable Definition");
- //System.exit(0);
- }
- symbol_table1.add(temp);
- }
- variable_def = false;
- }
- /******************************************/
- // Parameters //
- /******************************************/
- @Override
- public void inAFparDef(AFparDef node) {
- parameters_flag = true;
- }
- @Override
- public void outAFparDef(AFparDef node) {
- if(fun_decl_flag == true){ //(fun_decl_param_flag == true){
- //System.out.println("Function Declaration Parameters");
- fun_decl_flag = false; //fun_decl_param_flag = false;
- }
- else{
- PFparType type = node.getFparType();
- String t = type.toString();
- TId id = node.getId();
- String key = id.getText();
- data temp = new data(key.trim(),0, t.trim(), "var", "yes", null, idscope, null,null);
- if(symbol_table1.contains(temp)){
- System.out.println("ERROR : Identifier "+key.trim()+" already defined - Function Parameters");
- //System.exit(0);
- }
- else{
- symbol_table1.add(temp);
- paramTypes.add(temp);
- fuction_params_type.add(t.trim());
- }
- for (PComid comid : node.getComid()) {
- key = comid.toString();
- temp = new data(key.trim(), 5, t.trim(), "var", "yes", null, idscope,null,null);
- if (symbol_table1.contains(temp)) {
- System.out.println("ERROR : Identifier " + key.trim() + " already defined - Function Parameters"+idscope);
- //System.exit(0);
- } else {
- symbol_table1.add(temp);
- paramTypes.add(temp);
- fuction_params_type.add(t.trim());
- }
- }
- }
- parameters_flag = false;
- }
- /******************************************/
- // Header //
- /******************************************/
- @Override
- public void inAHeader(AHeader node) {
- //System.out.println("Header : " + node.toString());
- paramTypes = new ArrayList<>();
- fuction_params_type = new ArrayList<>();
- if(scope_flag==true){
- idscope++;
- scope_flag = false;
- }
- }
- @Override
- public void outAHeader(AHeader node) {
- TId ident = node.getId();
- String key = ident.getText();
- List<String> temp_l = new ArrayList();
- if(!(paramTypes.equals(temp_l)) && (idscope-1)==0){
- System.out.println("ERROR : Function "+key+" cannot have arguments");
- //System.exit(0);
- }
- PRetType type = node.getRetType();
- String type_str = type.toString();
- if(!(type_str.trim().equals("nothing")) && (idscope-1)==0){
- System.out.println("ERROR : Function "+key+" must have returning type nothing");
- //System.exit(0);
- }
- data temp = new data(key.trim(),0, type_str.trim(), "fun", "no", null, idscope-1, fuction_params_type,null);
- if(symbol_table1.contains(temp)) {
- System.out.println("ERROR : Identifier "+key.trim()+" already defined.");
- //System.exit(0);
- }
- else{
- symbol_table1.add(temp);
- }
- }
- /******************************************/
- // Assigment //
- /******************************************/
- @Override
- public void inALValueStmt(ALValueStmt node) {
- System.out.println("assigment : " + node.toString());
- exp_types = new ArrayList<>();
- ass_flag = true;
- }
- @Override
- public void outALValueStmt(ALValueStmt node) {
- PLValue val = node.getLValue();
- String key = val.toString();
- String[] new_key = key.split("\\[");
- key = new_key[0];
- data temp = new data(key.trim(),0,null,null, "-", null, idscope, null,null);
- if (symbol_table1.contains(temp)) {
- int index = symbol_table1.indexOf(temp);
- data temp1 = symbol_table1.get(index);
- String type = temp1.get_type();
- // elegxos tupwn
- for(String str : exp_types){
- System.out.println("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd "+exp_types);
- if(type.equals(str)){
- //System.out.println("ok");
- }
- else{
- System.out.println("ERROR : In L_value statement :"+type+"-"+str);
- //System.exit(0);
- }
- }
- //System.out.println(new_key.length);
- if(new_key.length>1 || temp1.get_id_type().equals("array")){
- List<Integer> temp_dim_list = new ArrayList(temp1.get_dimensions());
- if(new_key.length-1 != temp_dim_list.size()){
- System.out.println("ERROR : Array "+key+"dimensions mismatch");
- }
- else {
- //System.out.println("------------------- "+new_key[1]);
- for (int i = 1; i < new_key.length; i++) {
- new_key[i] = new_key[i].replace("]", "").replace("[", "");
- new_key[i] = new_key[i].trim();
- Boolean array_index = new_key[i].matches(".*\\d+.*"); //true for int const , false for identifier
- //System.out.println("/////////////////////" + array_index);
- if (array_index == true) {
- Integer temp_dim = Integer.valueOf(new_key[i]);
- if(temp_dim >= temp_dim_list.get(i-1)){
- System.out.println("ERROR : Array "+key+"out of bounds");
- }
- }
- else{
- //System.out.println(new_key[i]);
- data temp2 = new data(new_key[i].trim(),0,null,null, "-", null, idscope, null,null);
- if (symbol_table1.contains(temp2)) {
- int index1 = symbol_table1.indexOf(temp2);
- data temp3 = symbol_table1.get(index1);
- String type1 = temp3.get_type();
- if(!type1.equals("int") || !temp3.get_id_type().equals("var")){
- //System.out.println("---------- > "+temp3.get_id_type()+"--jjjj");
- System.out.println("Error") ;
- }
- }
- else{
- System.out.println("ERROR");
- }
- }
- }
- }
- }
- }
- else{
- System.out.println("ERROR : Variable "+key.trim()+" is not defined - 1");
- //System.exit(0);
- }
- ass_flag = false;
- }
- /******************************************/
- // Condition Comp1 //
- /******************************************/
- @Override
- public void inAComp1Cond(AComp1Cond node) {
- //System.out.println("Comp1 : " + node.toString());
- comp1_flag = true;
- comp1_exp_types = new ArrayList<>();
- }
- @Override
- public void outAComp1Cond(AComp1Cond node) {
- for(String str : comp1_exp_types){
- if(str.equals(comp1_exp_types.get(0))){
- //System.out.println("ok");
- }
- else{
- System.out.println("ERROR - Condition1 with incompatible types");
- //System.exit(0);
- }
- }
- comp1_flag = false;
- }
- /******************************************/
- // Condition Comp2 //
- /******************************************/
- @Override
- public void inAComp2Cond(AComp2Cond node) {
- //System.out.println("Comp2 : " + node.toString());
- comp2_flag = true;
- comp2_exp_types = new ArrayList<>();
- }
- @Override
- public void outAComp2Cond(AComp2Cond node) {
- for(String str : comp2_exp_types){
- if(str.equals(comp2_exp_types.get(0))){
- //System.out.println("ok");
- }
- else{
- System.out.println("ERROR - Condition2 with incompatible types");
- //System.exit(0);
- }
- }
- comp2_flag = false;
- }
- /******************************************/
- // Condition Comp3 //
- /******************************************/
- @Override
- public void inAComp3Cond(AComp3Cond node) {
- //System.out.println("Comp3 : " + node.toString());
- comp3_flag = true;
- comp3_exp_types = new ArrayList<>();
- }
- @Override
- public void outAComp3Cond(AComp3Cond node) {
- for(String str : comp3_exp_types){
- if(str.equals(comp3_exp_types.get(0))){
- //System.out.println("ok");
- }
- else{
- System.out.println("ERROR - Condition3 with incompatible types");
- //System.exit(0);
- }
- }
- comp3_flag = false;
- }
- /******************************************/
- // Condition Comp4 //
- /******************************************/
- @Override
- public void inAComp4Cond(AComp4Cond node) {
- //System.out.println("Comp4 : " + node.toString());
- comp4_flag = true;
- comp4_exp_types = new ArrayList<>();
- }
- @Override
- public void outAComp4Cond(AComp4Cond node) {
- for(String str : comp4_exp_types){
- if(str.equals(comp4_exp_types.get(0))){
- //System.out.println("ok");
- }
- else{
- System.out.println("ERROR - Condition4 with incompatible types");
- //System.exit(0);
- }
- }
- comp4_flag = false;
- }
- /******************************************/
- // Condition Comp5 //
- /******************************************/
- @Override
- public void inAComp5Cond(AComp5Cond node) {
- //System.out.println("Comp5 : " + node.toString());
- comp5_flag = true;
- comp5_exp_types = new ArrayList<>();
- }
- @Override
- public void outAComp5Cond(AComp5Cond node) {
- for(String str : comp5_exp_types){
- if(str.equals(comp5_exp_types.get(0))){
- //System.out.println("ok");
- }
- else{
- System.out.println("ERROR - Condition5 with incompatible types");
- //System.exit(0);
- }
- }
- comp5_flag = false;
- }
- /******************************************/
- // Condition Comp6 //
- /******************************************/
- @Override
- public void inAComp6Cond(AComp6Cond node) {
- //System.out.println("Comp6 : " + node.toString());
- comp6_flag = true;
- comp6_exp_types = new ArrayList<>();
- }
- @Override
- public void outAComp6Cond(AComp6Cond node) {
- for(String str : comp6_exp_types){
- if(str.equals(comp6_exp_types.get(0))){
- //System.out.println("ok");
- }
- else{
- System.out.println("ERROR - Condition6 with incompatible types");
- //System.exit(0);
- }
- }
- comp6_flag = false;
- }
- /******************************************/
- // Expression int_const //
- /******************************************/
- @Override
- public void inAExpr5aExpr(AExpr5aExpr node) {
- //System.out.println("expr5a : " + node.toString());
- }
- @Override
- public void outAExpr5aExpr(AExpr5aExpr node) {
- if(l_value1_flag == true){
- simple_expr_list_in_lvalue1.add("int");
- }
- else {
- simple_expr_list.add("int");
- }
- if(comp1_flag == true){
- comp1_exp_types.add("int");
- }
- if(comp2_flag == true){
- comp2_exp_types.add("int");
- }
- if(comp3_flag == true){
- comp3_exp_types.add("int");
- }
- if(comp4_flag == true){
- comp4_exp_types.add("int");
- }
- if(comp5_flag == true){
- comp5_exp_types.add("int");
- }
- if(comp6_flag == true){
- comp6_exp_types.add("int");
- }
- if( fuction_call_param_flag == true ){
- if(l_value1_flag == false) {
- fuction_call_param.add("int");
- }
- }
- }
- /******************************************/
- // Expression char_const //
- /******************************************/
- @Override
- public void inAExpr5bExpr(AExpr5bExpr node) {
- //System.out.println("expr5b : " + node.toString());
- }
- @Override
- public void outAExpr5bExpr(AExpr5bExpr node) {
- if(l_value1_flag == true){
- simple_expr_list_in_lvalue1.add("char");
- }
- else {
- simple_expr_list.add("char");
- }
- if(comp1_flag == true){
- comp1_exp_types.add("char");
- }
- if(comp2_flag == true){
- comp2_exp_types.add("char");
- }
- if(comp3_flag == true){
- comp3_exp_types.add("char");
- }
- if(comp4_flag == true){
- comp4_exp_types.add("char");
- }
- if(comp5_flag == true){
- comp5_exp_types.add("char");
- }
- if(comp6_flag == true){
- comp6_exp_types.add("char");
- }
- if( fuction_call_param_flag == true ){
- fuction_call_param.add("char");
- }
- }
- /******************************************/
- // L_value //
- /******************************************/
- @Override
- public void inALValue1LValue(ALValue1LValue node) {
- //System.out.println("l_value : " + node.toString());
- if(fuction_call_param_flag == true ){
- PLValue temp = node.getLValue();
- String id = temp.toString();
- data temp1 = new data(id.trim(),0,"-","fun","no",null,idscope,null,null);
- if(symbol_table1.contains(temp1)) {
- int index = symbol_table1.indexOf(temp1);
- data temp2 = symbol_table1.get(index);
- String type = temp2.get_type();
- }
- else{
- System.out.println("ERROR : Array "+id.trim()+" is not defined");
- }
- }
- l_value1_flag = true;
- }
- @Override
- public void outALValue1LValue(ALValue1LValue node) {
- l_value1_flag = false;
- }
- /******************************************/
- // Expression l_value //
- /******************************************/
- @Override
- public void inAExpr5cExpr(AExpr5cExpr node) {
- //System.out.println("expr5c : " + node.toString());
- }
- @Override
- public void outAExpr5cExpr(AExpr5cExpr node) {
- String key = node.toString();
- if(key.contains("\"")){
- simple_expr_list.add("char");
- fuction_call_param.add("char");
- return;
- }
- String[] result = key.split(" ");
- String first = result[0];
- key = first;
- data temp = new data(key.trim(),5, "int", "g", "-", null, idscope, null,null);
- if (symbol_table1.contains(temp)) { // report an error
- int index = symbol_table1.indexOf(temp);
- data temp1 = symbol_table1.get(index);
- String type = temp1.get_type();
- if(l_value1_flag == true){
- simple_expr_list_in_lvalue1.add(type.trim());
- }
- else {
- simple_expr_list.add(type.trim());
- }
- int temp_scope = temp1.get_scope();
- if(temp_scope > idscope){
- System.out.println("ERROR : Variable "+key.trim()+" out of scope");
- //System.exit(0);
- }
- if(ass_flag == true && fuction_call_param_flag == false) {
- //System.out.println("***************************** "+key);
- //exp_types.add(type.trim());
- }
- if(comp1_flag == true){
- comp1_exp_types.add(type.trim());
- }
- if(comp2_flag == true){
- comp2_exp_types.add(type.trim());
- }
- if(comp3_flag == true){
- comp3_exp_types.add(type.trim());
- }
- if(comp4_flag == true){
- comp4_exp_types.add(type.trim());
- }
- if(comp5_flag == true){
- comp5_exp_types.add(type.trim());
- }
- if(comp6_flag == true){
- comp6_exp_types.add(type.trim());
- }
- if( fuction_call_param_flag == true ){
- if(l_value1_flag == false ) {
- fuction_call_param.add(type.trim());
- }
- }
- }
- else{
- System.out.println("ERROR : Variable "+key.trim()+" is not defined - EXPR5");
- //System.exit(0);
- }
- }
- /******************************************/
- // Expression fun_call //
- /******************************************/
- @Override
- public void inAExpr5dExpr(AExpr5dExpr node) {
- //System.out.println("expr5d : " + node.toString());
- function_flag = true;
- }
- @Override
- public void outAExpr5dExpr(AExpr5dExpr node) {}
- public void Print() {
- System.out.println("List" + symbol_table1);
- }
- /******************************************/
- // Program //
- /******************************************/
- @Override
- public void inAProgram(AProgram node) {}
- @Override
- public void outAProgram(AProgram node) {
- Print();
- System.out.println("END !");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement