Skip to content

Commit 9ba39e5

Browse files
committed
Sort
1 parent 4c5e673 commit 9ba39e5

File tree

6 files changed

+216
-140
lines changed

6 files changed

+216
-140
lines changed

EvalFinale/Analyzer/Analyze.cs

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using EvalFinale.Grammar;
5+
6+
using static EvalFinale.Traductor;
7+
8+
namespace EvalFinale.Analyzer
9+
{
10+
public static class Analyze
11+
{
12+
public static char[] SEPARATORS = {' ', '\n', '\t'};
13+
14+
public static (List<string>, List<string>) LexicalAnalysis(string code, Language language)
15+
{
16+
ILanguage l = language switch
17+
{
18+
Language.Java => new Java(),
19+
Language.CS => new CS(),
20+
Language.PHP => new PHP(),
21+
_ => throw new ArgumentOutOfRangeException(nameof(language), language, null)
22+
};
23+
24+
return l.LexicalAnalysis(code);
25+
}
26+
27+
public static Node SyntaxicAnalysis(List<string> lexems, List<Rule> rules, Dictionary<Symbol, Dictionary<Symbol, Rule>> table)
28+
{
29+
Symbol actual = rules[0].Left;
30+
31+
Node tree = new Node(actual);
32+
Node node = tree.Copy();
33+
int i = 0;
34+
int cut = 0;
35+
36+
while (i < lexems.Count && cut < 1000)
37+
{
38+
string lexem = lexems[i];
39+
Symbol lexemT = new Symbol(lexem);
40+
if (actual.IsTerminal)
41+
{
42+
if (actual != new Symbol("ε") && actual != lexemT)
43+
throw new Exception("My Syntax Error: " + lexemT + " instead of " + actual);
44+
45+
if (actual == lexemT)
46+
i++;
47+
48+
node = tree.Next(node);
49+
actual = node.Value;
50+
}
51+
else
52+
{
53+
if (!table[actual].ContainsKey(lexemT))
54+
throw new Exception("My Syntax Error: no rule from " + actual);
55+
56+
57+
Rule rule = table[actual][lexemT];
58+
59+
foreach (Symbol symbol in rule.Expression)
60+
node.AddChildren(new Node(symbol));
61+
62+
node = node.Children[0];
63+
actual = node.Value;
64+
}
65+
66+
cut++;
67+
}
68+
69+
return tree;
70+
}
71+
72+
private class Java : ILanguage
73+
{
74+
private readonly Dictionary<string, string> _reserved = new Dictionary<string, string>
75+
{
76+
{"int", "type"},
77+
{"bool", "type"},
78+
{"true", "val"},
79+
{"=", "="},
80+
{";", ";"},
81+
{"(", "("},
82+
{")", ")"},
83+
{"{", "{"},
84+
{"}", "}"},
85+
{"while", "while"},
86+
{"if", "if"},
87+
{"else", "else"},
88+
{"<", "<"},
89+
{">", ">"},
90+
{"+", "+"},
91+
{"-", "-"},
92+
{"*", "*"},
93+
{"/", "/"},
94+
{"%", "%"},
95+
{"System.out.println", "id"}
96+
};
97+
98+
private List<string> _words;
99+
private List<string> _lexems;
100+
101+
private int _i;
102+
private string _builtWord;
103+
104+
public (List<string>, List<string>) LexicalAnalysis(string code)
105+
{
106+
_words = new List<string>();
107+
_lexems = new List<string>();
108+
109+
_i = 0;
110+
_builtWord = "";
111+
112+
while (_i < code.Length)
113+
{
114+
if (SEPARATORS.Contains(code[_i]))
115+
{
116+
if (_builtWord.Length != 0)
117+
{
118+
_words.Add(_builtWord);
119+
_lexems.Add(decimal.TryParse(_builtWord, out _) || bool.TryParse(_builtWord, out _) ? "val" : "id");
120+
}
121+
122+
_builtWord = "";
123+
_i++;
124+
}
125+
else
126+
{
127+
string foundWord = null;
128+
string foundLexem = null;
129+
130+
foreach (string key in _reserved.Keys)
131+
{
132+
if (_i + key.Length > code.Length)
133+
continue;
134+
135+
if (key == code.Substring(_i, key.Length))
136+
{
137+
foundWord = key;
138+
foundLexem = _reserved[key];
139+
break;
140+
}
141+
}
142+
143+
if (foundWord is null)
144+
{
145+
_builtWord += code[_i];
146+
_i++;
147+
}
148+
else
149+
{
150+
if (_builtWord.Length != 0)
151+
{
152+
_words.Add(_builtWord);
153+
_lexems.Add(decimal.TryParse(_builtWord, out _) || bool.TryParse(_builtWord, out _) ? "val" : "id");
154+
_builtWord = "";
155+
}
156+
157+
_words.Add(foundWord);
158+
_lexems.Add(foundLexem);
159+
160+
_i += foundWord.Length;
161+
}
162+
}
163+
}
164+
165+
return (_words, _lexems);
166+
}
167+
}
168+
169+
private class CS : ILanguage
170+
{
171+
public (List<string>, List<string>) LexicalAnalysis(string code)
172+
{
173+
throw new NotImplementedException();
174+
}
175+
}
176+
177+
private class PHP : ILanguage
178+
{
179+
public (List<string>, List<string>) LexicalAnalysis(string code)
180+
{
181+
throw new NotImplementedException();
182+
}
183+
}
184+
}
185+
}

