[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
update to %left is not working... still not working... trace of stack
From: |
Sasan Forghani |
Subject: |
update to %left is not working... still not working... trace of stack |
Date: |
Mon, 9 Aug 2010 10:02:08 -0400 |
Below is some thinking I've done on the %left problem not working. I'm
hoping that someone will see what is wrong and point it out to me so I can
get the behavior I want. If input is a := b + c + d, Bison is adding c + d
and then adding b. Since add is %left, it should add b + c and then d.
Thank you for all the help so far.
*
*
*input:* id assignment id addop id addop id
*key for nt:* R = reference name, E = expression, S = statement
*key for t:* addop is + or -, assignment is :=, id is identifier
*stack:*
//shift id; can reduce to E since E : id or etc, but since lookahead is
assignment don't reduce to E or etc
*id*
//reduce id to R since lookahead is assignment and there is a rule S : R
assignment E
*R*
//shift assignment
*R assignment*
//shift id since no reduction can be made on R assignment
*R assignment id*
//reduce id; reduction is for E addop E not ro S : R assignment E; addop is
the lookahead and has a
//a higher prec than assignment
*R assignment R*
*R assignment E*
//shift addop since it has higher prec than assignment
*R assignment E addop*
//shift id since nor reduction can be made on R assignment E addop
*R assignment E addop id*
//reduce id; though the the lookahead is addop, it is left associative and
reducing id matches rule
//E addop E
*R assignment E addop R*
*R assignment E addop E*
//shift addop; this should not occur since addop is left associative
*R assignment E addop E addop*
*R assignment E addop E addop id*
*R assignment E addop E addop R*
*R assignment E addop E addop E*
*R assignment E addop E*
*R assignment E*
*S*
The above stack as been deduced using the following output:
ju L0001
la L0001
add c d T0001
add b c T0002
mov b a
halt
Since the input was *a := b + c + d*, the first add should have been: *add
b c T0001*. The second add should have been: *add T0001 c T0002*. And the
mov should have been: *mov T0002 a*. The incorrect operands, to each
command, such as the second add command of *add b c T0002* instead of *add
T0001 c T0002* are due to assignments to $<type>$ not being sent up the
stack. However, the bigger issue is why a shift is occurring when a
reduction should occur? At least from the output, it seems that a shift is
occurring and not a reduction. The lexical analyzer is Flex generated.
Below is printf output of each production that was executed as the
translation progressed and below the printf output is the code snippets that
were executed to produce the above IR file.
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
This is: d
This is the value of $<blIdentifier>$ at ref_name: V0004
ref_name -> IDENTIFIER
expr -> ref_name
$<blIdentifier>$ at ADDOP at begin is: V0004
$<blIdentifier>1 at ADDOP is: c
$<blIdentifier>3 at ADDOP is: d
$<blIdentifier>$ at ADDOP is: T0001
expr -> expr ADDOP expr
$<blIdentifier>$ at ADDOP at begin is: +
$<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
stmt_seq -> stmt;
comp_stmt -> BEGIN stmt_seq END
block -> const_decl var_decl proc_decl comp_stmt
prog -> PROGRAM IDENTIFIER; block .
Code Snippets:
%union
{
char* blIdentifier;
char* blOperator;
float blRealConst;
int blIntConst;
}
%token <blIdentifier> BL_IDENTIFIER
%right BL_ASSIGNMENT
%left <blOperator> BL_RELOP
%right <blOperator> BL_ADDOP BL_OR
%left <blOperator> BL_MULOP BL_AND
%left BL_NOT
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>$);
}
{printf("ref_name -> IDENTIFIER\n");}
expr :
expr BL_ADDOP expr {
printf("$<blIdentifier>$ at ADDOP at begin is: %s\n", $<blIdentifier>$);
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>$);
}
{printf("expr -> expr ADDOP expr\n");}
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;
}
{printf("stmt -> ref_name := expr\n");}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- update to %left is not working... still not working... trace of stack,
Sasan Forghani <=