Commits

Nzen committed a179986 Merge

Merge branch 'master' of ssh://bitbucket.org/Nzen/311-mini-lexer

  • Participants
  • Parent commits 7713e7e, 0b53b07

Comments (0)

Files changed (6)

File FlatTrie.java

 	private Alphabet sigma;
 	private final String end;
 	private T_Flag state;
-	private int[] switch_; // preferred first_chars
-	private StringBuilder symbols; // preferred rest_stacked
-	private ArrayList<Integer> next; // preferred skip_list
+	private int[] first_chars; // in an identifier
+	private StringBuilder rest_stacked; // other chars with flag
+	private ArrayList<Integer> skip_list; // jump indicies for above
 	private int next_open;
 	private int focus; 
 
 		state = T_Flag.initial;
 		next_open = 0;
 		focus = 0;
-		switch_ = new int[ sigma.length() ];
-		symbols = new StringBuilder( conspicuous_consumption );
-		next = new ArrayList<Integer>( conspicuous_consumption );
+		first_chars = new int[ sigma.length() ];
+		rest_stacked = new StringBuilder( conspicuous_consumption );
+		skip_list = new ArrayList<Integer>( conspicuous_consumption );
 
 		overcome_stl_limitation( conspicuous_consumption - 1 );
 	}
 	// specifically, that I can't update the unallocated indicies otherwise
 	private void overcome_stl_limitation( int gluttony )
 	{
-		for ( int ind = switch_.length - 1; ind >=0; ind-- )
-			switch_[ ind ] = -1;
+		for ( int ind = first_chars.length - 1; ind >=0; ind-- )
+			first_chars[ ind ] = -1;
 		for ( ; gluttony >= 0; gluttony-- )
 		{
-			next.add( 0 );
-			symbols.append( ' ' );
+			skip_list.add( 0 );
+			rest_stacked.append( ' ' );
 		}
 	}
 
 	// first_char[0] is valid, so had to init with -1
 	private boolean been_seen( int first_index )
 	{
-		return switch_[ first_index ] >= 0;
+		return first_chars[ first_index ] >= 0;
 	}
 
 	// so start checking from the second char
 	private T_Flag first_used( int where )
 	{
-		focus = switch_[ where ];
+		focus = first_chars[ where ];
 		return T_Flag.f_seen;
 	}
 
 	// save index to switch to
 	private T_Flag save_first( int look_here, char is_new )
 	{
-		switch_[ look_here ] = next_open;
+		first_chars[ look_here ] = next_open;
 		return T_Flag.f_unseen;
 	}
 
 	private T_Flag save_char( char ready )
 	{
-		symbols.setCharAt( next_open, ready );
+		rest_stacked.setCharAt( next_open, ready );
 		next_open++;
 		return T_Flag.f_unseen;
 	}
 	// it matches, there's a branch, or I start saving
 	private T_Flag check_down_the_trie( int skip_ind, char hmm )
 	{
-		char presently = symbols.charAt( skip_ind );
+		char presently = rest_stacked.charAt( skip_ind );
 		if ( hmm == presently )
 			return compare_more_later( skip_ind );
 		else if ( more_to_try( skip_ind ) )
-			return check_down_the_trie( next.get( skip_ind ), hmm );
+			return check_down_the_trie( skip_list.get( skip_ind ), hmm );
 		else
 		{
 			state = T_Flag.saving;
-			next.set( skip_ind, next_open );
+			skip_list.set( skip_ind, next_open );
 			return save_char( hmm );
 		}
 	}
 	@Override
 	public T_Flag check_flag( char a_flag )
 	{
-		int old_focus = focus;
+		T_Flag previous = state;
+		state = T_Flag.initial;
 		if ( a_flag == end.charAt( 0 ) ) // keymode hack
 		{
 			save_char( a_flag );
-			state = T_Flag.initial;
-			return T_Flag.f_diff_flag;
+			return T_Flag.f_key;
 		}
-		else if ( state == T_Flag.saving )
+		else if ( previous == T_Flag.saving )
 		{
 			save_char( a_flag );
-			state = T_Flag.initial;
 			return T_Flag.f_unseen;
 		}
 		else // checking
 		{
-			state = T_Flag.initial;
-			return ( symbols.charAt( old_focus ) == a_flag ) ?
-					T_Flag.f_seen : T_Flag.f_diff_flag;
+			T_Flag matched = recur_for_flag( focus, a_flag );
+			state = T_Flag.initial; // trie might move to saving
+			return ( matched == T_Flag.f_seen ) ? T_Flag.f_seen : T_Flag.f_key ;
 		}
 	}
 
