import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


class Parsing_rules

{
	public static void main(String args[])
	{
		try
		{
		FileInputStream f=new FileInputStream("Schema_Parse_Entities.txt");
		FileInputStream f1=new FileInputStream("Schema_Parse_Types.txt");
		FileInputStream f2=new FileInputStream("ship_schema.exp");
		FileOutputStream fo=new FileOutputStream("entities_in_types2.txt");
		DataInputStream bf=new DataInputStream(f);
		DataInputStream bf1=new DataInputStream(f1);
		DataInputStream bf2=new DataInputStream(f2);
		PrintStream ds=new PrintStream(fo);
		
		ArrayList<String> arr_list_entity=new ArrayList<String>();
		ArrayList<ent_list> xy=new ArrayList<ent_list>();
		ArrayList<String> arr_list_type=new ArrayList<String>();
		
		boolean type=false;
		boolean end_type=false;
		
		String str_tmp=" ";
		//ArrayList<Integer> arr_list1=new ArrayList<Integer>();
		//PrintStream ds=new PrintStream(fo);
		
		while(bf.available()>0)
		{
			
			String str=bf.readLine();
			str.trim();
			arr_list_entity.add(str);
		
		}
		while(bf1.available()>0)
		{
			
			String str=bf1.readLine();
			str.trim();
			arr_list_type.add(str);
		
		}
			
			while(bf2.available()>0)
			{
				String str=bf2.readLine();				
				Pattern pattern = Pattern.compile("^*RULE");
				Pattern pattern2 = Pattern.compile("^*END_RULE");
				Matcher matcher = pattern.matcher(str);
				Matcher matcher2=pattern2.matcher(str);
				
				if(matcher.find())
				{
					type=true;
				}
				
				if(matcher2.find())
				{
					type=false;
					end_type=false;
				}
				
				if(type&!end_type)
				{
					str_tmp=str_tmp+str;
					
				}
				else
				{
					str_tmp=str_tmp.trim();
						
					//str_tmp=" ";
					System.out.println(str_tmp);
					str_tmp=" ";
				}
				//System.out.println("I am here");
			}
			System.out.println(arr_list_entity.size());
			for(int k=0;k<xy.size();k++)
			{
				ds.println("");
				ds.println(xy.get(k).name+":"+xy.get(k).cas);
				System.out.println(xy.get(k).name);
				for(int j=0;j<xy.get(k).getsize();j++)
				{
					
					ds.print(xy.get(k).getele(j)+" ");
				}
				ds.println("");
			}
			//for(int i=0;i<arr_list.size();i++)
			//{
				//System.out.println(arr_list.get(i));
			//}
			ds.close();
			fo.close();
		}catch(Exception e)		
		{
			System.err.println(e);
		}
	}
}
		