-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecParser.java
156 lines (138 loc) · 4.48 KB
/
RecParser.java
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
File: RecParser.java
Author: zerksis d. umrigar (zdu@acm.org)
Copyright (C) 1997 Zerksis D. Umrigar
Last Update Time-stamp: "97/06/27 20:40:12 umrigar"
This code is distributed under the terms of the GNU General Public License.
See the file COPYING with this distribution, or
http://www.fsf.org/copyleft/gpl.html
THERE IS ABSOLUTELY NO WARRANTY FOR THIS PROGRAM.
*/
package zdu.parsdemo;
import zdu.parsdemo.Grammar;
import zdu.parsdemo.OffsetTree;
import zdu.parsdemo.ParseNode;
import zdu.parsdemo.Scanner;
import java.util.Stack;
import java.util.EmptyStackException;
abstract class RecParser extends StepParser
{
RecParser(Grammar grammar, Scanner scanner, ParseDisplay parseDisplay)
{
super(grammar, scanner, parseDisplay);
reset2();
}
protected void reset2()
{
super.reset2();
}
protected void update() throws ParseException
{
switch (act)
{
case ACCEPT:
{
stk.popT();
ySelect = new Integer(lineNum);
treeSelect = null;
didLastUpdate = true;
break;
}
case CALL:
{
GramSym nonTerm = gramSym;
ParseNode n = new ParseNode(nonTerm.toString(), false);
OffsetTree t = new OffsetTree(n);
ParseStkEntry e = new ParseStkEntry(t);
if (stk.empty())
{
forest.addElement(t);
}
else
{
stk.peekT().getOffsetTree().addKid(t);
}
stk.pushT(e);
ySelect = new Integer(lineNum);
treeSelect = t;
break;
}
case MATCH:
{
Terminal terminal = (Terminal)gramSym;
if (tok.getTokNum() == terminal.getTok())
{
String name = tok.getText();
ParseNode n = new ParseNode(name, true);
OffsetTree t = new OffsetTree(n);
ParseStkEntry e = new ParseStkEntry(t);
stk.peekT().getOffsetTree().addKid(t);
ySelect = new Integer(lineNum);
tok = scanner.nextTok();
treeSelect = t;
}
else
{
ParseNode n = new ParseNode("ERROR", true);
OffsetTree t = new OffsetTree(n);
ParseStkEntry e = new ParseStkEntry(t);
stk.peekT().getOffsetTree().addKid(t);
stk.pushT(e);
ySelect = new Integer(lineNum);
didLastUpdate = true;
throw new ParseException();
}
break;
}
case RETURN:
{
treeSelect = stk.popT().getOffsetTree();
ySelect = new Integer(lineNum);
break;
}
default:
throw new InternalError("RecParser: bad act in update()");
}
}
protected void call(NonTerm nonTerm, int ruleN, int lineNum)
throws ParseException, ParseResetException
{
this.act = CALL;
this.lineNum = lineNum;
this.gramSym = nonTerm;
extFrameTag = "rule" + ruleN;
waitToStep();
}
protected void match(Terminal terminal, int ruleN, int lineNum)
throws ParseException, ParseResetException
{
this.act = MATCH;
this.lineNum = lineNum;
this.gramSym = terminal;
extFrameTag = "rule" + ruleN;
waitToStep();
}
protected void ret(int ruleN, int lineNum)
throws ParseException, ParseResetException
{
this.act = RETURN;
this.lineNum = lineNum;
extFrameTag = "rule" + ruleN;
waitToStep();
}
protected void accept(int ruleN, int lineNum)
throws ParseException, ParseResetException
{
this.act = ACCEPT;
this.lineNum = lineNum;
extFrameTag = "rule" + ruleN;
waitToStep();
}
private GramSym gramSym; //grammar symbol being processed.
private int lineNum; //current line #.
private int act; //current update action.
static private final int ACCEPT = 0;
static private final int CALL = ACCEPT + 1;
static private final int MATCH = CALL + 1;
static private final int RETURN = MATCH + 1;
}