• No results found

(Equivalent Programs Redefined) Any two program strings in L s are equivalent and satisfy the two conditions given in Definition 2 22

6.4 r e f l e c t i o n s o n va r i a b i l i t y

In this chapter, we have formalised the enumeration of the space of alterna- tive programs using formal language notations. This space represents novice programs’ possibilities — a knowledge of what the solutions can be. In the next chapter,Chapter 7, we present an algorithm that enumerates these programs.

7

I T E R AT I V E G E N E R AT I O N O F P R O G R A M VA R I AT I O N S

I

n this chapter, we present an algorithm — based on the new formalisms presented in Chapter 6 — that exploits the top-down theory of program comprehension by grouping novice program fragments into chunks that represent three abstraction levels. The lowest level consists of the single line statements in the program (e.g. assignment statements), the middle level are the statement blocks (e.g. loops, if statements) and the top level consists of the functions (main method and declared methods). The new algorithm automatically generates valid variations of program chunks. Variations of each chunk are collectively defined as the equivalence set of the chunk. The algorithm further creates a space of alternative solutions to the entire program by computing the concatenation of the language of the chunks that make up the program. The space of solutions generated by the new algorithm represents the alternative ways a novice may uniquely write a program using the same algorithm as the lecturer but making different choices of language constructs and plan composition while exercising his/her limited or extensive knowledge of the rules of discourse.

7.1 t h e a lt e r nat i v e p r o g r a m s e n u m e r at i o n a l g o r i t h m

In this section we present an algorithm for enumerating equivalent novice programs, based on the previously specified formalism. The idea is to take a lecturer’s program and preprocess it, then look up the equivalent sets of its language constructs, and formulate alternative implementations of the same program from the product of its different alternative parts (statements and chunks). This process can be further broken down into four major opera- tions:

1. preprocessing, granulation and source text recognition, 2. alternate program generation,

3. enumeration of valid reordered plans, and 4. elimination of incorrect programs.

p r e p r o c e s s i n g, granulation, and recognition. Preprocessing is the cleaning up of source code by removing redundant text such as comments. Granulation is the breaking down of language constructs

to the lowest atomic operations. Source text recognition is using reg- ular expressions to recognize statements of the program text. To per- form these operations on programs, we have adapted the preprocessor, granulator and text recognition modules used by NOPRON for these operations.

a lt e r nat e p r o g r a m s g e n e r at i o n. This is the generation of alterna- tive programs using the new program equivalence formalism.

e n u m e r at i o n o f va l i d r e o r d e r e d p l a n s. Here we used a permuta- tion algorithm that takes thestatement_blockobjects of length n (from the alternate programs generated) and returns a finite set of length n! of reordering.

e l i m i nat i o n o f i n c o r r e c t p r o g r a m s. This involves filtering the set of alternative programs using two elimination functions: FilterOutInvalidBlocks() andFilterWithIO() (See Lines17 and22

of Algorithm 7.1). More on the elimination functions is presented in

Chapter 8.

Figure 7.1: Alternative programs enumeration model

The new algorithm (Alternative Programs Enumerator or APE Algorithm) has two phases as shown in Figure 7.1; the first phase pre-processes,

7.1 the alternative programs enumeration algorithm 75

granulates and recognises the semantic tokens in the program text, while the second phase generates alternate statements, which are chunked up into alternative blocks, and finally alternative programs. This process is expressed inAlgorithm 7.1.

Data:input_program, a lecturer’s program text

Result:altPrograms, a list of alternative programs

1 <ProgramObject> aProgram ←preprocess(input_program); 2 List<ProgramObject> altPrograms ←New List();

3 List<ChunkObject> chunks ←granulate(aProgram); 4 foreach chunk in chunks do

5 List<StmtObject> statements ←granulate(chunk); 6 List<ChunkObject> altChunks ←New List();

7 List<ChunkObject> altReorderedChunks ←New List(); 8 foreach statement in statements do

9 List<StmtObject> alternateStmts ←New List();

10 alternateStmts.add(getAlternativeStatement(statement));

/* make a new chunk of alternative statements */

11 <ChunkObject> aChunk ←New Chunk(); 12 aChunk.linesOfChunk ← altStatements; 13 aChunk.preChunkLines ← prelines;

/* Comment: retain the misc lines */14 altChunks.Add(aChunk);

end

15 altChunks ←cartesianProduct(altChunks);

/* altChunks now holds the product of all possibilities */

/* before adding this chunk, enumerate the valid permutations of lines in it */

16 altReorderedChunks ←getPermutation(altChunks); 17 altReorderedChunks ←filterOutInvalidBlocks

(altReorderedChunks);

18 <ProgramObject> newProgram ←New(); 19 newProgram.chunks ← altReorderedChunks; 20 altPrograms.add(newProgram);

end

/* generate the product of program functions */

21 altPrograms ←getPermutationOfFunctions(altPrograms);

/* reduce programs to IO valid ones */

22 altPrograms ←FilterWithIO(altPrograms);

Result: altPrograms