Skip to content

Commit f32afae

Browse files
committed
添加对空产生式的解析
1 parent 7f47a2c commit f32afae

2 files changed

Lines changed: 30 additions & 18 deletions

File tree

Assets/Grammar/GrammaParser.cs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ public class GrammaParser
99
{
1010
/*
1111
* S -> Nonterminal:Right'
12-
* Right' -> Right;Right' | Right
12+
* Right' -> Right;Right' | Right | Empty
1313
* Right -> Minal Right | Minal
1414
* Minal -> Terminal | Nonterminal
1515
*/
1616

1717
private enum TokenType
1818
{
19+
SPACE,
1920
EMPTY,
2021
SYMBOL,
2122
MINAL,
@@ -24,7 +25,7 @@ private enum TokenType
2425

2526
private class Token
2627
{
27-
public static readonly Token EMPTY = new Token("", TokenType.EMPTY);
28+
public static readonly Token SPACE = new Token("", TokenType.SPACE);
2829

2930
public string Name
3031
{
@@ -76,10 +77,11 @@ public override string ToString()
7677
public GrammaParser() {
7778
lex = new LexicalAnalyzer<Token>(
7879
new LexicalAnalyzer<Token>.RegularAndAction[] {
79-
new LexicalAnalyzer<Token>.RegularAndAction("\t| ", (lex, line, column) => { return Token.EMPTY; }),
80+
new LexicalAnalyzer<Token>.RegularAndAction("\t| ", (lex, line, column) => { return Token.SPACE; }),
8081
new LexicalAnalyzer<Token>.RegularAndAction("\".\"", (lex, line, column) => { return new Token(new string(lex[1], 1), TokenType.SINGLE_CHAR_MINAL); }),
8182
new LexicalAnalyzer<Token>.RegularAndAction("([A-Z]|[a-z]|[0-9])+", (lex, line, column) => {return new Token(lex, TokenType.MINAL); }),
82-
new LexicalAnalyzer<Token>.RegularAndAction(":|;|%|", (lex, line, column) => {return new Token(lex, TokenType.SYMBOL); })
83+
new LexicalAnalyzer<Token>.RegularAndAction(":|;|%|", (lex, line, column) => {return new Token(lex, TokenType.SYMBOL); }),
84+
new LexicalAnalyzer<Token>.RegularAndAction("ε", (lex, line, column) => { return new Token(lex, TokenType.EMPTY); })
8385
}
8486
);
8587
}
@@ -126,22 +128,30 @@ private List<Minal[]> RightT(List<Minal[]> productions)
126128
{
127129
result = new List<Minal[]>();
128130
}
129-
LinkedList<Minal> minalLinkedList = new LinkedList<Minal>();
130-
minalLinkedList = Right(minalLinkedList);
131-
Minal[] minals = new Minal[minalLinkedList.Count];
132-
LinkedListNode<Minal> head = minalLinkedList.First;
133-
int index = 0;
134-
while(head != null)
131+
if(current.Type == TokenType.EMPTY)
135132
{
136-
minals[index] = head.Value;
137-
index++;
138-
head = head.Next;
133+
Minal[] minals = new Minal[] { Grammar.Minal.EMPTY };
134+
result.Add(minals);
139135
}
140-
result.Add(minals);
141-
if (current != null && current.Type == TokenType.SYMBOL && current.Name == ";")
136+
else
142137
{
143-
Match(TokenType.SYMBOL);
144-
return RightT(result);
138+
LinkedList<Minal> minalLinkedList = new LinkedList<Minal>();
139+
minalLinkedList = Right(minalLinkedList);
140+
Minal[] minals = new Minal[minalLinkedList.Count];
141+
LinkedListNode<Minal> head = minalLinkedList.First;
142+
int index = 0;
143+
while (head != null)
144+
{
145+
minals[index] = head.Value;
146+
index++;
147+
head = head.Next;
148+
}
149+
result.Add(minals);
150+
if (current != null && current.Type == TokenType.SYMBOL && current.Name == ";")
151+
{
152+
Match(TokenType.SYMBOL);
153+
return RightT(result);
154+
}
145155
}
146156
return result;
147157
}
@@ -207,7 +217,7 @@ private Nonterminal Nonterminal()
207217
private void Match(TokenType type)
208218
{
209219
current = lex.NextToken();
210-
while (current != null && current.Type == TokenType.EMPTY)
220+
while (current != null && current.Type == TokenType.SPACE)
211221
{
212222
current = lex.NextToken();
213223
}

Assets/Grammar/Minal.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ namespace Xtaieer.Grammar
44
{
55
public class Minal
66
{
7+
public static readonly Minal EMPTY = new Minal("ε");
8+
79
public string Name
810
{
911
get;

0 commit comments

Comments
 (0)