EvalFinale/Analyzer/ILanguage.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Collections.Generic;
2+
3+
namespace EvalFinale.Analyzer
4+
{
5+
public interface ILanguage
6+
{
7+
public (List<string>, List<string>) LexicalAnalysis(string code);
8+
9+
}
10+
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using EvalFinale.Grammar;
43

5-
namespace EvalFinale
4+
namespace EvalFinale.Analyzer
65
{
76
public class Node
87
{

EvalFinale/EvalFinale.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@
4747
</Reference>
4848
</ItemGroup>
4949
<ItemGroup>
50+
<Compile Include="Analyzer\Analyze.cs" />
51+
<Compile Include="Analyzer\ILanguage.cs" />
5052
<Compile Include="Grammar\Rule.cs" />
5153
<Compile Include="Grammar\Symbol.cs" />
52-
<Compile Include="Node.cs" />
54+
<Compile Include="Analyzer\Node.cs" />
5355
<Compile Include="Program.cs" />
5456
<Compile Include="Properties\AssemblyInfo.cs" />
5557
<Compile Include="Traductor.cs" />
@@ -62,5 +64,6 @@
6264
<Content Include="bin\Release\Traduction.xml" />
6365
<Content Include="Traduction.xml" />
6466
</ItemGroup>
67+
<ItemGroup />
6568
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
6669
</Project>

EvalFinale/Program.cs

Lines changed: 3 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using EvalFinale.Analyzer;
45
using EvalFinale.Grammar;
56
using XmlJsonManager.Xml;
67

@@ -181,9 +182,9 @@ static void Main(string[] args)
181182

182183
LoadTranslationData();
183184

184-
(List<string> words, List<string> lexems) = LexicalAnalysis(code);
185+
(List<string> words, List<string> lexems) = Analyze.LexicalAnalysis(code, Traductor.Language.Java);
185186

186-
Node tree = SyntaxicAnalysis(lexems, grammar, table);
187+
Node tree = Analyze.SyntaxicAnalysis(lexems, grammar, table);
187188

188189
string codePHP = Traductor.Translate(tree, words, lexems, Traductor.Language.Java, Traductor.Language.PHP);
189190
string codeCS = Traductor.Translate(tree, words, lexems, Traductor.Language.Java, Traductor.Language.CS);
@@ -215,140 +216,6 @@ private static void LoadTranslationData()
215216
Traductor.Language.Java,
216217
};
217218
}
218-
219-
private static (List<string>, List<string>) LexicalAnalysis(string code)
220-
{
221-
char[] separators = {' ', '\n', '\t'};
222-
Dictionary<string, string> reserved = new Dictionary<string, string>
223-
{
224-
{"int", "type"},
225-
{"bool", "type"},
226-
{"true", "val"},
227-
{"=", "="},
228-
{";", ";"},
229-
{"(", "("},
230-
{")", ")"},
231-
{"{", "{"},
232-
{"}", "}"},
233-
{"while", "while"},
234-
{"if", "if"},
235-
{"else", "else"},
236-
{"<", "<"},
237-
{">", ">"},
238-
{"+", "+"},
239-
{"-", "-"},
240-
{"*", "*"},
241-
{"/", "/"},
242-
{"%", "%"},
243-
{"System.out.println", "id"}
244-
};
245-
246-
List<string> words = new List<string>();
247-
List<string> lexems = new List<string>();
248-
249-
int i = 0;
250-
string builtWord = "";
251-
252-
while (i < code.Length)
253-
{
254-
if (separators.Contains(code[i]))
255-
{
256-
if (builtWord.Length != 0)
257-
{
258-
words.Add(builtWord);
259-
lexems.Add(decimal.TryParse(builtWord, out _) || bool.TryParse(builtWord, out _) ? "val" : "id");
260-
}
261-
262-
builtWord = "";
263-
i++;
264-
}
265-
else
266-
{
267-
string foundWord = null;
268-
string foundLexem = null;
269-
270-
foreach (string key in reserved.Keys)
271-
{
272-
if (i + key.Length > code.Length)
273-
continue;
274-
275-
if (key == code.Substring(i, key.Length))
276-
{
277-
foundWord = key;
278-
foundLexem = reserved[key];
279-
break;
280-
}
281-
}
282-
283-
if (foundWord is null)
284-
{
285-
builtWord += code[i];
286-
i++;
287-
}
288-
else
289-
{
290-
if (builtWord.Length != 0)
291-
{
292-
words.Add(builtWord);
293-
lexems.Add(decimal.TryParse(builtWord, out _) || bool.TryParse(builtWord, out _) ? "val" : "id");
294-
builtWord = "";
295-
}
296-
297-
words.Add(foundWord);
298-
lexems.Add(foundLexem);
299-
300-
i += foundWord.Length;
301-
}
302-
}
303-
}
304-
305-
return (words, lexems);
306-
}
307-
308-
private static Node SyntaxicAnalysis(List<string> lexems, List<Rule> rules, Dictionary<Symbol, Dictionary<Symbol, Rule>> table)
309-
{
310-
Symbol actual = rules[0].Left;
311-
312-
Node tree = new Node(actual);
313-
Node node = tree.Copy();
314-
int i = 0;
315-
int cut = 0;
316-
317-
while (i < lexems.Count && cut < 1000)
318-
{
319-
string lexem = lexems[i];
320-
Symbol lexemT = new Symbol(lexem);
321-
if (actual.IsTerminal)
322-
{
323-
if (actual != new Symbol("ε") && actual != lexemT)
324-
throw new Exception("My Syntax Error: " + lexemT + " instead of " + actual);
325-
326-
if (actual == lexemT)
327-
i++;
328-
329-
node = tree.Next(node);
330-
actual = node.Value;
331-
}
332-
else
333-
{
334-
if (!table[actual].ContainsKey(lexemT))
335-
throw new Exception("My Syntax Error: no rule from " + actual);
336-
337-
338-
Rule rule = table[actual][lexemT];
339-
340-
foreach (Symbol symbol in rule.Expression)
341-
node.AddChildren(new Node(symbol));
342-
343-
node = node.Children[0];
344-
actual = node.Value;
345-
}
346-
347-
cut++;
348-
}
349-
350-
return tree;
351-
}
352219
}
353220

354221
public static class Utility

0 commit comments

Comments
 (0)