+	// only called when id ended in checking state, no saving applicable
+	private T_Flag recur_for_flag( int looking, char clients )
+	{
+		char previous = rest_stacked.charAt( looking );
+		if ( clients == previous )
+			return T_Flag.f_seen;
+		else if ( more_to_try( looking ) )
+			return recur_for_flag( skip_list.get( looking ), clients );
+		else // ran out of jumps, only encountered alphabet and *
+			return T_Flag.f_key;
+	}
+
 	public boolean t_matches_state( T_Flag client_thinks )
 	{ // for tests
 		return state == client_thinks;
 	// skip_list is only for indices past 0, else unallocated
 	private boolean more_to_try( int skip_ind )
 	{
-		return next.get( skip_ind ) > 0 ;
+		return skip_list.get( skip_ind ) > 0 ;
 	}
 
 	@Override
 	{
 		int end_i = letras.length();
 		char[] bits = letras.toCharArray();
-		Arrays.sort( bits );		
+		Arrays.sort( bits );
 		int start = 0;
 		int iterations = 5;
 		int mid_lim = end_i / iterations;
 			for ( int ind = start; ind < mid_lim; ind++ )
 			{
 				nn = sigma.stored_in( bits[ ind ] );
-				System.out.printf( "%4d|", switch_[ nn ] );
+				System.out.printf( "%4d|", first_chars[ nn ] );
 				start++;
 			}
 			mid_lim += offset;
 	private void emit_database( )
 	{
 		int new_low = 0;
-		int upper = symbols.lastIndexOf( "?" ) + 1;
-		String storage = symbols.substring( 0, upper );
+		int upper = rest_stacked.lastIndexOf( "?" ) + 1;
+		String storage = rest_stacked.substring( 0, upper );
 		upper = storage.length() - 1;
 		int times = 200;
 		int offset = upper / times;
 
 	private void sys_print_skip( int ind )
 	{
-		boolean used = next.get( ind ) > 0;
-		String either = used ? next.get( ind ).toString() : "_";
+		boolean used = skip_list.get( ind ) > 0;
+		String either = used ? skip_list.get( ind ).toString() : "_";
 		System.out.printf( "%4s|", either );
 	}
 
 		// capture last, incomplete char row
 		print_sans_sigma( ended_at, upper - ended_at, storage );
 	}
-}
+
+	public void show_tiny()
+	{
+		show_small_alphabet();
+		show_few_ids();
+	}
+
+	// why do I am I fighting this so hard?
+	private void show_small_alphabet()
+	{
+		String arbitrary_order = sigma.get_letters();
+		int len = arbitrary_order.length();
+		IterableString letras = new IterableString( arbitrary_order );
+		System.out.printf( "%nindex\t" );
+		for ( int ind = 0; ind < len; ind++ )
+		{
+			System.out.printf( "%3d|", ind );
+		}
+		System.out.printf( "%nalpha\t" );
+		for ( Character individ : letras )
+		{
+			System.out.printf( "%3c|", individ );
+		}
+		System.out.printf( "%njump\t" );
+		// will fill with first_char indicies matching sigma;s actual storage order
+		int[] unsorted = new int[ len ];
+		int ind = 0;
+		for ( Character individ : letras )
+		{
+			unsorted[ ind++ ] = sigma.stored_in( individ ); // has
+		}
+		int nn;
+		for ( Character gah : letras )
+		{
+			nn = sigma.stored_in( gah );
+			if ( nn >= first_chars.length ) // why is nn 13?
+				nn = first_chars.length - 1;
+			System.out.printf( "%3d|", first_chars[ nn ] );
+			//System.out.printf( "%3d|", first_chars[ ind++] );//unsorted[ind++] ] );
+		}
+		//for ( int uu = 0; uu < len; uu++ )
+		//	System.out.printf( "%3d|", unsorted[ uu ] );
+		//System.out.print( "\n\t" );
+		/*
+alpha	f|  d|  u|  e|  t|  b|  r|  c|  n|  _|  l|  y|  i|
+jump	6| 11|  1| 10|  2|  7|  9|  0|  3| 12|  4|  8| 13|
+		0| -1| -1|  3|  4| -1| -1|  8| -1| 10| -1| -1| -1|
+	  * /
+		// I'm saying, get the firstchar cell values of the letters
+		// in the order of their arbitrary_order index.
+		for ( int outer_ind = 0; outer_ind < len; outer_ind++ )
+		{
+			for ( int search = 0; search < len; search++ )
+			{
+				if ( unsorted[search] == outer_ind )
+				System.out.printf( "%3d|", first_chars[ unsorted[ search ] ] );
+			}
+		}/*
+		System.out.print( "\n\t" );
+		ind = 0;
+		for ( Character gah : letras )
+		{
+			System.out.printf( "%3d|", first_chars[ ind++] );//unsorted[ind++] ] );
+		}*/
+		System.out.printf( "%n%n" );
+	}
+
+	private void show_few_ids()
+	{
+		String id_chars = rest_stacked.substring( 0 ,
+						rest_stacked.lastIndexOf( "?" ) );
+		int len = id_chars.length();
+		IterableString saved = new IterableString( id_chars );
+		System.out.printf( "ind\t" );
+		for ( int nn = 0; nn < len; nn++ )
+			System.out.printf( "%3d|", nn );
+		System.out.printf( "%nrest\t" );
+		for ( Character nn : saved )
+			System.out.printf( "%3c|", nn );
+		System.out.printf( "%nskip\t" );
+		for ( int nn = 0; nn < len; nn++ )
+			System.out.printf( "%3d|", skip_list.get( nn ) );
+	}
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
 
 	private char proper_flag( T_Flag which )
 	{
-		if ( keymode || which == T_Flag.f_diff_flag )
+		if ( keymode || which == T_Flag.f_key )
 			return '*';
 		else if ( which == T_Flag.f_unseen )
 			return '?';

File Project2.java

 
 	public static void main( String[] args )
 	{
-		String keys = args[0].substring( 0, args[0].length() - 1 );
-		run( keys, get_file_names( args[1] ) );
+		//String keys = args[0].substring( 0, args[0].length() - 1 );
+		//run( keys, get_file_names( args[1] ) );
 		//run( "InputFile1.txt", new String[] {"Project2.java"} );
-		//run_tests();
+		run_tests();
 	}
 
 	// first line in list_file is number of files
 		generic.close();
 	}
 
-	/*static void run_tests()
+	static void run_tests()
 	{
 		TestSuite for_classes = new TestSuite();
-		for_classes.test_iterable_string();
-		for_classes.test_word_loader( "arg.java" );
-		for_classes.test_loader( "monkey.txt" );
-		for_classes.test_alphabet();
-		for_classes.test_trie();
-		for_classes.test_lexer();
-	}*/
+		//for_classes.test_iterable_string();
+		//for_classes.test_word_loader( "arg.java" );
+		//for_classes.test_loader( "monkey.txt" );
+		//for_classes.test_alphabet();
+		//for_classes.test_trie();
+		//for_classes.test_lexer();
+		for_classes.test_for_false_key();
+	}
 }
 
 public enum T_Flag
 {
-	f_seen, f_unseen, f_diff_flag, saving, initial, checking 
+	f_seen, f_unseen, f_key, saving, initial, checking 
 }

