• Welcome to Valhalla Legends Archive.
 

[C/LEX/YACC] Parsing parameter assignments: shift/reduce conflicts

Started by Rule, June 16, 2006, 01:48 AM

Previous topic - Next topic

Rule

Any help appreciated.  Although this post is somewhat long, and I have linked to my code (browser viewable) to avoid making a mess here, my question is rather simple and undemanding; in any case, I've provided enough code here so that it isn't necessary to look at the files to help me with the most conspicuous problem -- shift reduce conflicts.

I'm modifying a parser that searches for a line of the form

name := value

and assigns the value to name.

I am changing it so that it can do single-pass parameter interpolation of the form

paramName := value
otherParam := $[paramName]


given that paramName has been defined.

The original files: yacc file, lexer, header.

The modified files: yacc file, lexer, header.

The original files work decently for their intended purpose.  I have only changed the lexer to recognize $ and return EXTRACT.  I wrote the addAssign and assignLook C subroutines, and changed the yacc file so that assignments are added to an assignment_list (a data structure I added in the header file).  I also added the global variables change, and asgnList (in the header file), and the following rules in the yacc grammar:

|  datatype IDEN becomes EXTRACT OBRACK IDEN CBRACK {
      extern int change;
      myasgn = assignLook($6)->assign;

     
      if (!change)
      {
        fprintf(stderr, "gpar: error:  can't extrapolate parameter %s, as it hasn't been initialized.\n", $6);
        exit(0);
      }

      myasgn.name=$2;
      addAssign(myasgn);
      gotit=1;
    }



datatype: LONG | DOUBLE | STRING | IVEC | FVEC | VECTOR | SCALAR
  ;


The changes are rather small, and it should be very easy to take a quick look.

As I said above, I am trying to get the parser to recognize an expression of the form something:=$[somevalue], where somevalue was assigned.  As things are now,
there are shift-reduce conflicts because yacc only has one token of lookahead (and there are currently other rules like):


someannoyingrule: DOUBLE IDEN EVILTOKENS


I'm trying to think of a way to resolve these conflicts and solve my problem without drastically altering my yacc file.  Any clever suggestions? :)
I'd be very grateful for any help, including comments on my code as well.  I know I've been a bit sloppy -- for example, I'll probably declare those global variables in the header file to be static globals in the yacc file.  I'll also change return values for errors.

Thanks in advance.

Edit:  I would really like the assignments to have the form

myParam=$[greatvalue]


I know a quick fix would simply be to add another token near the beginning of the assignment: e.g.

$myParam=$[greatvalue]


Although this may seem like no big deal, the parser is intricately woven into other lengthy subroutines that assume the declaration will start with an IDEN.  I have a feeling that I may be able to use associativity or precedence to my advantage in this situation, although I'm not familiar enough with these things to be sure how (that is, without completely rewriting my parser).