Line 35... |
Line 35... |
35 |
%token ASSIGNOP
|
35 |
%token ASSIGNOP
|
36 |
|
36 |
|
37 |
/* JMinus Grammar */
|
37 |
/* JMinus Grammar */
|
38 |
%%
|
38 |
%%
|
39 |
program : modifier CLASS ID LBRACE method_declaration RBRACE
|
39 |
program : modifier CLASS ID LBRACE method_declaration RBRACE
|
40 |
{ System.out.println ("program reduce"); };
|
40 |
{ System.out.println ("program -> modifier class id { method_declaration }"); };
|
41 |
|
41 |
|
42 |
modifier : PUBLIC { System.out.println ("modifier reduce"); }
|
42 |
modifier : PUBLIC { System.out.println ("modifier -> public"); }
|
43 |
| PRIVATE { System.out.println ("modifier reduce"); };
|
43 |
| PRIVATE { System.out.println ("modifier -> private"); };
|
44 |
|
44 |
|
45 |
method_declaration : modifier type_specifier ID LPAREN param RPAREN LBRACE local_declaration stmt_sequence RBRACE
|
45 |
method_declaration : modifier type_specifier ID LPAREN param RPAREN
|
- |
|
46 |
LBRACE local_declaration stmt_sequence RBRACE
|
46 |
{ System.out.println ("method_declaration reduce"); };
|
47 |
{ System.out.println ("method_declaration -> modifier type_specifier id (param) "
|
- |
|
48 |
+"{ local_declaration stmt_sequence }"); };
|
47 |
|
49 |
|
48 |
type_specifier : INT { System.out.println ("type_specifier -> INT reduce"); }
|
50 |
type_specifier : INT { System.out.println ("type_specifier -> int"); }
|
49 |
| FLOAT { System.out.println ("type_specifier -> INT reduce"); }
|
51 |
| FLOAT { System.out.println ("type_specifier -> float"); }
|
50 |
| VOID { System.out.println ("type_specifier -> INT reduce"); };
|
52 |
| VOID { System.out.println ("type_specifier -> void"); };
|
51 |
|
53 |
|
52 |
param : type_specifier ID { System.out.println ("param reduce"); }
|
54 |
param : type_specifier ID { System.out.println ("param -> type_specifier id"); }
|
53 |
| VOID { System.out.println ("param -> VOID reduce"); };
|
55 |
| VOID { System.out.println ("param -> void"); };
|
54 |
|
56 |
|
- |
|
57 |
local_declaration : type_specifier ID SEMI
|
55 |
local_declaration : type_specifier ID SEMI { System.out.println ("local_declaration reduce"); }
|
58 |
{ System.out.println ("local_declaration -> id;"); }
|
56 |
| { System.out.println ("local_declaration -> epsilon reduce"); };
|
59 |
| { System.out.println ("local_declaration -> epsilon"); };
|
57 |
|
60 |
|
- |
|
61 |
stmt_sequence : stmt_sequence statement
|
58 |
stmt_sequence : stmt_sequence statement { System.out.println ("stmt_sequence reduce"); }
|
62 |
{ System.out.println ("stmt_sequence -> stmt_sequence statement"); }
|
59 |
| statement { System.out.println ("stmt_sequence -> statement reduce"); };
|
63 |
| statement { System.out.println ("stmt_sequence -> statement"); };
|
60 |
|
64 |
|
61 |
statement : if_stmt { System.out.println ("statement -> if_stmt reduce"); }
|
65 |
statement : if_stmt { System.out.println ("statement -> if_stmt"); }
|
62 |
| while_stmt { System.out.println ("statement -> while_stmt reduce"); }
|
66 |
| while_stmt { System.out.println ("statement -> while_stmt"); }
|
63 |
| assign_stmt { System.out.println ("statement -> assign_stmt reduce"); }
|
67 |
| assign_stmt { System.out.println ("statement -> assign_stmt"); }
|
64 |
| return_stmt { System.out.println ("statement -> return_stmt reduce"); }
|
68 |
| return_stmt { System.out.println ("statement -> return_stmt"); }
|
65 |
| expr_stmt { System.out.println ("statement -> expr_stmt reduce"); };
|
69 |
| expr_stmt { System.out.println ("statement -> expr_stmt"); };
|
66 |
|
70 |
|
67 |
if_stmt : IF LPAREN exp RPAREN statement ELSE statement { System.out.println ("if_stmt reduce"); };
|
71 |
if_stmt : IF LPAREN exp RPAREN statement ELSE statement
|
- |
|
72 |
{ System.out.println ("if_stmt -> if (exp) statement else statement"); };
|
68 |
|
73 |
|
69 |
while_stmt : WHILE LPAREN exp RPAREN statement { System.out.println ("while_stmt reduce"); };
|
74 |
while_stmt : WHILE LPAREN exp RPAREN statement
|
- |
|
75 |
{ System.out.println ("while_stmt -> (exp) statement"); };
|
70 |
|
76 |
|
- |
|
77 |
assign_stmt : ID ASSIGNOP exp SEMI
|
71 |
assign_stmt : ID ASSIGNOP exp SEMI { System.out.println ("assign_stmt reduce"); };
|
78 |
{ System.out.println ("assign_stmt -> id = exp;"); };
|
72 |
|
79 |
|
73 |
return_stmt : RETURN exp SEMI { System.out.println ("return_stmt -> RETURN exp ; reduce"); }
|
80 |
return_stmt : RETURN exp SEMI { System.out.println ("return_stmt -> return exp;"); }
|
74 |
| RETURN SEMI { System.out.println ("return_stmt -> RETURN ; reduce"); };
|
81 |
| RETURN SEMI { System.out.println ("return_stmt -> return;"); };
|
75 |
|
82 |
|
76 |
expr_stmt : exp SEMI { System.out.println ("expr_stmt -> exp ; reduce"); };
|
83 |
expr_stmt : exp SEMI { System.out.println ("expr_stmt -> exp;"); };
|
77 |
|
84 |
|
78 |
exp : exp ADDOP term { System.out.println ("exp -> exp + term reduce"); }
|
85 |
exp : exp ADDOP term { System.out.println ("exp -> exp + term"); }
|
79 |
| term { System.out.println ("exp -> term reduce"); };
|
86 |
| term { System.out.println ("exp -> term"); };
|
80 |
|
87 |
|
81 |
term : term MULOP factor { System.out.println ("term -> term * factor reduce"); }
|
88 |
term : term MULOP factor { System.out.println ("term -> term * factor"); }
|
82 |
| factor { System.out.println ("term -> factor reduce"); };
|
89 |
| factor { System.out.println ("term -> factor"); };
|
83 |
|
90 |
|
84 |
factor : LPAREN exp RPAREN { System.out.println ("factor -> ( exp ) reduce"); }
|
91 |
factor : LPAREN exp RPAREN { System.out.println ("factor -> (exp)"); }
|
85 |
| ADDOP factor { System.out.println ("factor -> + factor reduce"); }
|
92 |
| ADDOP factor { System.out.println ("factor -> + factor"); }
|
86 |
| NUM { System.out.println ("factor -> NUM reduce"); }
|
93 |
| NUM { System.out.println ("factor -> NUM"); }
|
87 |
| ID { System.out.println ("factor -> ID reduce"); };
|
94 |
| ID { System.out.println ("factor -> ID"); };
|
88 |
|
95 |
|
89 |
%%
|
96 |
%%
|
90 |
|
97 |
|
91 |
private Yylex lexer;
|
98 |
private Yylex lexer;
|
92 |
|
99 |
|