I've modified my code to the following: $<blIdentifier>$ = varLocation //at the ref_name rule level $<blIdentifier>$ = $<blIdentifier>2 //at the ( expr ) rule level $<blIdentifier>$ = bufferT //at the expr rule level
The output is still incorrect. Moving up the stack the value does not hold. For example after derivating to the ref_name level for the variable b, $<blIdentifier>$ is assigned the value V0002. At the ( expr ) level, $<blIdentifier>$ is assigned the value b and not V0002. At the expr rule level, $<blIdentifier>$ is assigned the value b and not V0002. In all cases where b was assigned, V0002 should have been assigned. In the code below, the action $<blIdentifier>$ = $<blIdentifier>2 at the ( expr ) rule level has been removed; removing the action did not change the values of either $<blIdentifier>$ or $<blIdentifier>2. So, how am I suppose to send data back up the stack? Here are the code snippets that were run by the parser: stmt : ref_name BL_ASSIGNMENT expr { fprintf(ptrToIrFile, "mov %s %s\n", $<blIdentifier>3, $<blIdentifier>1); printf("$<blIdentifier>1 at := is: %s\n", $<blIdentifier>1); printf("$<blIdentifier>3 at := is: %s\n", $<blIdentifier>3); printf("$<blIdentifier>$ at := is: %s\n", $<blIdentifier>$); amtOfTemps = 0; } expr : expr BL_ADDOP expr { char bufferSym[4]; if(strcmp($<blOperator>2, "+") == 0) strcpy(bufferSym, "add"); else strcpy(bufferSym, "sub"); char bufferT[6]; changeToString(++amtOfTemps, "T", bufferT); fprintf(ptrToIrFile, "%s %s %s %s\n", bufferSym, $<blIdentifier>1, $<blIdentifier>3, bufferT); $<blIdentifier>$ = bufferT; printf("$<blIdentifier>1 at ADDOP is: %s\n", $<blIdentifier>1); printf("$<blIdentifier>3 at ADDOP is: %s\n", $<blIdentifier>3); printf("$<blIdentifier>$ at ADDOP is: %s\n", $<blIdentifier>$); } | '(' expr ')' { printf("This is the value of $<blIdentifier>$ at ( expr ): %s\n", $<blIdentifier>$); printf("This is the value of $<blIdentifier>2 at ( expr ): %s\n", $<blIdentifier>2); } ref_name : BL_IDENTIFIER { struct symTblEntry* addressOfEntry; addressOfEntry = findEntryInRow($<blIdentifier>1); printf("This is: %s\n", $<blIdentifier>1); char varLocation[7]; if(addressOfEntry-> entryRole == VARIABLE) changeToString(addressOfEntry-> declarationNumber, "V", varLocation); else if(addressOfEntry-> entryRole == VALPARAMETER) changeToString(addressOfEntry-> declarationNumber, "P", varLocation); else if(addressOfEntry-> entryRole == REFPARAMETER) changeToString(addressOfEntry-> declarationNumber, "*P", varLocation); $<blIdentifier>$ = varLocation; printf("This is the value of $<blIdentifier>$ at ref_name: %s\n", $<blIdentifier>$); } Here is the test case: PROGRAM simple; VAR a,b,c,d,e: integer; BEGIN a := b + c; a := b + c + d; a := b + c + d + e; a := (b + c); a := (b + c) + d; a := b + (c + d); a := (b + c) + d + e; a := b + (c + d) + e; a := b + c + (d + e); a := (b + c) + (d + e) END. Here is the output after running the parser on a test case: const_decl -> Epsilon ident_list -> IDENTIFIER ident_list -> IDENTIFIER, ident_list ident_list -> IDENTIFIER, ident_list ident_list -> IDENTIFIER, ident_list ident_list -> IDENTIFIER, ident_list basic_type -> INTEGER type -> basic_type basic_var -> ident_list: type var_list -> basic_var; var_decl -> VAR var_list proc_decl -> Epsilon This is: a This is the value of $<blIdentifier>$ at ref_name: V0001 ref_name -> IDENTIFIER This is: b This is the value of $<blIdentifier>$ at ref_name: V0002 ref_name -> IDENTIFIER expr -> ref_name This is: c This is the value of $<blIdentifier>$ at ref_name: V0003 ref_name -> IDENTIFIER expr -> ref_name $<blIdentifier>1 at ADDOP is: b $<blIdentifier>3 at ADDOP is: c $<blIdentifier>$ at ADDOP is: T0001 expr -> expr ADDOP expr $<blIdentifier>1 at := is: a $<blIdentifier>3 at := is: b $<blIdentifier>$ at := is: + stmt -> ref_name := expr This is: a This is the value of $<blIdentifier>$ at ref_name: V0001 ref_name -> IDENTIFIER This is: b This is the value of $<blIdentifier>$ at ref_name: V0002 ref_name -> IDENTIFIER expr -> ref_name This is: c This is the value of $<blIdentifier>$ at ref_name: V0003 ref_name -> IDENTIFIER expr -> ref_name This is: d This is the value of $<blIdentifier>$ at ref_name: V0004 ref_name -> IDENTIFIER expr -> ref_name $<blIdentifier>1 at ADDOP is: c $<blIdentifier>3 at ADDOP is: d $<blIdentifier>$ at ADDOP is: T0001 expr -> expr ADDOP expr $<blIdentifier>1 at ADDOP is: b $<blIdentifier>3 at ADDOP is: c $<blIdentifier>$ at ADDOP is: T0002 expr -> expr ADDOP expr $<blIdentifier>1 at := is: a $<blIdentifier>3 at := is: b $<blIdentifier>$ at := is: + stmt -> ref_name := expr This is: a This is the value of $<blIdentifier>$ at ref_name: V0001 ref_name -> IDENTIFIER This is: b This is the value of $<blIdentifier>$ at ref_name: V0002 ref_name -> IDENTIFIER expr -> ref_name This is: c This is the value of $<blIdentifier>$ at ref_name: V0003 ref_name -> IDENTIFIER expr -> ref_name This is: d This is the value of $<blIdentifier>$ at ref_name: V0004 ref_name -> IDENTIFIER expr -> ref_name This is: e This is the value of $<blIdentifier>$ at ref_name: V0005 ref_name -> IDENTIFIER expr -> ref_name $<blIdentifier>1 at ADDOP is: d $<blIdentifier>3 at ADDOP is: e $<blIdentifier>$ at ADDOP is: T0001 expr -> expr ADDOP expr $<blIdentifier>1 at ADDOP is: c $<blIdentifier>3 at ADDOP is: d $<blIdentifier>$ at ADDOP is: T0002 expr -> expr ADDOP expr $<blIdentifier>1 at ADDOP is: b $<blIdentifier>3 at ADDOP is: c $<blIdentifier>$ at ADDOP is: T0003 expr -> expr ADDOP expr $<blIdentifier>1 at := is: a $<blIdentifier>3 at := is: b $<blIdentifier>$ at := is: + stmt -> ref_name := expr This is: a This is the value of $<blIdentifier>$ at ref_name: V0001 ref_name -> IDENTIFIER This is: b This is the value of $<blIdentifier>$ at ref_name: V0002 ref_name -> IDENTIFIER expr -> ref_name This is: c This is the value of $<blIdentifier>$ at ref_name: V0003 ref_name -> IDENTIFIER expr -> ref_name $<blIdentifier>1 at ADDOP is: b $<blIdentifier>3 at ADDOP is: c $<blIdentifier>$ at ADDOP is: T0001 expr -> expr ADDOP expr This is the value of $<blIdentifier>$ at ( expr ): c This is the value of $<blIdentifier>2 at ( expr ): b expr -> (expr) $<blIdentifier>1 at := is: a $<blIdentifier>3 at := is: a $<blIdentifier>$ at := is: b stmt -> ref_name := expr This is: a This is the value of $<blIdentifier>$ at ref_name: V0001 ref_name -> IDENTIFIER This is: b This is the value of $<blIdentifier>$ at ref_name: V0002 ref_name -> IDENTIFIER expr -> ref_name This is: c This is the value of $<blIdentifier>$ at ref_name: V0003 ref_name -> IDENTIFIER expr -> ref_name $<blIdentifier>1 at ADDOP is: b $<blIdentifier>3 at ADDOP is: c $<blIdentifier>$ at ADDOP is: T0001 expr -> expr ADDOP expr This is the value of $<blIdentifier>$ at ( expr ): c This is the value of $<blIdentifier>2 at ( expr ): b expr -> (expr) This is: d This is the value of $<blIdentifier>$ at ref_name: V0004 ref_name -> IDENTIFIER expr -> ref_name $<blIdentifier>1 at ADDOP is: a $<blIdentifier>3 at ADDOP is: d $<blIdentifier>$ at ADDOP is: T0002 expr -> expr ADDOP expr $<blIdentifier>1 at := is: a $<blIdentifier>3 at := is: a $<blIdentifier>$ at := is: + stmt -> ref_name := expr This is: a This is the value of $<blIdentifier>$ at ref_name: V0001 ref_name -> IDENTIFIER This is: b This is the value of $<blIdentifier>$ at ref_name: V0002 ref_name -> IDENTIFIER expr -> ref_name This is: c This is the value of $<blIdentifier>$ at ref_name: V0003 ref_name -> IDENTIFIER expr -> ref_name This is: d This is the value of $<blIdentifier>$ at ref_name: V0004 ref_name -> IDENTIFIER expr -> ref_name $<blIdentifier>1 at ADDOP is: c $<blIdentifier>3 at ADDOP is: d $<blIdentifier>$ at ADDOP is: T0001 expr -> expr ADDOP expr This is the value of $<blIdentifier>$ at ( expr ): d This is the value of $<blIdentifier>2 at ( expr ): c expr -> (expr) $<blIdentifier>1 at ADDOP is: b $<blIdentifier>3 at ADDOP is: + $<blIdentifier>$ at ADDOP is: T0002 expr -> expr ADDOP expr $<blIdentifier>1 at := is: a $<blIdentifier>3 at := is: b $<blIdentifier>$ at := is: + stmt -> ref_name := expr This is: a This is the value of $<blIdentifier>$ at ref_name: V0001 ref_name -> IDENTIFIER This is: b This is the value of $<blIdentifier>$ at ref_name: V0002 ref_name -> IDENTIFIER expr -> ref_name This is: c This is the value of $<blIdentifier>$ at ref_name: V0003 ref_name -> IDENTIFIER expr -> ref_name $<blIdentifier>1 at ADDOP is: b $<blIdentifier>3 at ADDOP is: c $<blIdentifier>$ at ADDOP is: T0001 expr -> expr ADDOP expr This is the value of $<blIdentifier>$ at ( expr ): c This is the value of $<blIdentifier>2 at ( expr ): b expr -> (expr) This is: d This is the value of $<blIdentifier>$ at ref_name: V0004 ref_name -> IDENTIFIER expr -> ref_name This is: e This is the value of $<blIdentifier>$ at ref_name: V0005 ref_name -> IDENTIFIER expr -> ref_name $<blIdentifier>1 at ADDOP is: d $<blIdentifier>3 at ADDOP is: e $<blIdentifier>$ at ADDOP is: T0002 expr -> expr ADDOP expr $<blIdentifier>1 at ADDOP is: a $<blIdentifier>3 at ADDOP is: d $<blIdentifier>$ at ADDOP is: T0003 expr -> expr ADDOP expr $<blIdentifier>1 at := is: a $<blIdentifier>3 at := is: a $<blIdentifier>$ at := is: + stmt -> ref_name := expr This is: a This is the value of $<blIdentifier>$ at ref_name: V0001 ref_name -> IDENTIFIER This is: b This is the value of $<blIdentifier>$ at ref_name: V0002 ref_name -> IDENTIFIER expr -> ref_name This is: c This is the value of $<blIdentifier>$ at ref_name: V0003 ref_name -> IDENTIFIER expr -> ref_name This is: d This is the value of $<blIdentifier>$ at ref_name: V0004 ref_name -> IDENTIFIER expr -> ref_name $<blIdentifier>1 at ADDOP is: c $<blIdentifier>3 at ADDOP is: d $<blIdentifier>$ at ADDOP is: T0001 expr -> expr ADDOP expr This is the value of $<blIdentifier>$ at ( expr ): d This is the value of $<blIdentifier>2 at ( expr ): c expr -> (expr) This is: e This is the value of $<blIdentifier>$ at ref_name: V0005 ref_name -> IDENTIFIER expr -> ref_name $<blIdentifier>1 at ADDOP is: + $<blIdentifier>3 at ADDOP is: e $<blIdentifier>$ at ADDOP is: T0002 expr -> expr ADDOP expr $<blIdentifier>1 at ADDOP is: b $<blIdentifier>3 at ADDOP is: + $<blIdentifier>$ at ADDOP is: T0003 expr -> expr ADDOP expr $<blIdentifier>1 at := is: a $<blIdentifier>3 at := is: b $<blIdentifier>$ at := is: + stmt -> ref_name := expr This is: a This is the value of $<blIdentifier>$ at ref_name: V0001 ref_name -> IDENTIFIER This is: b This is the value of $<blIdentifier>$ at ref_name: V0002 ref_name -> IDENTIFIER expr -> ref_name This is: c This is the value of $<blIdentifier>$ at ref_name: V0003 ref_name -> IDENTIFIER expr -> ref_name This is: d This is the value of $<blIdentifier>$ at ref_name: V0004 ref_name -> IDENTIFIER expr -> ref_name This is: e This is the value of $<blIdentifier>$ at ref_name: V0005 ref_name -> IDENTIFIER expr -> ref_name $<blIdentifier>1 at ADDOP is: d $<blIdentifier>3 at ADDOP is: e $<blIdentifier>$ at ADDOP is: T0001 expr -> expr ADDOP expr This is the value of $<blIdentifier>$ at ( expr ): e This is the value of $<blIdentifier>2 at ( expr ): d expr -> (expr) $<blIdentifier>1 at ADDOP is: c $<blIdentifier>3 at ADDOP is: + $<blIdentifier>$ at ADDOP is: T0002 expr -> expr ADDOP expr $<blIdentifier>1 at ADDOP is: b $<blIdentifier>3 at ADDOP is: c $<blIdentifier>$ at ADDOP is: T0003 expr -> expr ADDOP expr $<blIdentifier>1 at := is: a $<blIdentifier>3 at := is: b $<blIdentifier>$ at := is: + stmt -> ref_name := expr This is: a This is the value of $<blIdentifier>$ at ref_name: V0001 ref_name -> IDENTIFIER This is: b This is the value of $<blIdentifier>$ at ref_name: V0002 ref_name -> IDENTIFIER expr -> ref_name This is: c This is the value of $<blIdentifier>$ at ref_name: V0003 ref_name -> IDENTIFIER expr -> ref_name $<blIdentifier>1 at ADDOP is: b $<blIdentifier>3 at ADDOP is: c $<blIdentifier>$ at ADDOP is: T0001 expr -> expr ADDOP expr This is the value of $<blIdentifier>$ at ( expr ): c This is the value of $<blIdentifier>2 at ( expr ): b expr -> (expr) This is: d This is the value of $<blIdentifier>$ at ref_name: V0004 ref_name -> IDENTIFIER expr -> ref_name This is: e This is the value of $<blIdentifier>$ at ref_name: V0005 ref_name -> IDENTIFIER expr -> ref_name $<blIdentifier>1 at ADDOP is: d $<blIdentifier>3 at ADDOP is: e $<blIdentifier>$ at ADDOP is: T0002 expr -> expr ADDOP expr This is the value of $<blIdentifier>$ at ( expr ): e This is the value of $<blIdentifier>2 at ( expr ): d expr -> (expr) $<blIdentifier>1 at ADDOP is: a $<blIdentifier>3 at ADDOP is: + $<blIdentifier>$ at ADDOP is: T0003 expr -> expr ADDOP expr $<blIdentifier>1 at := is: a $<blIdentifier>3 at := is: a $<blIdentifier>$ at := is: + stmt -> ref_name := expr stmt_seq -> stmt; stmt_seq -> stmt; stmt_seq stmt_seq -> stmt; stmt_seq stmt_seq -> stmt; stmt_seq stmt_seq -> stmt; stmt_seq stmt_seq -> stmt; stmt_seq stmt_seq -> stmt; stmt_seq stmt_seq -> stmt; stmt_seq stmt_seq -> stmt; stmt_seq stmt_seq -> stmt; stmt_seq comp_stmt -> BEGIN stmt_seq END block -> const_decl var_decl proc_decl comp_stmt prog -> PROGRAM IDENTIFIER; block . _______________________________________________ help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison