help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: %left


From: Sasan Forghani
Subject: Re: %left
Date: Mon, 9 Aug 2010 09:51:26 -0400

Thank you Chris for the information.  However, I think you are right in
saying that I am confused in regards to how Bison is working.  After making
the modifications you suggested, nothing has changed.  Here is the output
and below is the code snippets with the changes.  Also, the input is a := (b
+ c) + d

const_decl -> Epsilon
ident_list -> IDENTIFIER
basic_type -> INTEGER
type -> basic_type
basic_var -> ident_list: type
ident_list -> IDENTIFIER
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_list -> basic_var; var_list
var_decl -> VAR var_list
proc_decl -> Epsilon
This is the value of $<blIdentifier>$ at ref_name begin: a
This is the value of $<blIdentifier>$ at ref_name end: V0001
ref_name -> IDENTIFIER
This is the value of $<blIdentifier>$ at ref_name begin: b
This is the value of $<blIdentifier>$ at ref_name end: V0002
ref_name -> IDENTIFIER
expr -> ref_name
This is the value of $<blIdentifier>$ at ref_name begin: c
This is the value of $<blIdentifier>$ at ref_name end: V0003
ref_name -> IDENTIFIER
expr -> ref_name
$<blIdentifier>$ at ADDOP at begin is: V0003
$<blIdentifier>1 at ADDOP at begin is: b
$<blIdentifier>3 at ADDOP at begin is: c
$<blIdentifier>1 at ADDOP end is: b
$<blIdentifier>3 at ADDOP end is: c
$<blIdentifier>$ at ADDOP end 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
This is the value of $<blIdentifier>$ at ( expr ) after
$<blIdentifier>$ = $<blIdentifier>2: b
expr -> (expr)
This is the value of $<blIdentifier>$ at ref_name begin: d
This is the value of $<blIdentifier>$ at ref_name end: V0004
ref_name -> IDENTIFIER
expr -> ref_name
$<blIdentifier>$ at ADDOP at begin is: V0004
$<blIdentifier>1 at ADDOP at begin is: a
$<blIdentifier>3 at ADDOP at begin is: d
$<blIdentifier>1 at ADDOP end is: a
$<blIdentifier>3 at ADDOP end is: d
$<blIdentifier>$ at ADDOP end is: T0002
expr -> expr ADDOP expr
$<blIdentifier>1 at := is: a
$<blIdentifier>3 at := is: a
$<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 .

ref_name

: BL_IDENTIFIER {

    struct symTblEntry* addressOfEntry;
    addressOfEntry = findEntryInRow($<blIdentifier>1);
    printf("This is the value of $<blIdentifier>$ at ref_name begin: %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>$ = strdup(varLocation);
    printf("This is the value of $<blIdentifier>$ at ref_name end: %s\n",
                    $<blIdentifier>$);
}

stmt

: '(' 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);

    $<blIdentifier>$ = $<blIdentifier>2;

    printf("This is the value of $<blIdentifier>$ at ( expr )
after\n$<blIdentifier>$ = $<blIdentifier>2: %s\n", $<blIdentifier>$);
}

stmt

: expr BL_ADDOP expr {

    printf("$<blIdentifier>$ at ADDOP at begin is: %s\n", $<blIdentifier>$);
    printf("$<blIdentifier>1 at ADDOP at begin is: %s\n", $<blIdentifier>1);
    printf("$<blIdentifier>3 at ADDOP at begin is: %s\n", $<blIdentifier>3);

    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>$ = strdup(bufferT);
    printf("$<blIdentifier>1 at ADDOP end is: %s\n", $<blIdentifier>1);
    printf("$<blIdentifier>3 at ADDOP end is: %s\n", $<blIdentifier>3);
    printf("$<blIdentifier>$ at ADDOP end is: %s\n", $<blIdentifier>$);
}

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;
}

On Fri, Aug 6, 2010 at 7:47 PM, Chris verBurg <address@hidden>wrote:

>
>
> On Fri, Aug 6, 2010 at 5:17 AM, Sasan Forghani <address@hidden>wrote:
>
>>
>> ..
>>
>> expr : expr BL_ADDOP expr
>> ...
>>    char bufferT[6];
>> ...
>>    $<blIdentifier>$ = bufferT;
>>
>
> $<blIdentifier>$ is a char* isn't it?  In which case this assigns it to the
> temporary bufferT, which is going to get wiped out as soon as it goes out of
> scope.
>
> Maybe use strdup?  "$<blIdentifier>$ = strdup(bufferT);" ?
>
>
> ...
>>
>> | '(' expr ')'
>>  {
>>    printf("This is the value of $<blIdentifier>$ at ( expr ): %s\n",
>> $<blIdentifier>$);
>>
>
> At this point, you haven't assigned to $$.
>
>
> ...
>> ref_name : BL_IDENTIFIER
>> ...
>>    char varLocation[7];
>> ...
>>    $<blIdentifier>$ = varLocation;
>>
>
> Another place where the buffer will go out of scope and wipe out $$.
> (strdup again?)
>
> Are you still a bit confused about how the $ variables work?  And/or how
> bison rules have associated values?
>
> -Chris
>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]