| Line 11... |
Line 11... |
| 11 |
|
11 |
|
| 12 |
%{
|
12 |
%{
|
| 13 |
import java.lang.*;
|
13 |
import java.lang.*;
|
| 14 |
import java.io.*;
|
14 |
import java.io.*;
|
| 15 |
import java.util.StringTokenizer;
|
15 |
import java.util.StringTokenizer;
|
| - |
|
16 |
import java.util.Vector;
|
| 16 |
%}
|
17 |
%}
|
| 17 |
|
18 |
|
| 18 |
/* Token Declarations */
|
19 |
/* Token Declarations */
|
| 19 |
%token CLASS
|
20 |
%token CLASS
|
| 20 |
%token ID
|
21 |
%token ID
|
| Line 37... |
Line 38... |
| 37 |
%token SEMI
|
38 |
%token SEMI
|
| 38 |
%token ASSIGNOP
|
39 |
%token ASSIGNOP
|
| 39 |
|
40 |
|
| 40 |
/* J-Minus Grammar */
|
41 |
/* J-Minus Grammar */
|
| 41 |
%%
|
42 |
%%
|
| 42 |
program : modifier CLASS ID LBRACE method_declaration RBRACE
|
- |
|
| 43 |
{ System.out.println ("program -> modifier class id { method_declaration }");
|
- |
|
| 44 |
System.out.println ("\nPARSING SUCCESSFUL: ACCEPT"); };
|
- |
|
| 45 |
|
43 |
|
| - |
|
44 |
program : program_good
|
| - |
|
45 |
| program_bad { badpath=true; };
|
| - |
|
46 |
|
| 46 |
modifier : PUBLIC { System.out.println ("modifier -> public"); }
|
47 |
program_good : modifier CLASS ID LBRACE method_declaration RBRACE
|
| 47 |
| PRIVATE { System.out.println ("modifier -> private"); };
|
48 |
{ System.out.println ("program -> modifier class id { method_declaration }"); };
|
| 48 |
|
49 |
|
| - |
|
50 |
program_bad : modifier CLASS ID LBRACE method_declaration { v.add("Missing RBRACE in program"); }
|
| - |
|
51 |
| modifier CLASS ID method_declaration RBRACE { v.add("Missing LBRACE in program"); }
|
| - |
|
52 |
| modifier ID LBRACE method_declaration RBRACE { v.add("Missing CLASS in program"); }
|
| - |
|
53 |
| modifier ID ID LBRACE method_declaration RBRACE { v.add("Mis-spelled? CLASS in program"); };
|
| - |
|
54 |
|
| - |
|
55 |
modifier : PUBLIC { System.out.println ("modifier -> public"); }
|
| - |
|
56 |
| PRIVATE { System.out.println ("modifier -> private"); }
|
| - |
|
57 |
| ID { badpath=true; v.add("Mis-spelled? PUBLIC or PRIVATE modifier"); }
|
| - |
|
58 |
| { badpath=true; v.add("Missing PUBLIC or PRIVATE modifier"); };
|
| - |
|
59 |
|
| - |
|
60 |
method_declaration : method_declaration_good
|
| - |
|
61 |
| method_declaration_bad { badpath=true; }
|
| - |
|
62 |
|
| 49 |
method_declaration : modifier type_specifier ID LPAREN param RPAREN
|
63 |
method_declaration_good : modifier type_specifier ID LPAREN param RPAREN
|
| 50 |
LBRACE local_declaration stmt_sequence RBRACE
|
64 |
LBRACE local_declaration stmt_sequence RBRACE
|
| 51 |
{ System.out.println ("method_declaration -> modifier type_specifier id (param) "
|
65 |
{ System.out.println ("method_declaration -> modifier type_specifier id (param) "
|
| 52 |
+"{ local_declaration stmt_sequence }"); };
|
66 |
+"{ local_declaration stmt_sequence }"); };
|
| 53 |
|
67 |
|
| - |
|
68 |
method_declaration_bad : modifier type_specifier ID param RPAREN LBRACE local_declaration stmt_sequence RBRACE
|
| - |
|
69 |
{ v.add("Missing LPAREN in method_declaration"); }
|
| - |
|
70 |
| modifier type_specifier ID LPAREN param LBRACE local_declaration stmt_sequence RBRACE
|
| - |
|
71 |
{ v.add("Missing RPAREN in method_declaration"); }
|
| - |
|
72 |
| modifier type_specifier ID LPAREN param RPAREN local_declaration stmt_sequence RBRACE
|
| - |
|
73 |
{ v.add("Missing LBRACE in method_declaration"); }
|
| - |
|
74 |
| modifier type_specifier ID LPAREN param RPAREN LBRACE local_declaration stmt_sequence
|
| - |
|
75 |
{ v.add("Missing RBRACE in method_declaration"); };
|
| - |
|
76 |
|
| 54 |
type_specifier : INT { System.out.println ("type_specifier -> int"); }
|
77 |
type_specifier : INT { System.out.println ("type_specifier -> int"); }
|
| 55 |
| FLOAT { System.out.println ("type_specifier -> float"); }
|
78 |
| FLOAT { System.out.println ("type_specifier -> float"); }
|
| 56 |
| VOID { System.out.println ("type_specifier -> void"); };
|
79 |
| VOID { System.out.println ("type_specifier -> void"); }
|
| - |
|
80 |
| { badpath=true; v.add("Missing type_specifier"); }
|
| - |
|
81 |
| ID { badpath=true; v.add("Mis-spelled? type_specifier"); };
|
| 57 |
|
82 |
|
| 58 |
param : type_specifier ID { System.out.println ("param -> type_specifier id"); }
|
83 |
param : type_specifier ID { System.out.println ("param -> type_specifier id"); }
|
| 59 |
| VOID { System.out.println ("param -> void"); };
|
84 |
| VOID { System.out.println ("param -> void"); };
|
| 60 |
|
85 |
|
| - |
|
86 |
local_declaration : local_declaration_good
|
| - |
|
87 |
| local_declaration_bad { badpath=true; }
|
| - |
|
88 |
|
| 61 |
local_declaration : type_specifier ID SEMI
|
89 |
local_declaration_good : type_specifier ID SEMI
|
| 62 |
{ System.out.println ("local_declaration -> id;"); }
|
90 |
{ System.out.println ("local_declaration -> id;"); }
|
| 63 |
| { System.out.println ("local_declaration -> epsilon"); };
|
91 |
| { System.out.println ("local_declaration -> epsilon"); };
|
| 64 |
|
92 |
|
| - |
|
93 |
local_declaration_bad : type_specifier ID { v.add("Missing SEMI in local_declaration"); };
|
| - |
|
94 |
|
| 65 |
stmt_sequence : stmt_sequence statement
|
95 |
stmt_sequence : stmt_sequence statement
|
| 66 |
{ System.out.println ("stmt_sequence -> stmt_sequence statement"); }
|
96 |
{ System.out.println ("stmt_sequence -> stmt_sequence statement"); }
|
| 67 |
| statement { System.out.println ("stmt_sequence -> statement"); };
|
97 |
| statement { System.out.println ("stmt_sequence -> statement"); };
|
| 68 |
|
98 |
|
| 69 |
statement : if_stmt { System.out.println ("statement -> if_stmt"); }
|
99 |
statement : if_stmt { System.out.println ("statement -> if_stmt"); }
|
| 70 |
| while_stmt { System.out.println ("statement -> while_stmt"); }
|
100 |
| while_stmt { System.out.println ("statement -> while_stmt"); }
|
| 71 |
| assign_stmt { System.out.println ("statement -> assign_stmt"); }
|
101 |
| assign_stmt { System.out.println ("statement -> assign_stmt"); }
|
| 72 |
| return_stmt { System.out.println ("statement -> return_stmt"); }
|
102 |
| return_stmt { System.out.println ("statement -> return_stmt"); }
|
| 73 |
| expr_stmt { System.out.println ("statement -> expr_stmt"); };
|
103 |
| expr_stmt { System.out.println ("statement -> expr_stmt"); };
|
| 74 |
|
104 |
|
| - |
|
105 |
if_stmt : if_stmt_good
|
| - |
|
106 |
| if_stmt_bad { badpath=true; };
|
| - |
|
107 |
|
| 75 |
if_stmt : IF LPAREN exp RPAREN statement ELSE statement
|
108 |
if_stmt_good : IF LPAREN exp RPAREN statement ELSE statement
|
| 76 |
{ System.out.println ("if_stmt -> if (exp) statement else statement"); };
|
109 |
{ System.out.println ("if_stmt -> if (exp) statement else statement"); };
|
| 77 |
|
110 |
|
| - |
|
111 |
if_stmt_bad : IF exp RPAREN statement ELSE statement { v.add("Missing LPAREN in if_stmt"); }
|
| - |
|
112 |
| IF LPAREN exp statment ELSE statement { v.add("Missing RPAREN in if_stmt"); }
|
| - |
|
113 |
| IF LPAREN exp RPAREN statement { v.add("Missing ELSE part in if_stmt"); };
|
| - |
|
114 |
|
| - |
|
115 |
while_stmt : while_stmt_good
|
| - |
|
116 |
| while_stmt_bad { badpath=true; };
|
| - |
|
117 |
|
| 78 |
while_stmt : WHILE LPAREN exp RPAREN statement
|
118 |
while_stmt_good : WHILE LPAREN exp RPAREN statement
|
| 79 |
{ System.out.println ("while_stmt -> (exp) statement"); };
|
119 |
{ System.out.println ("while_stmt -> (exp) statement"); };
|
| 80 |
|
120 |
|
| - |
|
121 |
while_stmt_bad : WHILE exp RPAREN statement { v.add("Missing LPAREN in while_stmt"); }
|
| - |
|
122 |
| WHILE LPAREN exp statement { v.add("Missing RPAREN in while_stmt"); };
|
| - |
|
123 |
|
| - |
|
124 |
assign_stmt : assign_stmt_good
|
| - |
|
125 |
| assign_stmt_bad { badpath=true; };
|
| - |
|
126 |
|
| 81 |
assign_stmt : ID ASSIGNOP exp SEMI
|
127 |
assign_stmt_good : ID ASSIGNOP exp SEMI
|
| 82 |
{ System.out.println ("assign_stmt -> id = exp;"); };
|
128 |
{ System.out.println ("assign_stmt -> id = exp;"); };
|
| 83 |
|
129 |
|
| - |
|
130 |
assign_stmt_bad : ID ASSIGNOP exp { v.add("Missing SEMI in assign_stmt"); };
|
| - |
|
131 |
|
| - |
|
132 |
return_stmt : return_stmt_good
|
| - |
|
133 |
| return_stmt_bad { badpath=true; };
|
| - |
|
134 |
|
| 84 |
return_stmt : RETURN exp SEMI { System.out.println ("return_stmt -> return exp;"); }
|
135 |
return_stmt_good : RETURN exp SEMI { System.out.println ("return_stmt -> return exp;"); }
|
| 85 |
| RETURN SEMI { System.out.println ("return_stmt -> return;"); };
|
136 |
| RETURN SEMI { System.out.println ("return_stmt -> return;"); };
|
| 86 |
|
137 |
|
| - |
|
138 |
return_stmt_bad : RETURN exp { v.add("Missing SEMI in return_stmt"); }
|
| - |
|
139 |
| RETURN { v.add("Missing SEMI in return_stmt"); };
|
| - |
|
140 |
|
| - |
|
141 |
expr_stmt : expr_stmt_good
|
| - |
|
142 |
| expr_stmt_bad { badpath=true; };
|
| - |
|
143 |
|
| 87 |
expr_stmt : exp SEMI { System.out.println ("expr_stmt -> exp;"); };
|
144 |
expr_stmt_good : exp SEMI { System.out.println ("expr_stmt -> exp;"); };
|
| - |
|
145 |
|
| - |
|
146 |
expr_stmt_bad : exp { v.add("Missing SEMI in expr_stmt"); }
|
| 88 |
|
147 |
|
| 89 |
exp : exp ADDOP term { System.out.println ("exp -> exp + term"); }
|
148 |
exp : exp ADDOP term { System.out.println ("exp -> exp + term"); }
|
| 90 |
| term { System.out.println ("exp -> term"); };
|
149 |
| term { System.out.println ("exp -> term"); };
|
| 91 |
|
150 |
|
| 92 |
term : term MULOP factor { System.out.println ("term -> term * factor"); }
|
151 |
term : term MULOP factor { System.out.println ("term -> term * factor"); }
|
| 93 |
| factor { System.out.println ("term -> factor"); };
|
152 |
| factor { System.out.println ("term -> factor"); };
|
| 94 |
|
153 |
|
| 95 |
factor : LPAREN exp RPAREN { System.out.println ("factor -> (exp)"); }
|
154 |
factor : LPAREN exp RPAREN { System.out.println ("factor -> (exp)"); }
|
| 96 |
| ADDOP factor { System.out.println ("factor -> + factor"); }
|
155 |
| ADDOP factor { System.out.println ("factor -> + factor"); }
|
| 97 |
| NUM { System.out.println ("factor -> NUM"); }
|
156 |
| NUM { System.out.println ("factor -> NUM"); }
|
| 98 |
| ID { System.out.println ("factor -> ID"); };
|
157 |
| ID { System.out.println ("factor -> ID"); }
|
| - |
|
158 |
| exp RPAREN { badpath=true; v.add("Missing LPAREN in factor"); }
|
| - |
|
159 |
| LPAREN exp { badpath=true; v.add("Missing RPAREN in factor"); };
|
| 99 |
|
160 |
|
| 100 |
%%
|
161 |
%%
|
| 101 |
|
162 |
|
| - |
|
163 |
/**
|
| - |
|
164 |
* Special Stuff
|
| - |
|
165 |
*/
|
| - |
|
166 |
|
| - |
|
167 |
static boolean badpath = false;
|
| - |
|
168 |
static Vector v = new Vector();
|
| - |
|
169 |
|
| - |
|
170 |
/**
|
| - |
|
171 |
* END
|
| - |
|
172 |
*/
|
| - |
|
173 |
|
| 102 |
private Yylex lexer;
|
174 |
private Yylex lexer;
|
| 103 |
|
175 |
|
| 104 |
public void yyerror (String error)
|
176 |
public void yyerror (String error)
|
| 105 |
{
|
177 |
{
|
| 106 |
System.out.println ("Parse Error: " + error);
|
178 |
System.out.println ("Parse Error: " + error);
|
| Line 127... |
Line 199... |
| 127 |
lexer = new Yylex (r, this);
|
199 |
lexer = new Yylex (r, this);
|
| 128 |
}
|
200 |
}
|
| 129 |
|
201 |
|
| 130 |
public static void main (String[] args) throws Exception
|
202 |
public static void main (String[] args) throws Exception
|
| 131 |
{
|
203 |
{
|
| - |
|
204 |
try
|
| - |
|
205 |
{
|
| 132 |
Parser yyparser = new Parser (new FileReader (args[0]));
|
206 |
Parser yyparser = new Parser (new FileReader (args[0]));
|
| 133 |
yyparser.yyparse();
|
207 |
yyparser.yyparse();
|
| - |
|
208 |
}
|
| - |
|
209 |
catch (Exception e)
|
| - |
|
210 |
{
|
| - |
|
211 |
badpath=true;
|
| - |
|
212 |
}
|
| - |
|
213 |
|
| - |
|
214 |
if (badpath)
|
| - |
|
215 |
{
|
| - |
|
216 |
int i;
|
| - |
|
217 |
String s;
|
| - |
|
218 |
|
| - |
|
219 |
System.out.println ("\n\nError Report:");
|
| - |
|
220 |
System.out.println ("============================================");
|
| - |
|
221 |
|
| - |
|
222 |
for (i=0; i<v.size(); i++)
|
| - |
|
223 |
{
|
| - |
|
224 |
s = (String)v.get(i);
|
| - |
|
225 |
System.out.println (s);
|
| - |
|
226 |
}
|
| - |
|
227 |
|
| - |
|
228 |
System.out.println ("\nPARSING FAILED");
|
| - |
|
229 |
|
| - |
|
230 |
}
|
| - |
|
231 |
else
|
| - |
|
232 |
{
|
| - |
|
233 |
System.out.println ("\nPARSING SUCCESSFUL");
|
| - |
|
234 |
}
|
| 134 |
}
|
235 |
}
|
| 135 |
|
236 |
|