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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
%Options gp=java,act,tab,an=expract.java,hn=exprhdr.java,tab=space,fp=expr,prefix=TK_,
%Options nogoto-default,output-size=125,name=max,error-maps
%Define
-- This software is subject to the terms of the IBM Jikes Parser
-- Generator License Agreement available at the following URL:
-- http://www.ibm.com/research/jikes.
-- Copyright (C) 1983, 1999, International Business Machines Corporation
-- and others. All Rights Reserved.
-- You must accept the terms of that agreement to use this software.
-- This grammar has been augmented with productions that captures
-- most errors that a user is likely to make. This saves the need
-- to have an error recovery system.
--
-- This macro is used to initialize the rule_action array
-- to the null_action function.
--
%null_action
/.
new NullAction(),
./
--
-- This macro is used to initialize the rule_action array
-- to the no_action function.
--
%no_action
/.
new NoAction(),
./
%Terminals
PLUS MINUS STAR SLASH NUMBER LPAREN RPAREN
EOF ERROR
%Alias
'+' ::= PLUS
'-' ::= MINUS
'*' ::= STAR
'/' ::= SLASH
'(' ::= LPAREN
')' ::= RPAREN
%EOF ::= EOF
%ERROR ::= ERROR
%Rules
/:
class exprhdr extends expract
{
Action rule_action[] = {
null, // no element 0
:/
/.
class expract
{
Parser parser;
expract(Parser parser)
{
this.parser = parser;
}
interface Action
{
public void action();
}
final class NoAction implements Action
{
public void action() {}
}
final class NullAction implements Action
{
public void action() { parser.setSYM1(null); }
}
./
Goal ::= %empty
/:%null_action:/
Goal ::= Expression
/:%no_action:/
Expression ::= Expression '+' Term
/: new act%rule_number(),:/
/.
//
// Rule %rule_number: %rule_text
//
final class act%rule_number implements Action
{
public void action()
{
AstPlus node = new AstPlus();
node.left = parser.SYM(1);
node.op = parser.TOKEN(2);
node.right = parser.SYM(3);
parser.setSYM1(node);
return;
}
}
./
Expression ::= Expression '-' Term
/: new act%rule_number(),:/
/.
//
// Rule %rule_number: %rule_text
//
final class act%rule_number implements Action
{
public void action()
{
AstMinus node = new AstMinus();
node.left = parser.SYM(1);
node.op = parser.TOKEN(2);
node.right = parser.SYM(3);
parser.setSYM1(node);
}
}
./
Expression ::= Term
/:%no_action:/
Term ::= Term '*' Factor
/: new act%rule_number(),:/
/.
//
// Rule %rule_number: %rule_text
//
final class act%rule_number implements Action
{
public void action()
{
AstStar node = new AstStar();
node.left = parser.SYM(1);
node.op = parser.TOKEN(2);
node.right = parser.SYM(3);
parser.setSYM1(node);
}
}
./
Term ::= Term '/' Factor
/: new act%rule_number(),:/
/.
//
// Rule %rule_number: %rule_text
//
final class act%rule_number implements Action
{
public void action()
{
AstSlash node = new AstSlash();
node.left = parser.SYM(1);
node.op = parser.TOKEN(2);
node.right = parser.SYM(3);
parser.setSYM1(node);
}
}
./
Term ::= Factor
/:%no_action:/
Factor ::= NUMBER
/: new act%rule_number(),:/
/.
//
// Rule %rule_number: %rule_text
//
final class act%rule_number implements Action
{
public void action()
{
AstNumber node = new AstNumber();
node.token = parser.TOKEN(1);
node.value = Integer.parseInt(parser.lex_stream.Name(node.token));
parser.setSYM1(node);
}
}
./
Factor ::= '-' NUMBER
/: new act%rule_number(),:/
/.
//
// Rule %rule_number: %rule_text
//
final class act%rule_number implements Action
{
public void action()
{
AstNegativeNumber node = new AstNegativeNumber();
node.op = parser.TOKEN(1);
node.token = parser.TOKEN(2);
node.value = - Integer.parseInt(parser.lex_stream.Name(node.token));
parser.setSYM1(node);
}
}
./
Factor ::= '(' Expression ')'
/: new act%rule_number(),:/
/.
//
// Rule %rule_number: %rule_text
//
final class act%rule_number implements Action
{
public void action()
{
AstParen node = new AstParen();
node.expression = parser.SYM(2);
parser.setSYM1(node);
}
}
./
/:
};
exprhdr(Parser parser)
{
super(parser);
}
}
:/
/.
}
./
%End
|