Subversion Repositories programming

Rev

Rev 232 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
225 ira 1
/*******************************************************************************
2
 * File: jminus.y
3
 * 
4
 * Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
5
 * License: GNU General Public License v2
229 ira 6
 *
7
 * This file contains a definition of the J-Minus language, and will
8
 * create a parser for the language when compiled using BYACC/J, which
9
 * is available from http://byaccj.sourceforge.net.
225 ira 10
 ******************************************************************************/
11
 
12
%{
13
    import java.lang.*;
14
    import java.io.*;
226 ira 15
    import java.util.StringTokenizer;
232 ira 16
    import java.util.Vector;
225 ira 17
%}
18
 
19
/* Token Declarations */
20
%token CLASS
21
%token ID
22
%token PUBLIC
23
%token PRIVATE
24
%token INT
25
%token FLOAT
26
%token VOID
27
%token IF
28
%token ELSE
29
%token WHILE
30
%token RETURN
31
%token ADDOP
32
%token MULOP
33
%token NUM
34
%token LPAREN
35
%token RPAREN
36
%token LBRACE
37
%token RBRACE
38
%token SEMI
39
%token ASSIGNOP
40
 
229 ira 41
/* J-Minus Grammar */
225 ira 42
%%
43
 
232 ira 44
program : program_good
45
    | program_bad { badpath=true; };
46
 
47
program_good : modifier CLASS ID LBRACE method_declaration RBRACE
48
        { System.out.println ("program -> modifier class id { method_declaration }"); };
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
 
228 ira 55
modifier : PUBLIC   { System.out.println ("modifier -> public"); }
232 ira 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"); };
225 ira 59
 
232 ira 60
method_declaration : method_declaration_good
61
    | method_declaration_bad { badpath=true; }
62
 
63
method_declaration_good : modifier type_specifier ID LPAREN param RPAREN
228 ira 64
    LBRACE local_declaration stmt_sequence RBRACE
65
        { System.out.println ("method_declaration -> modifier type_specifier id (param) "
66
                             +"{ local_declaration stmt_sequence }"); };
225 ira 67
 
232 ira 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
 
228 ira 77
type_specifier : INT { System.out.println ("type_specifier -> int"); }
78
    | FLOAT { System.out.println ("type_specifier -> float"); }
232 ira 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"); };
225 ira 82
 
228 ira 83
param : type_specifier ID { System.out.println ("param -> type_specifier id"); }
84
    | VOID  { System.out.println ("param -> void"); };
225 ira 85
 
232 ira 86
local_declaration : local_declaration_good
87
    | local_declaration_bad { badpath=true; }
88
 
89
local_declaration_good : type_specifier ID SEMI
228 ira 90
            { System.out.println ("local_declaration -> id;"); }
91
    |       { System.out.println ("local_declaration -> epsilon"); };
225 ira 92
 
232 ira 93
local_declaration_bad : type_specifier ID { v.add("Missing SEMI in local_declaration"); };
94
 
228 ira 95
stmt_sequence : stmt_sequence statement
96
                { System.out.println ("stmt_sequence -> stmt_sequence statement"); }
97
    | statement { System.out.println ("stmt_sequence -> statement"); };
225 ira 98
 
228 ira 99
statement : if_stmt { System.out.println ("statement -> if_stmt"); }
100
    | while_stmt    { System.out.println ("statement -> while_stmt"); }
101
    | assign_stmt   { System.out.println ("statement -> assign_stmt"); }
102
    | return_stmt   { System.out.println ("statement -> return_stmt"); }
103
    | expr_stmt     { System.out.println ("statement -> expr_stmt"); };
225 ira 104
 
232 ira 105
if_stmt : if_stmt_good
106
    | if_stmt_bad { badpath=true; };
107
 
108
if_stmt_good : IF LPAREN exp RPAREN statement ELSE statement
228 ira 109
        { System.out.println ("if_stmt -> if (exp) statement else statement"); };
225 ira 110
 
232 ira 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
 
118
while_stmt_good : WHILE LPAREN exp RPAREN statement
228 ira 119
        { System.out.println ("while_stmt -> (exp) statement"); };
225 ira 120
 
232 ira 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
 
127
assign_stmt_good : ID ASSIGNOP exp SEMI
228 ira 128
        { System.out.println ("assign_stmt -> id = exp;"); };
225 ira 129
 
232 ira 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
 
135
return_stmt_good : RETURN exp SEMI   { System.out.println ("return_stmt -> return exp;"); }
228 ira 136
    | RETURN SEMI               { System.out.println ("return_stmt -> return;"); };
225 ira 137
 
232 ira 138
return_stmt_bad : RETURN exp { v.add("Missing SEMI in return_stmt"); }
139
    | RETURN { v.add("Missing SEMI in return_stmt"); };
225 ira 140
 
232 ira 141
expr_stmt : expr_stmt_good
142
    | expr_stmt_bad { badpath=true; };
143
 
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"); }
147
 
228 ira 148
exp : exp ADDOP term            { System.out.println ("exp -> exp + term"); }
149
    | term                      { System.out.println ("exp -> term"); };
225 ira 150
 
228 ira 151
term : term MULOP factor        { System.out.println ("term -> term * factor"); }
152
    | factor                    { System.out.println ("term -> factor"); };
225 ira 153
 
228 ira 154
factor : LPAREN exp RPAREN      { System.out.println ("factor -> (exp)"); }
155
    | ADDOP factor              { System.out.println ("factor -> + factor"); }
156
    | NUM                       { System.out.println ("factor -> NUM"); }
232 ira 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"); };
225 ira 160
 
161
%%
162
 
232 ira 163
/**
164
 * Special Stuff
165
 */
166
 
167
static boolean badpath = false;
168
static Vector v = new Vector();
169
 
170
/**
171
 * END
172
 */
173
 
226 ira 174
private Yylex lexer;
225 ira 175
 
226 ira 176
public void yyerror (String error)
177
{
178
    System.out.println ("Parse Error: " + error);
179
}
225 ira 180
 
226 ira 181
int yylex ()
182
{
183
    int lex_return = -1;
184
 
185
    try
186
    {
187
        lex_return = lexer.yylex();
188
    }
189
    catch (IOException e)
190
    {
191
        System.out.println ("IO Error: " + e);
192
    }
193
 
194
    return lex_return;
195
}
196
 
197
public Parser (Reader r)
198
{
199
    lexer = new Yylex (r, this);
200
}
201
 
202
public static void main (String[] args) throws Exception
203
{
240 ira 204
    System.out.println ("Grammar Productions - Rightmost Derivation in Reverse Order");
205
    System.out.println ("===============================================================");
206
 
232 ira 207
    try
208
    {
209
        Parser yyparser = new Parser (new FileReader (args[0]));
210
        yyparser.yyparse();
211
    }
212
    catch (Exception e)
213
    {
214
        badpath=true;
215
    }
216
 
217
    if (badpath)
218
    {
219
        int i;
220
        String s;
221
 
222
        System.out.println ("\n\nError Report:");
223
        System.out.println ("============================================");
224
 
225
        for (i=0; i<v.size(); i++)
226
        {
227
            s = (String)v.get(i);
228
            System.out.println (s);
229
        }
230
 
231
        System.out.println ("\nPARSING FAILED");
232
 
233
    }
234
    else
235
    {
236
        System.out.println ("\nPARSING SUCCESSFUL");
237
    }
226 ira 238
}
239