File TestSuite.java

 		ram.t_t_dump_for_reveal( delta );
 		delta.reveal_thyself(); //not until it's bigger than the alphabet
 	}
+	// for_file_names@ read_line? cut? n* left? by* read_int@
+	// else* if* skip_list@ r* windows@ ending?
+	// this is pretty terrible, even though I thought I fixed it earlier in check flag
+	public void test_for_false_key()
+	{
+		IterableString f_key = new IterableString( "cut ni left by n read_int " );
+		// bam. n produces the behavior when it has been seen. wth?
+		T_Flag says;
+		String flags = "*?";
+		String beta = "cutnlefbyred_i";
+		FlatTrie compo = new FlatTrie( beta, flags );
+		for ( Character nu : f_key )
+		{
+			if ( nu == ' ' )
+			{
+				says = compo.check_flag( flags.charAt( 1 ) );
+				System.out.print( was_flag( says ) + " " );
+			}
+			else
+			{
+				compo.determine_char( nu );
+				System.out.print( nu );
+			}
+		}
+		System.out.println( );
+		compo.show_tiny();
+	}
+
+	private char was_flag( T_Flag received )
+	{
+		if ( received == T_Flag.f_seen )
+			return '@';
+		if ( received == T_Flag.f_unseen )
+			return '?';
+		if ( received == T_Flag.f_key )
+			return '*';
+		else
+			return 'X'; // assert unreachable
+	}
 
 	public void test_iterable_string( )
 	{

File TrieTester.java

 				}
 			}
 			shoul_b_diff = delta.check_flag( k_f );
-			if ( shoul_b_diff != T_Flag.f_diff_flag )
+			if ( shoul_b_diff != T_Flag.f_key )
 			{
 				System.out.println( "Tr: saving key didn't give diff flag" );
 				worked = fail;
 					"matching key not in check mode" ) && worked;
 		}
 		report = delta.check_flag( n_f );
-		if ( report != T_Flag.f_diff_flag )
+		if ( report != T_Flag.f_key )
 		{
 			System.out.println( "Tr: ending key didn't produce diff flag" ); // also trips this
 			worked = fail;