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;
|
225 |
ira |
16 |
%}
|
|
|
17 |
|
|
|
18 |
/* Token Declarations */
|
|
|
19 |
%token CLASS
|
|
|
20 |
%token ID
|
|
|
21 |
%token PUBLIC
|
|
|
22 |
%token PRIVATE
|
|
|
23 |
%token INT
|
|
|
24 |
%token FLOAT
|
|
|
25 |
%token VOID
|
|
|
26 |
%token IF
|
|
|
27 |
%token ELSE
|
|
|
28 |
%token WHILE
|
|
|
29 |
%token RETURN
|
|
|
30 |
%token ADDOP
|
|
|
31 |
%token MULOP
|
|
|
32 |
%token NUM
|
|
|
33 |
%token LPAREN
|
|
|
34 |
%token RPAREN
|
|
|
35 |
%token LBRACE
|
|
|
36 |
%token RBRACE
|
|
|
37 |
%token SEMI
|
|
|
38 |
%token ASSIGNOP
|
|
|
39 |
|
229 |
ira |
40 |
/* J-Minus Grammar */
|
225 |
ira |
41 |
%%
|
|
|
42 |
program : modifier CLASS ID LBRACE method_declaration RBRACE
|
231 |
ira |
43 |
{ System.out.println ("program -> modifier class id { method_declaration }");
|
|
|
44 |
System.out.println ("\nPARSING SUCCESSFUL: ACCEPT"); };
|
225 |
ira |
45 |
|
228 |
ira |
46 |
modifier : PUBLIC { System.out.println ("modifier -> public"); }
|
|
|
47 |
| PRIVATE { System.out.println ("modifier -> private"); };
|
225 |
ira |
48 |
|
228 |
ira |
49 |
method_declaration : modifier type_specifier ID LPAREN param RPAREN
|
|
|
50 |
LBRACE local_declaration stmt_sequence RBRACE
|
|
|
51 |
{ System.out.println ("method_declaration -> modifier type_specifier id (param) "
|
|
|
52 |
+"{ local_declaration stmt_sequence }"); };
|
225 |
ira |
53 |
|
228 |
ira |
54 |
type_specifier : INT { System.out.println ("type_specifier -> int"); }
|
|
|
55 |
| FLOAT { System.out.println ("type_specifier -> float"); }
|
|
|
56 |
| VOID { System.out.println ("type_specifier -> void"); };
|
225 |
ira |
57 |
|
228 |
ira |
58 |
param : type_specifier ID { System.out.println ("param -> type_specifier id"); }
|
|
|
59 |
| VOID { System.out.println ("param -> void"); };
|
225 |
ira |
60 |
|
228 |
ira |
61 |
local_declaration : type_specifier ID SEMI
|
|
|
62 |
{ System.out.println ("local_declaration -> id;"); }
|
|
|
63 |
| { System.out.println ("local_declaration -> epsilon"); };
|
225 |
ira |
64 |
|
228 |
ira |
65 |
stmt_sequence : stmt_sequence statement
|
|
|
66 |
{ System.out.println ("stmt_sequence -> stmt_sequence statement"); }
|
|
|
67 |
| statement { System.out.println ("stmt_sequence -> statement"); };
|
225 |
ira |
68 |
|
228 |
ira |
69 |
statement : if_stmt { System.out.println ("statement -> if_stmt"); }
|
|
|
70 |
| while_stmt { System.out.println ("statement -> while_stmt"); }
|
|
|
71 |
| assign_stmt { System.out.println ("statement -> assign_stmt"); }
|
|
|
72 |
| return_stmt { System.out.println ("statement -> return_stmt"); }
|
|
|
73 |
| expr_stmt { System.out.println ("statement -> expr_stmt"); };
|
225 |
ira |
74 |
|
228 |
ira |
75 |
if_stmt : IF LPAREN exp RPAREN statement ELSE statement
|
|
|
76 |
{ System.out.println ("if_stmt -> if (exp) statement else statement"); };
|
225 |
ira |
77 |
|
228 |
ira |
78 |
while_stmt : WHILE LPAREN exp RPAREN statement
|
|
|
79 |
{ System.out.println ("while_stmt -> (exp) statement"); };
|
225 |
ira |
80 |
|
228 |
ira |
81 |
assign_stmt : ID ASSIGNOP exp SEMI
|
|
|
82 |
{ System.out.println ("assign_stmt -> id = exp;"); };
|
225 |
ira |
83 |
|
228 |
ira |
84 |
return_stmt : RETURN exp SEMI { System.out.println ("return_stmt -> return exp;"); }
|
|
|
85 |
| RETURN SEMI { System.out.println ("return_stmt -> return;"); };
|
225 |
ira |
86 |
|
228 |
ira |
87 |
expr_stmt : exp SEMI { System.out.println ("expr_stmt -> exp;"); };
|
225 |
ira |
88 |
|
228 |
ira |
89 |
exp : exp ADDOP term { System.out.println ("exp -> exp + term"); }
|
|
|
90 |
| term { System.out.println ("exp -> term"); };
|
225 |
ira |
91 |
|
228 |
ira |
92 |
term : term MULOP factor { System.out.println ("term -> term * factor"); }
|
|
|
93 |
| factor { System.out.println ("term -> factor"); };
|
225 |
ira |
94 |
|
228 |
ira |
95 |
factor : LPAREN exp RPAREN { System.out.println ("factor -> (exp)"); }
|
|
|
96 |
| ADDOP factor { System.out.println ("factor -> + factor"); }
|
|
|
97 |
| NUM { System.out.println ("factor -> NUM"); }
|
|
|
98 |
| ID { System.out.println ("factor -> ID"); };
|
225 |
ira |
99 |
|
|
|
100 |
%%
|
|
|
101 |
|
226 |
ira |
102 |
private Yylex lexer;
|
225 |
ira |
103 |
|
226 |
ira |
104 |
public void yyerror (String error)
|
|
|
105 |
{
|
|
|
106 |
System.out.println ("Parse Error: " + error);
|
|
|
107 |
}
|
225 |
ira |
108 |
|
226 |
ira |
109 |
int yylex ()
|
|
|
110 |
{
|
|
|
111 |
int lex_return = -1;
|
|
|
112 |
|
|
|
113 |
try
|
|
|
114 |
{
|
|
|
115 |
lex_return = lexer.yylex();
|
|
|
116 |
}
|
|
|
117 |
catch (IOException e)
|
|
|
118 |
{
|
|
|
119 |
System.out.println ("IO Error: " + e);
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
return lex_return;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
public Parser (Reader r)
|
|
|
126 |
{
|
|
|
127 |
lexer = new Yylex (r, this);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
public static void main (String[] args) throws Exception
|
|
|
131 |
{
|
|
|
132 |
Parser yyparser = new Parser (new FileReader (args[0]));
|
|
|
133 |
yyparser.yyparse();
|
|
|
134 |
}
|
|
|
135 |
|