Brauche Hilfe bei Übersetzung von Java in VB.NET - Regex, Pattern u. ä.

  • VB.NET

Es gibt 11 Antworten in diesem Thema. Der letzte Beitrag () ist von ErfinderDesRades.

    Brauche Hilfe bei Übersetzung von Java in VB.NET - Regex, Pattern u. ä.

    Hallo, ich bin gerade dabei, einen auto-generierten Code in VB.NET zu übersetzen. Das läuft soweit ganz gut, nur auf dem Gebiet "Regular Expression" habe ich gar keine Ahnung. Das Problem ist, zu genutzten Java-Klassen die nötigen .NET-Gegenstücke zu finden. Für jemanden mit Ahnung bestimmt kein Problem.

    Hier die Methode, die von einem Online-Übersetzer automatisch in VB.NET übersetzt wurde:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports java.io.Star
    2. Imports java.util.Star
    3. Imports java.util.regex.Star
    4. Class V1
    5. '....
    6. Public Function FindToken(ByVal Rest As String) As AToken
    7. Dim Results As ArrayList(Of Integer) = New ArrayList(Of Integer)
    8. Dim ResultsV As ArrayList(Of String) = New ArrayList(Of String)
    9. Try
    10. Dim p As Pattern
    11. Dim m As Matcher
    12. p = Pattern.compile("^(\\()")
    13. m = p.matcher(Rest)
    14. If m.find Then
    15. Results.add(New Integer(Me.t_CharKlammerAuf))
    16. ResultsV.add(m.group)
    17. End If
    18. p = Pattern.compile("^(\\))")
    19. m = p.matcher(Rest)
    20. If m.find Then
    21. Results.add(New Integer(Me.t_CharKlammerZu))
    22. ResultsV.add(m.group)
    23. End If
    24. p = Pattern.compile("^(0)")
    25. m = p.matcher(Rest)
    26. If m.find Then
    27. Results.add(New Integer(Me.t_T_0))
    28. ResultsV.add(m.group)
    29. End If
    30. p = Pattern.compile("^(1)")
    31. m = p.matcher(Rest)
    32. If m.find Then
    33. Results.add(New Integer(Me.t_T_1))
    34. ResultsV.add(m.group)
    35. End If
    36. p = Pattern.compile("^(or)")
    37. m = p.matcher(Rest)
    38. If m.find Then
    39. Results.add(New Integer(Me.t_or))
    40. ResultsV.add(m.group)
    41. End If
    42. p = Pattern.compile("^(and)")
    43. m = p.matcher(Rest)
    44. If m.find Then
    45. Results.add(New Integer(Me.t_and))
    46. ResultsV.add(m.group)
    47. End If
    48. p = Pattern.compile("^(!)")
    49. m = p.matcher(Rest)
    50. If m.find Then
    51. Results.add(New Integer(Me.t_CharNot))
    52. ResultsV.add(m.group)
    53. End If
    54. p = Pattern.compile("^(gdw)")
    55. m = p.matcher(Rest)
    56. If m.find Then
    57. Results.add(New Integer(Me.t_gdw))
    58. ResultsV.add(m.group)
    59. End If
    60. p = Pattern.compile("^([\"& vbCr&"\"& vbLf&"\"& vbTab&"\\s])")
    61. m = p.matcher(Rest)
    62. If m.find Then
    63. Results.add(New Integer(Me.t_ignore))
    64. ResultsV.add(m.group)
    65. End If
    66. Catch e As Exception
    67. End Try
    68. Dim maxlength As Integer = 0
    69. Dim besttoken As Integer = 0
    70. Dim ret As AToken = New AToken
    71. ret.token = besttoken
    72. Dim i As Integer = 0
    73. Do While (i < Results.size)
    74. If (ResultsV.get(i).toString.length > maxlength) Then
    75. maxlength = ResultsV.get(i).toString.length
    76. besttoken = CType(Results.get(i),Integer).intValue
    77. ret.token = besttoken
    78. If (besttoken <> 0) Then
    79. ret.val = ResultsV.get(i).toString
    80. End If
    81. End If
    82. i = (i + 1)
    83. Loop
    84. Return ret
    85. End Function


    Kann mir jemand sagen, wo es die Typen "Pattern" und "Matcher" in .NET gibt und wie ich das entsprechend einbauen muss?

    Besten Dank!

    *Topic verschoben, Spoiler eingefügt (Code sehr lang)*
    „Was ich gerade mache? Ich reite Hand in Hand mit dem Matthäus-Effekt auf einer Exponentialfunktion.“ ^^

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Marcus Gräfe“ ()

    Was du sichst ist die Klasse RegEx aus dem Namespace System.Text.RegularExpressions.
    Außerdem: Mach das Try-Catch weg und verwende keine ArrayList, sondern eine List(Of T)
    In general (across programming languages), a pointer is a number that represents a physical location in memory. A nullpointer is (almost always) one that points to 0, and is widely recognized as "not pointing to anything". Since systems have different amounts of supported memory, it doesn't always take the same number of bytes to hold that number, so we call a "native size integer" one that can hold a pointer on any particular system. - Sam Harwell
    Die Klasse habe ich auch bereits gefunden, nur meckert er dann:
    "System.Text.RegularExpressions.Regex.pattern" ist in diesem Kontext nicht zugreifbar, da es "Protected Friend" ist.

    Und was entspricht dem ".Get" in einer "List(Of T)"?
    „Was ich gerade mache? Ich reite Hand in Hand mit dem Matthäus-Effekt auf einer Exponentialfunktion.“ ^^
    Entweder der zugriff über index: list(2) in vb, liste[2] in c# oder über die property Item:

    VB.NET-Quellcode

    1. Dim liste = New List(Of String)
    2. Dim item1 = liste(1)
    3. Dim item2 = liste.Item(1)
    In general (across programming languages), a pointer is a number that represents a physical location in memory. A nullpointer is (almost always) one that points to 0, and is widely recognized as "not pointing to anything". Since systems have different amounts of supported memory, it doesn't always take the same number of bytes to hold that number, so we call a "native size integer" one that can hold a pointer on any particular system. - Sam Harwell
    Jetzt habe ich nur noch das Problem, die Typen "Pattern" und "Matcher" fehlen.

    Und wenn ich Regex importiere, kommt eben der Fehler:
    "System.Text.RegularExpressions.Regex.pattern" ist in diesem Kontext nicht zugreifbar, da es "Protected Friend" ist.


    „Was ich gerade mache? Ich reite Hand in Hand mit dem Matthäus-Effekt auf einer Exponentialfunktion.“ ^^

    ErfinderDesRades schrieb:

    Also wenn das "kommt", dann haste wohl einen Fehler in deim Code.
    Korrigiere den Code, dann "kommt" das nicht mehr.


    Und genau deswegen habe ich ja das Thema erstellt, weil der "Fehler" ist, dass die Typen "Pattern" und "Matcher" so nicht in VB.NET existieren. Da ich mich auf dem Gebiet nicht auskenne, ist meine Frage ob es zu diesen Java-Typen äquivalente Typen in .NET gibt.
    „Was ich gerade mache? Ich reite Hand in Hand mit dem Matthäus-Effekt auf einer Exponentialfunktion.“ ^^
    Hier mal der komplette Auto-generierte Code von AtoCC (Visual Compiler Compiler):
    (Es geht um die unterste Methode)
    Spoiler anzeigen

    Quellcode

    1. import java.io.*;
    2. import java.util.*;
    3. import java.util.regex.*;
    4. class V1
    5. {
    6. YYARec[] yya;
    7. YYARec[] yyg;
    8. YYRRec[] yyr;
    9. int[] yyd;
    10. int[] yyal;
    11. int[] yyah;
    12. int[] yygl;
    13. int[] yygh;
    14. int yyn = 0;
    15. int yystate = 0;
    16. int yychar = -1;
    17. int yynerrs = 0;
    18. int yyerrflag = 0;
    19. int yysp = 0;
    20. int yymaxdepth = 1024;
    21. int yyflag = 0;
    22. int yyfnone = 0;
    23. int[] yys = new int[1024];
    24. String[] yyv = new String[1024];
    25. String yyval = "";
    26. /*public static class TOutput extends PrintWriter {
    27. public TOutput(OutputStreamWriter o) { super(o); }
    28. public TOutput(BufferedWriter b) { super(b); }
    29. public TOutput(PrintStream s) { super(s); }
    30. public void WriteLine(String s) { println(s); }
    31. }
    32. TOutput Output; */
    33. String Output;
    34. class YYARec
    35. {
    36. public int sym;
    37. public int act;
    38. public YYARec (int s, int a){ sym = s; act = a; }
    39. }
    40. class YYRRec
    41. {
    42. public int len;
    43. public int sym;
    44. public YYRRec (int l, int s){ sym = s; len = l; }
    45. }
    46. ////////////////////////////////////////////////////////////////
    47. /// Constant values / tokens
    48. ////////////////////////////////////////////////////////////////
    49. int t_CharKlammerAuf = 257;
    50. int t_CharKlammerZu = 258;
    51. int t_T_0 = 259;
    52. int t_T_1 = 260;
    53. int t_or = 261;
    54. int t_and = 262;
    55. int t_CharNot = 263;
    56. int t_gdw = 264;
    57. int t_ignore = 256;
    58. ///////////////////////////////////////////////////////////
    59. /// Global settings:
    60. ///////////////////////////////////////////////////////////
    61. /*Verstaendnis-Sachen:
    62. $$ := Rückgabewert
    63. $1 := 1. Argument
    64. $2 := 2. Argument */
    65. ///////////////////////////////////////////////////////////
    66. /// <summary>
    67. /// Der Haupteinstiegspunkt für die Anwendung.
    68. /// </summary>
    69. public static void main(String[] args)
    70. {
    71. V1 compiler = new V1();
    72. Scanner inp = new Scanner (System.in);
    73. String inputstream = inp.nextLine();
    74. ////////////////////////////////////////////////////////////////
    75. /// Compiler Code:
    76. ////////////////////////////////////////////////////////////////
    77. if (!compiler.Scanner(inputstream)) {
    78. System.out.println("Error while scanning");
    79. System.exit(1);
    80. }
    81. compiler.InitTables();
    82. if(!compiler.yyparse()) {
    83. System.out.println("Error while parsing");
    84. System.exit(1);
    85. }
    86. System.out.println(compiler.Output);
    87. }
    88. public void yyaction (int yyruleno)
    89. {
    90. switch (yyruleno)
    91. {
    92. ////////////////////////////////////////////////////////////////
    93. /// YYAction code:
    94. ////////////////////////////////////////////////////////////////
    95. case 1 :
    96. //expression -> term A
    97. //Bedeutung: yyval = yyv[yysp-1] or yyv[yysp-0]
    98. if (yyv[yysp-0].equals("1")){
    99. yyval = "1";
    100. }else{
    101. yyval = yyv[yysp-1];
    102. }
    103. break;
    104. case 2 :
    105. //expression -> term
    106. yyval = yyv[yysp-0];
    107. break;
    108. case 3 :
    109. //A -> oder term
    110. yyval = yyv[yysp-0];
    111. break;
    112. case 4 :
    113. // A -> oder term A
    114. if (yyv[yysp-0].equals("1")){
    115. yyval = "1";
    116. }else{
    117. yyval = yyv[yysp-1];
    118. }
    119. break;
    120. case 5 :
    121. //term -> factor B
    122. // Bedeutung: yyval = yyv[yysp-1] and yyv[yysp-0]
    123. if (yyv[yysp-0].equals("0")){
    124. yyval = "0";
    125. }else{
    126. yyval = yyv[yysp-1];
    127. }
    128. break;
    129. case 6 :
    130. //term -> factor
    131. yyval = yyv[yysp-0];
    132. break;
    133. case 7 :
    134. // B -> und factor
    135. yyval = yyv[yysp-0];
    136. break;
    137. case 8 :
    138. // B -> und factor B
    139. if (yyv[yysp-0].equals("0")){
    140. yyval = "0";
    141. }else{
    142. yyval = yyv[yysp-1];
    143. }
    144. break;
    145. case 9 :
    146. //factor -> ( S )
    147. yyval = yyv[yysp-1];
    148. break;
    149. case 10 :
    150. //factor -> constant
    151. yyval = yyv[yysp-0];
    152. break;
    153. case 11 :
    154. //factor -> not factor
    155. if (yyv[yysp-0].equals("0")){
    156. yyval = "1";
    157. }else{
    158. yyval = "0";
    159. }
    160. break;
    161. case 12 :
    162. yyval = yyv[yysp-0];
    163. break;
    164. case 13 :
    165. yyval = yyv[yysp-0];
    166. break;
    167. case 14 :
    168. yyval = yyv[yysp-0];
    169. break;
    170. case 15 :
    171. yyval = yyv[yysp-0];
    172. break;
    173. case 16 :
    174. //expression -> term A
    175. if (yyv[yysp-0].equals("1")){
    176. yyval = "0";
    177. }else{
    178. yyval = yyv[yysp-0];
    179. }
    180. break;
    181. case 17 :
    182. yyval = yyv[yysp-0];
    183. break;
    184. default : return;
    185. }
    186. }
    187. public void InitTables()
    188. {
    189. ////////////////////////////////////////////////////////////////
    190. /// Init Table code:
    191. ////////////////////////////////////////////////////////////////
    192. int yynacts = 36;
    193. int yyngotos = 28;
    194. int yynstates = 23;
    195. int yynrules = 17;
    196. yya = new YYARec[yynacts+1]; int yyac = 1;
    197. yyg = new YYARec[yyngotos+1]; int yygc = 1;
    198. yyr = new YYRRec[yynrules+1]; int yyrc = 1;
    199. yya[yyac] = new YYARec(257,6);yyac++;
    200. yya[yyac] = new YYARec(259,7);yyac++;
    201. yya[yyac] = new YYARec(260,8);yyac++;
    202. yya[yyac] = new YYARec(263,9);yyac++;
    203. yya[yyac] = new YYARec(257,6);yyac++;
    204. yya[yyac] = new YYARec(259,7);yyac++;
    205. yya[yyac] = new YYARec(260,8);yyac++;
    206. yya[yyac] = new YYARec(263,9);yyac++;
    207. yya[yyac] = new YYARec(262,13);yyac++;
    208. yya[yyac] = new YYARec(0,-6 );yyac++;
    209. yya[yyac] = new YYARec(258,-6 );yyac++;
    210. yya[yyac] = new YYARec(261,-6 );yyac++;
    211. yya[yyac] = new YYARec(261,16);yyac++;
    212. yya[yyac] = new YYARec(0,-2 );yyac++;
    213. yya[yyac] = new YYARec(258,-2 );yyac++;
    214. yya[yyac] = new YYARec(0,0);yyac++;
    215. yya[yyac] = new YYARec(257,6);yyac++;
    216. yya[yyac] = new YYARec(259,7);yyac++;
    217. yya[yyac] = new YYARec(260,8);yyac++;
    218. yya[yyac] = new YYARec(263,9);yyac++;
    219. yya[yyac] = new YYARec(257,6);yyac++;
    220. yya[yyac] = new YYARec(259,7);yyac++;
    221. yya[yyac] = new YYARec(260,8);yyac++;
    222. yya[yyac] = new YYARec(263,9);yyac++;
    223. yya[yyac] = new YYARec(257,6);yyac++;
    224. yya[yyac] = new YYARec(259,7);yyac++;
    225. yya[yyac] = new YYARec(260,8);yyac++;
    226. yya[yyac] = new YYARec(263,9);yyac++;
    227. yya[yyac] = new YYARec(258,20);yyac++;
    228. yya[yyac] = new YYARec(262,13);yyac++;
    229. yya[yyac] = new YYARec(0,-7 );yyac++;
    230. yya[yyac] = new YYARec(258,-7 );yyac++;
    231. yya[yyac] = new YYARec(261,-7 );yyac++;
    232. yya[yyac] = new YYARec(261,16);yyac++;
    233. yya[yyac] = new YYARec(0,-3 );yyac++;
    234. yya[yyac] = new YYARec(258,-3 );yyac++;
    235. yyg[yygc] = new YYARec(-10,1);yygc++;
    236. yyg[yygc] = new YYARec(-9,2);yygc++;
    237. yyg[yygc] = new YYARec(-6,3);yygc++;
    238. yyg[yygc] = new YYARec(-3,4);yygc++;
    239. yyg[yygc] = new YYARec(-2,5);yygc++;
    240. yyg[yygc] = new YYARec(-10,1);yygc++;
    241. yyg[yygc] = new YYARec(-9,2);yygc++;
    242. yyg[yygc] = new YYARec(-6,10);yygc++;
    243. yyg[yygc] = new YYARec(-8,11);yygc++;
    244. yyg[yygc] = new YYARec(-7,12);yygc++;
    245. yyg[yygc] = new YYARec(-5,14);yygc++;
    246. yyg[yygc] = new YYARec(-4,15);yygc++;
    247. yyg[yygc] = new YYARec(-10,1);yygc++;
    248. yyg[yygc] = new YYARec(-9,2);yygc++;
    249. yyg[yygc] = new YYARec(-6,3);yygc++;
    250. yyg[yygc] = new YYARec(-3,4);yygc++;
    251. yyg[yygc] = new YYARec(-2,17);yygc++;
    252. yyg[yygc] = new YYARec(-10,1);yygc++;
    253. yyg[yygc] = new YYARec(-9,2);yygc++;
    254. yyg[yygc] = new YYARec(-6,18);yygc++;
    255. yyg[yygc] = new YYARec(-10,1);yygc++;
    256. yyg[yygc] = new YYARec(-9,2);yygc++;
    257. yyg[yygc] = new YYARec(-6,3);yygc++;
    258. yyg[yygc] = new YYARec(-3,19);yygc++;
    259. yyg[yygc] = new YYARec(-8,11);yygc++;
    260. yyg[yygc] = new YYARec(-7,21);yygc++;
    261. yyg[yygc] = new YYARec(-5,14);yygc++;
    262. yyg[yygc] = new YYARec(-4,22);yygc++;
    263. yyd = new int[yynstates];
    264. yyd[0] = 0;
    265. yyd[1] = 0;
    266. yyd[2] = -10;
    267. yyd[3] = 0;
    268. yyd[4] = 0;
    269. yyd[5] = 0;
    270. yyd[6] = 0;
    271. yyd[7] = -12;
    272. yyd[8] = -13;
    273. yyd[9] = -16;
    274. yyd[10] = -11;
    275. yyd[11] = 0;
    276. yyd[12] = -5;
    277. yyd[13] = -15;
    278. yyd[14] = 0;
    279. yyd[15] = -1;
    280. yyd[16] = -14;
    281. yyd[17] = 0;
    282. yyd[18] = 0;
    283. yyd[19] = 0;
    284. yyd[20] = -9;
    285. yyd[21] = -8;
    286. yyd[22] = -4;
    287. yyal = new int[yynstates];
    288. yyal[0] = 1;
    289. yyal[1] = 5;
    290. yyal[2] = 9;
    291. yyal[3] = 9;
    292. yyal[4] = 13;
    293. yyal[5] = 16;
    294. yyal[6] = 17;
    295. yyal[7] = 21;
    296. yyal[8] = 21;
    297. yyal[9] = 21;
    298. yyal[10] = 21;
    299. yyal[11] = 21;
    300. yyal[12] = 25;
    301. yyal[13] = 25;
    302. yyal[14] = 25;
    303. yyal[15] = 29;
    304. yyal[16] = 29;
    305. yyal[17] = 29;
    306. yyal[18] = 30;
    307. yyal[19] = 34;
    308. yyal[20] = 37;
    309. yyal[21] = 37;
    310. yyal[22] = 37;
    311. yyah = new int[yynstates];
    312. yyah[0] = 4;
    313. yyah[1] = 8;
    314. yyah[2] = 8;
    315. yyah[3] = 12;
    316. yyah[4] = 15;
    317. yyah[5] = 16;
    318. yyah[6] = 20;
    319. yyah[7] = 20;
    320. yyah[8] = 20;
    321. yyah[9] = 20;
    322. yyah[10] = 20;
    323. yyah[11] = 24;
    324. yyah[12] = 24;
    325. yyah[13] = 24;
    326. yyah[14] = 28;
    327. yyah[15] = 28;
    328. yyah[16] = 28;
    329. yyah[17] = 29;
    330. yyah[18] = 33;
    331. yyah[19] = 36;
    332. yyah[20] = 36;
    333. yyah[21] = 36;
    334. yyah[22] = 36;
    335. yygl = new int[yynstates];
    336. yygl[0] = 1;
    337. yygl[1] = 6;
    338. yygl[2] = 9;
    339. yygl[3] = 9;
    340. yygl[4] = 11;
    341. yygl[5] = 13;
    342. yygl[6] = 13;
    343. yygl[7] = 18;
    344. yygl[8] = 18;
    345. yygl[9] = 18;
    346. yygl[10] = 18;
    347. yygl[11] = 18;
    348. yygl[12] = 21;
    349. yygl[13] = 21;
    350. yygl[14] = 21;
    351. yygl[15] = 25;
    352. yygl[16] = 25;
    353. yygl[17] = 25;
    354. yygl[18] = 25;
    355. yygl[19] = 27;
    356. yygl[20] = 29;
    357. yygl[21] = 29;
    358. yygl[22] = 29;
    359. yygh = new int[yynstates];
    360. yygh[0] = 5;
    361. yygh[1] = 8;
    362. yygh[2] = 8;
    363. yygh[3] = 10;
    364. yygh[4] = 12;
    365. yygh[5] = 12;
    366. yygh[6] = 17;
    367. yygh[7] = 17;
    368. yygh[8] = 17;
    369. yygh[9] = 17;
    370. yygh[10] = 17;
    371. yygh[11] = 20;
    372. yygh[12] = 20;
    373. yygh[13] = 20;
    374. yygh[14] = 24;
    375. yygh[15] = 24;
    376. yygh[16] = 24;
    377. yygh[17] = 24;
    378. yygh[18] = 26;
    379. yygh[19] = 28;
    380. yygh[20] = 28;
    381. yygh[21] = 28;
    382. yygh[22] = 28;
    383. yyr[yyrc] = new YYRRec(2,-2);yyrc++;
    384. yyr[yyrc] = new YYRRec(1,-2);yyrc++;
    385. yyr[yyrc] = new YYRRec(2,-4);yyrc++;
    386. yyr[yyrc] = new YYRRec(3,-4);yyrc++;
    387. yyr[yyrc] = new YYRRec(2,-3);yyrc++;
    388. yyr[yyrc] = new YYRRec(1,-3);yyrc++;
    389. yyr[yyrc] = new YYRRec(2,-7);yyrc++;
    390. yyr[yyrc] = new YYRRec(3,-7);yyrc++;
    391. yyr[yyrc] = new YYRRec(3,-6);yyrc++;
    392. yyr[yyrc] = new YYRRec(1,-6);yyrc++;
    393. yyr[yyrc] = new YYRRec(2,-6);yyrc++;
    394. yyr[yyrc] = new YYRRec(1,-9);yyrc++;
    395. yyr[yyrc] = new YYRRec(1,-9);yyrc++;
    396. yyr[yyrc] = new YYRRec(1,-5);yyrc++;
    397. yyr[yyrc] = new YYRRec(1,-8);yyrc++;
    398. yyr[yyrc] = new YYRRec(1,-10);yyrc++;
    399. yyr[yyrc] = new YYRRec(1,-11);yyrc++;
    400. }
    401. public boolean yyact(int state, int sym)
    402. {
    403. int k = yyal[state];
    404. while ( k <= yyah[state] && yya[k].sym != sym) k++;
    405. if (k > yyah[state]) return false;
    406. yyn = yya[k].act;
    407. return true;
    408. }
    409. public boolean yygoto(int state, int sym)
    410. {
    411. int k = yygl[state];
    412. while ( k <= yygh[state] && yyg[k].sym != sym) k++;
    413. if (k > yygh[state]) return false;
    414. yyn = yyg[k].act;
    415. return true;
    416. }
    417. public void yyerror (String s)
    418. {
    419. System.out.println("Error" + s);
    420. }
    421. int yylexpos = -1;
    422. String yylval = "";
    423. public int yylex ()
    424. {
    425. yylexpos++;
    426. if(yylexpos >= TokenList.size())
    427. {
    428. yylval = "";
    429. return 0;
    430. }
    431. else
    432. {
    433. yylval = ((AToken)TokenList.get(yylexpos)).val;
    434. return ((AToken)TokenList.get(yylexpos)).token;
    435. }
    436. }
    437. public boolean yyparse ()
    438. {
    439. boolean jumpToNext = false;
    440. while(true){ // Parse:
    441. if (!jumpToNext){
    442. yysp++;
    443. if (yysp>yymaxdepth)
    444. {
    445. yyerror("yyparse stack overflow");
    446. return false;
    447. }
    448. yys[yysp] = yystate;
    449. yyv[yysp] = yyval;
    450. }
    451. jumpToNext = false;
    452. if (yyd[yystate]==0 && yychar==-1)
    453. {
    454. yychar = yylex();
    455. if (yychar<0) yychar = 0;
    456. }
    457. yyn = yyd[yystate];
    458. if (yyn != 0) {
    459. /// reduce:
    460. yyflag = yyfnone;
    461. yyaction(-yyn);
    462. yysp -= yyr[-yyn].len;
    463. if (yygoto(yys[yysp], yyr[-yyn].sym)) yystate = yyn;
    464. switch (yyflag)
    465. {
    466. case 1 : return true;
    467. case 2 : return false;
    468. case 3 : {
    469. int r = yy_errlab();
    470. if (r == 0) return false;
    471. if (r == 1) continue;
    472. if (r == 2) {jumpToNext = true; continue; }
    473. }
    474. }
    475. continue; // PARSE:
    476. ///////////////
    477. }
    478. if (! yyact(yystate, yychar)) {
    479. if (yyerrflag==0) yyerror("syntax error");
    480. int r = yy_errlab();
    481. if (r == 0) return false;
    482. if (r == 1) continue;
    483. if (r == 2) {jumpToNext = true; continue; }
    484. }
    485. else if (yyn>0) {
    486. /// shift:
    487. yystate = yyn;
    488. yychar = -1;
    489. yyval = yylval;
    490. if (yyerrflag>0) yyerrflag--;
    491. continue; // PARSE:
    492. ///////////////
    493. }
    494. else if (yyn<0) {
    495. /// reduce:
    496. yyflag = yyfnone;
    497. yyaction(-yyn);
    498. yysp -= yyr[-yyn].len;
    499. if (yygoto(yys[yysp], yyr[-yyn].sym)) yystate = yyn;
    500. switch (yyflag)
    501. {
    502. case 1 : return true;
    503. case 2 : return false;
    504. case 3 : {
    505. int r = yy_errlab();
    506. if (r == 0) return false;
    507. if (r == 1) continue;
    508. if (r == 2) {jumpToNext = true; continue; }
    509. }
    510. }
    511. continue; // PARSE:
    512. ///////////////
    513. }
    514. else return true;
    515. } // WHILE Parse:
    516. }
    517. public int yy_errlab (){
    518. if (yyerrflag==0) yynerrs++;
    519. if (yyerrflag<=2)
    520. {
    521. yyerrflag = 3;
    522. while (yysp>0 && !(yyact(yys[yysp], 255) && yyn > 0)) yysp--;
    523. if (yysp==0) return 0;
    524. yystate = yyn;
    525. return 1; // PARSE:
    526. }
    527. else
    528. {
    529. if (yychar==0) return 0;
    530. yychar = -1;
    531. return 2;
    532. }
    533. }
    534. ////////////////////////////////////////////////////////////////
    535. /// Scanner
    536. ////////////////////////////////////////////////////////////////
    537. public class AToken
    538. {
    539. public int token;
    540. public String val;
    541. }
    542. ArrayList<AToken> TokenList = new ArrayList<AToken>();
    543. public boolean Scanner (String Input)
    544. {
    545. if (Input.length() == 0) return true;
    546. TokenList = new ArrayList<AToken>();
    547. while (1==1)
    548. {
    549. AToken lasttoken = FindToken(Input);
    550. if (lasttoken.token == 0) break;
    551. if (lasttoken.token != t_ignore) TokenList.add(lasttoken);
    552. if (Input.length() > lasttoken.val.length())
    553. Input = Input.substring(lasttoken.val.length()); else return true;
    554. }
    555. System.out.println(Input);
    556. System.out.println();
    557. System.out.println("No matching token found!");
    558. return false;
    559. }
    560. public AToken FindToken (String Rest)
    561. {
    562. ArrayList<Integer> Results = new ArrayList<Integer>();
    563. ArrayList<String> ResultsV = new ArrayList<String>();
    564. try{
    565. Pattern p;
    566. Matcher m;
    567. p = Pattern.compile("^(\\()");
    568. m = p.matcher(Rest);
    569. if (m.find()){
    570. Results.add (new Integer(t_CharKlammerAuf));
    571. ResultsV.add(m.group());}
    572. p = Pattern.compile("^(\\))");
    573. m = p.matcher(Rest);
    574. if (m.find()){
    575. Results.add (new Integer(t_CharKlammerZu));
    576. ResultsV.add(m.group());}
    577. p = Pattern.compile("^(0)");
    578. m = p.matcher(Rest);
    579. if (m.find()){
    580. Results.add (new Integer(t_T_0));
    581. ResultsV.add(m.group());}
    582. p = Pattern.compile("^(1)");
    583. m = p.matcher(Rest);
    584. if (m.find()){
    585. Results.add (new Integer(t_T_1));
    586. ResultsV.add(m.group());}
    587. p = Pattern.compile("^(or)");
    588. m = p.matcher(Rest);
    589. if (m.find()){
    590. Results.add (new Integer(t_or));
    591. ResultsV.add(m.group());}
    592. p = Pattern.compile("^(and)");
    593. m = p.matcher(Rest);
    594. if (m.find()){
    595. Results.add (new Integer(t_and));
    596. ResultsV.add(m.group());}
    597. p = Pattern.compile("^(!)");
    598. m = p.matcher(Rest);
    599. if (m.find()){
    600. Results.add (new Integer(t_CharNot));
    601. ResultsV.add(m.group());}
    602. p = Pattern.compile("^(gdw)");
    603. m = p.matcher(Rest);
    604. if (m.find()){
    605. Results.add (new Integer(t_gdw));
    606. ResultsV.add(m.group());}
    607. p = Pattern.compile("^([\\r\\n\\t\\s])");
    608. m = p.matcher(Rest);
    609. if (m.find()){
    610. Results.add (new Integer(t_ignore));
    611. ResultsV.add(m.group());}
    612. } catch (Exception e) {}
    613. int maxlength = 0;
    614. int besttoken = 0;
    615. AToken ret = new AToken();
    616. ret.token = besttoken;
    617. for (int i = 0; i < Results.size(); i++){
    618. if (ResultsV.get(i).toString().length() > maxlength)
    619. {
    620. maxlength = ResultsV.get(i).toString().length();
    621. besttoken = ((Integer)Results.get(i)).intValue();
    622. ret.token = besttoken;
    623. if (besttoken != 0)
    624. ret.val = ResultsV.get(i).toString();
    625. }
    626. }
    627. return ret;
    628. }
    629. }



    Ich möchte eigentlich diesen Code in VB.NET übersetzen, da meine Java-Fähigkeiten stark eingerostet sind. Aber ich habe mich schon mit der Idee angefreundet, es in Java zu lassen und mir da eine passende GUI etc. zu bauen.
    „Was ich gerade mache? Ich reite Hand in Hand mit dem Matthäus-Effekt auf einer Exponentialfunktion.“ ^^
    ja, also scheints hat java da eine Pattern - Klasse, die iwas compilen kann, um reguläre Ausdrücke anzuwenden.

    .net tickt da anders, da gibts die Regex - Klasse, um reguläre Ausdrücke anzuwenden.
    Aber .net tickt auch mitte ArrayList anders, in .Net gibts keine ArrayList(Of T), sondern man nähme List(Of T).

    Bestimmt sind da noch lauter so Ungereimtheiten im Code - scheints lässt sich das Zeugs nur sehr bedingt einfach nach .Net übersetzen...
    Ich habe den Code soweit übersetzt, auch die Listen geändert. Nur das genannte Problem ist noch da - alles andere passt.
    „Was ich gerade mache? Ich reite Hand in Hand mit dem Matthäus-Effekt auf einer Exponentialfunktion.“ ^^
    Na, dass du es übersetzt hättest, ist eine starke Behauptung - solange du es ühaupt nicht testen kannst.

    Aber die letzte Methode habich mal versucht, nach vb zu übertragen - ob das so Sinn ergibt, weiß ich nicht.

    Auch der Java-Code sieht mir nicht sonderlich professionell aus - voll mit lauter Wiederholungen...
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public Class AToken
    2. Public token As Integer
    3. Public val As [String]
    4. End Class
    5. Public Class Scanner
    6. Private t_CharKlammerAuf As Integer = 257
    7. Private t_CharKlammerZu As Integer = 258
    8. Private t_T_0 As Integer = 259
    9. Private t_T_1 As Integer = 260
    10. Private t_or As Integer = 261
    11. Private t_and As Integer = 262
    12. Private t_CharNot As Integer = 263
    13. Private t_gdw As Integer = 264
    14. Private t_ignore As Integer = 256
    15. Public Function FindToken(Rest As [String]) As AToken
    16. Dim Results As New List(Of Integer)()
    17. Dim ResultsV As New List(Of [String])()
    18. Try
    19. Dim p As Regex
    20. Dim m As Match
    21. p = New Regex("^(\()")
    22. m = p.Match(Rest)
    23. If m.Success Then
    24. Results.Add(t_CharKlammerAuf)
    25. ResultsV.Add(m.Value)
    26. End If
    27. p = New Regex("^(\))")
    28. m = p.Match(Rest)
    29. If m.Success Then
    30. Results.Add(t_CharKlammerZu)
    31. ResultsV.Add(m.Value)
    32. End If
    33. p = New Regex("^(0)")
    34. m = p.Match(Rest)
    35. If m.Success Then
    36. Results.Add(t_T_0)
    37. ResultsV.Add(m.Value)
    38. End If
    39. p = New Regex("^(1)")
    40. m = p.Match(Rest)
    41. If m.Success Then
    42. Results.Add(t_T_1)
    43. ResultsV.Add(m.Value)
    44. End If
    45. p = New Regex("^(or)")
    46. m = p.Match(Rest)
    47. If m.Success Then
    48. Results.Add(t_or)
    49. ResultsV.Add(m.Value)
    50. End If
    51. p = New Regex("^(and)")
    52. m = p.Match(Rest)
    53. If m.Success Then
    54. Results.Add(t_and)
    55. ResultsV.Add(m.Value)
    56. End If
    57. p = New Regex("^(!)")
    58. m = p.Match(Rest)
    59. If m.Success Then
    60. Results.Add(t_CharNot)
    61. ResultsV.Add(m.Value)
    62. End If
    63. p = New Regex("^(gdw)")
    64. m = p.Match(Rest)
    65. If m.Success Then
    66. Results.Add(t_gdw)
    67. ResultsV.Add(m.Value)
    68. End If
    69. p = New Regex("^([\r\n\t\s])")
    70. m = p.Match(Rest)
    71. If m.Success Then
    72. Results.Add(t_ignore)
    73. ResultsV.Add(m.Value)
    74. End If
    75. Catch e As Exception
    76. End Try
    77. Dim maxlength As Integer = 0
    78. Dim besttoken As Integer = 0
    79. Dim ret As New AToken()
    80. ret.token = besttoken
    81. For i As Integer = 0 To Results.Count - 1
    82. If ResultsV(i).Length > maxlength Then
    83. maxlength = ResultsV(i).Length
    84. besttoken = Results(i)
    85. ret.token = besttoken
    86. If besttoken <> 0 Then
    87. ret.val = ResultsV(i)
    88. End If
    89. End If
    90. Next
    91. Return ret
    92. End Function
    93. End Class
    Also die Klasse habich da einfach drumrum gemacht - die gehört eiglich nicht da hin, aber es soll ja kompilieren.