Need guidance in converting Perl code to Java code

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2008
Posts: 29
Reputation: artemis_f is an unknown quantity at this point 
Solved Threads: 0
artemis_f artemis_f is offline Offline
Light Poster

Need guidance in converting Perl code to Java code

 
0
  #1
Oct 1st, 2008
Hello, I don't know any Perl but I do know Java. I am studying about Push down automatas at the moment and wanted to make a simulator for myself. I have found some code in Perl that does it but I do not know how I can go about using the concepts it has to implement in Java. The following is the Perl code:
  1. #!usr/bin/perl
  2. use Text::ParseWords;
  3. use strict;
  4. use warnings;
  5.  
  6. my (@string, @branches, @stack, %accepts, %alphabet, @rules);
  7.  
  8. # Grabs the filenames for the machine and the word to be run on it.
  9. my $pdaFile = $ARGV[0];
  10. my $input = $ARGV[1];
  11.  
  12. # We use subroutines to parse and verify the data in the input files.
  13. # The machine data is stored in the $machine structure as the keys rules, accepts, alphabet, and startState.
  14. my $machine = readPDA($pdaFile);
  15. # Rules and accepts are extracted from the $machine structure for ease of access.
  16. @rules = @{$machine->{rules}};
  17. %accepts = %{$machine->{accepts}};
  18. # This reads the input file and parses it into an array of strings, with each element being one input symbol.
  19. # It checks to make sure the elements are all in the machine's alphabet.
  20. @string = readInput($input, $machine->{alphabet});
  21.  
  22. # The newstate is a temporary storage point for when a new state is calculated.
  23. my $newstate;
  24. # The usedRule is a temporary storage point for when a new state is calculated.
  25. my $usedRule;
  26. # The changed variable represents whether or the current branch is unfinished.
  27. my $changed = 1;
  28.  
  29. ------ I UNDERSTAND HOW TO DO THE ABOVE------
  30.  
  31. push(@stack, "");
  32. # The top level of the branches array corresponds to each branch of possibilities created by the non-determinism of the PDA.
  33. # Each element contains the conditions of the machine for that branched possibility.
  34. # The first element of each collection is the state of the branch.
  35. $branches[0][0] = $machine->{startState};
  36. # The second element is how much of the input string the branch has read.
  37. $branches[0][1] = 0;
  38. # The third element is an array containing the stack for that branch.
  39. $branches[0][2][0] = "";
  40. # Now that the first branch is initialized, the processing can begin
  41.  
  42. for (my $i = 0; $i < @branches; $i++)
  43. {
  44. # When we start a branch, print the branch number
  45. print "\nBeginning branch ".$i.".\n";
  46. # As long as things keep changing, keep cycling through the rules.
  47. while($changed)
  48. {
  49. # Unless it changes while going through the rules, this branch will quit.
  50. $changed = 0;
  51. # The input word is printed, with the next symbol highlighted.
  52. print "Input: @string[0..$branches[$i][1]-1] <".$string[$branches[$i][1]]."> @string[$branches[$i][1]+1..@string-1]\n";
  53. # The current state of the stack is printed.
  54. print "Stack: @{$branches[$i][2]}\n";
  55. # A new state is calculated by checking conditions against the list of rules
  56. for my $rNum (0..@rules-1)
  57. {
  58. # print "::$rules[$rNum][0]??$branches[$i][0]";
  59. # print "::$rules[$rNum][1]??$string[$branches[$i][1]]";
  60. # print "::$rules[$rNum][2]??".${$branches[$i][2]}[@{$branches[$i][2]}-1]."::\n";
  61. # Checks the current state, input, and top stack item against the rule
  62. if (($rules[$rNum][0] eq $branches[$i][0]) and
  63. (($rules[$rNum][1] eq "e") or ($rules[$rNum][1] eq $string[$branches[$i][1]])) and
  64. (($rules[$rNum][2] eq "e") or ($rules[$rNum][2] eq ${$branches[$i][2]}[@{$branches[$i][2]}-1])))
  65. {
  66. if ($changed == 0)
  67. {
  68. # Set the new state.
  69. $newstate = $rules[$rNum][3];
  70. # The state transition is printed.
  71. print "State: ".$branches[$i][0]." -> ".$newstate."\n\n";
  72. $changed = 1;
  73. # Because possible branched depend on this state, we can't update it yet.
  74. # When we can update this state, $usedRule will help us remember which rule to base those updates on.
  75. $usedRule = $rNum;
  76. }
  77. else
  78. {
  79. # Set the new state.
  80. my $branchState = $rules[$rNum][3];
  81. # The state transition is printed.
  82. print "(branching) State: ".$branches[$i][0]." -> ".$branchState."\n\n";
  83. my $newBranch = @branches;
  84. # The state in the new branch is set.
  85. $branches[$newBranch][0] = $branchState;
  86. # The new branch starts with the same string position as the old branch,
  87. $branches[$newBranch][1] = $branches[$i][1];
  88. # and the same stack, so the stack has to be replicated.
  89. @{$branches[$newBranch][2]} = @{$branches[$i][2]};
  90. # If we read a symbol from the input to make the transition,
  91. unless ($rules[$rNum][1] eq "e")
  92. {
  93. # then we should move to the next symbol.
  94. $branches[$newBranch][1]++;
  95. }
  96. # If we used an element from the stack to make the transition,
  97. unless ($rules[$rNum][2] eq "e")
  98. {
  99. # then it's used up and should be removed.
  100. pop(@{$branches[$newBranch][2]});
  101. }
  102. # If the rule adds something to the stack,
  103. unless ($rules[$rNum][4] eq "e")
  104. {
  105. # then it gets added.
  106. push(@{$branches[$newBranch][2]}, $rules[$rNum][4]);
  107. }
  108. }
  109. }
  110. }
  111. # Now that any branching has been finished, we can update the original branch.
  112. if ($changed)
  113. {
  114. # If we read a symbol from the input to make the transition,
  115. unless ($rules[$usedRule][1] eq "e")
  116. {
  117. # then we should move to the next symbol.
  118. $branches[$i][1]++;
  119. }
  120. # If we used an element from the stack to make the transition,
  121. unless ($rules[$usedRule][2] eq "e")
  122. {
  123. # then it's used up and should be removed.
  124. pop(@{$branches[$i][2]});
  125. }
  126. # If the rule adds something to the stack,
  127. unless ($rules[$usedRule][4] eq "e")
  128. {
  129. # then it gets added.
  130. push(@{$branches[$i][2]}, $rules[$usedRule][4]);
  131. }
  132. # The state changes to the new state.
  133. $branches[$i][0] = $newstate;
  134. }
  135. }
  136. # When the input is exhausted, the branch is in its final state.
  137. print "Final state of branch ".$i." is ".$branches[$i][0]."\n";
  138. # If that state is in the accept states list, the machine accepts the input and halts.
  139. if ((defined($accepts{$branches[$i][0]})) and ($branches[$i][1] == @string-1))
  140. {
  141. print "The machine accepts the string.\n";
  142. exit;
  143. }
  144. # If that state doesn't, point it out.
  145. else { print "The branch does not accept the string.\n"; }
  146. # And move on.
  147. $changed = 1;
  148. }
  149. print "The machine does not accept the string.\n";
  150.  
  151. ###################################################

The two methods this references ReadPDA and ReadInput can be found here:
http://en.wikibooks.org/wiki/Computa...Free_Languages

I don't need help with that as I know how to do that in Java already. For the function above I know the beginning bit but after that I am lost on what to do in Java. I need explaination of what kind of data structures to use for "branch" (what is it? Is it a global variable? Is it a method that takes attributes??) and if someone can explain in Java terms what to do with the lines or help me get started it would be great.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Need guidance in converting Perl code to Java code

 
0
  #2
Oct 1st, 2008
@branch is an array of arrays in the perl code. It is a global variable in the above code, but global only to that perl script. Its a lexical variable that is scoped to the entire script so in essence it is global, but still only global to that script. It is not a method, it is just an array. I can't help you with translation to JAVA because I don't know any JAVA.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Perl Forum
Thread Tools Search this Thread



Tag cloud for Perl
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC