-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexTest.cs
More file actions
87 lines (76 loc) · 3.24 KB
/
LexTest.cs
File metadata and controls
87 lines (76 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using Xtaieer.Lex;
using UnityEngine;
using System.IO;
public class LexTest : MonoBehaviour
{
private class Token
{
private string lex;
public Token(string lex)
{
this.lex = lex;
}
public override string ToString()
{
return lex;
}
}
private const string DIGIT_REGULAR = "([1-9][0-9]*|0)(.[0-9]+)?";
private const string PLUS_REGULAR = "%+";
private const string SUB_REGULAR = "-";
private const string MUL_REGULAR = "%*";
private const string DIV_REGULAR = "/";
private const string LEFT_BRACKET_REGULAR = "%(";
private const string RIGHT_BRACKET_REGULAR = "%)";
private Token DigitAction(string lex, int ling, int column)
{
return new Token(lex);
}
[SerializeField]
private string exp;
[SerializeField]
private string regular;
private void Start()
{
Test();
}
[ContextMenu("Test")]
public void Test()
{
LexicalAnalyzer<Token>.RegularAndAction[] regularAndActions = new LexicalAnalyzer<Token>.RegularAndAction[]
{
new LexicalAnalyzer<Token>.RegularAndAction(PLUS_REGULAR, DigitAction),
new LexicalAnalyzer<Token>.RegularAndAction(SUB_REGULAR, DigitAction),
new LexicalAnalyzer<Token>.RegularAndAction(MUL_REGULAR, DigitAction),
new LexicalAnalyzer<Token>.RegularAndAction(DIV_REGULAR, DigitAction),
new LexicalAnalyzer<Token>.RegularAndAction(LEFT_BRACKET_REGULAR, DigitAction),
new LexicalAnalyzer<Token>.RegularAndAction(RIGHT_BRACKET_REGULAR, DigitAction),
new LexicalAnalyzer<Token>.RegularAndAction(DIGIT_REGULAR, DigitAction)
};
LexicalAnalyzer<Token> lexicalAnalyzer = new LexicalAnalyzer<Token>(regularAndActions);
lexicalAnalyzer.SetInput(new StringReader(exp));
Token token = lexicalAnalyzer.NextToken();
while(token != null)
{
Debug.Log(token);
token = lexicalAnalyzer.NextToken();
}
}
[ContextMenu("TestNfa")]
private void TestNfa()
{
LexicalAnalyzer<Token>.RegularAndAction[] regularAndActions = new LexicalAnalyzer<Token>.RegularAndAction[]
{
new LexicalAnalyzer<Token>.RegularAndAction(DIGIT_REGULAR, DigitAction),
// new LexicalAnalyzer<Token>.RegularAndAction(PLUS_REGULAR, DigitAction),
new LexicalAnalyzer<Token>.RegularAndAction(SUB_REGULAR, DigitAction)
// new LexicalAnalyzer<Token>.RegularAndAction(MUL_REGULAR, DigitAction),
// new LexicalAnalyzer<Token>.RegularAndAction(DIV_REGULAR, DigitAction),
// new LexicalAnalyzer<Token>.RegularAndAction(LEFT_BRACKET_REGULAR, DigitAction),
// new LexicalAnalyzer<Token>.RegularAndAction(RIGHT_BRACKET_REGULAR, DigitAction)
};
LexicalAnalyzer<Token> lexicalAnalyzer = new LexicalAnalyzer<Token>(regularAndActions);
// LexicalAnalyzer<Token> lexicalAnalyzer = new LexicalAnalyzer<Token>(new LexicalAnalyzer<Token>.RegularAndAction[] { new LexicalAnalyzer<Token>.RegularAndAction(regular, (arg1, arg2, arg3) => { return null; })});
Debug.Log(lexicalAnalyzer.Accept(exp));
}
}