Hello everyone, I have a production that is giving me a problem. It turns out to be an array element. I have been given what my output of the AST should be and is as follows:
Code:
begin
real
Ident x (1)
boolean
Ident z (2)
integer
Ident a (3)
Ident b (4)
array <---- The problem
ArrElem y (5) <----
U- <--- No worry just a unary minus
IntConst 1
IntConst 3
:=
There is more but I believe this is the relevant part. What my AST output is:
Code:
begin
real
Ident x (1)
boolean
Ident z (2)
integer
Ident a (3)
Ident b (4)
array <---- My differences
real <----
[ <----
Ident y (5) <----
-
IntConst 1
IntConst 3
:=
The relevant part of the CFG productions from my yacc file are as follows:
Code:
%{
#include "tree.h"
extern tree root;
%}
%token <i> Ident 1 IntConst 2
%token <f> RealConst 3
%token And 4 Array 5
%token <i> Begin 6
%token Boolean 7 Div 8 Do 9 Else 10
%token End 11 False 12 For 13 Goto 14 If 15
%token Imply 16 Integer 17 Label 18 Not 19 Or 20
%token Own 21 Procedure 22 Real 23 Step 24 String 25
%token Then 26 True 27 Until 28 Value 29 While 30
%token Plus 31 Minus 32 Mult 33 Divide 34 Less 35
%token LessEq 36 Great 37 GreatEq 38 Eq 39 NotEq 40
%token Comma 41 Colon 42 Semi 43 LParan 44 RParan 45
%token LBrak 46 RBrak 47 Assign 48 NoType 49
%token <b> BoolConst 50
%start program
%union { tree t; int i; float f; int b; }
%type <t> program block optdecls decl vardecl type idlist arraydecl arraylist arrayseg
%type <t> stmts stmt u_stmt assign dummy for_stmt if_stmt var factor term sum
%type <t> brel relation bsecond bfactor bterm expr a_expr
%%
program
: block
{ root = $1; }
;
block
: Begin optdecls stmts End
{ $$ = buildTree(Begin, $2, $3, buildTree(End,NULL, NULL,NULL)); }
;
optdecls
: /* empty */
{ $$ = NULL; }
| decl Semi optdecls
{ $$ = $1; $$->next = $3; }
;
decl
: vardecl
{ $$ = $1; }
| arraydecl
{ $$ = $1; }
;
vardecl
: type idlist
{ $$ = $1; $$->first = $2; }
;
type
: Real
{ $$ = buildFloatTree(Real, $<f>1); }
| Integer
{ $$ = buildIntTree(Integer, $<i>1); }
| Boolean
{ $$ = buildIntTree(Boolean, $<i>1); }
;
idlist
: Ident
{ $$ = buildIntTree(Ident, $1); }
| Ident Comma idlist
{ $$ = buildIntTree(Ident, $1); $$->next = $3; }
;
arraydecl
: Array arraylist
{ $$ = buildTree(Array, NULL, $2, NULL); }
| type Array arraylist
{ $$ = buildTree(Array, $1, $3, NULL); }
;
arraylist
: arrayseg
{ $$ = $1; }
| arrayseg Comma arraylist
{ $$ = $1; $$->next = $3; }
;
arrayseg
: Ident LBrak a_expr Colon a_expr RBrak
{ $$ = buildTree(LBrak, buildIntTree(Ident, $1), $3, $5); }
;
And lastly the test file I have been given:
Code:
begin
real x; boolean z; integer a, b;
real array y[-1:3];
x := 3.0+4.0*9.0; a:= 12345;
z := true;
if z and x >= 12.56
then
begin z := a < 56 or x <= 235.672;
end
else
z := not z and x != (if b > 4 then 2.5 else 1.9);
for b := a+3 step -1 until -2034 div a
do
y[b] := 27 - x * y[b-1];
end
I am trying the get mine look like his. I am not sure if a new type in the productions need to be spelled out or not. It seems like it needs to be. Am I off here? I would appreciate if someone could show me what do to correct this. Thank you!