parsing problem with lex/yacc - yylval returns unintended value for $1

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rajatkmitra
    New Member
    • Jun 2010
    • 5

    #1

    parsing problem with lex/yacc - yylval returns unintended value for $1

    Hi Folks,
    I am writing a simple parser to parse SPICE code ( Spice is a transistor level simulator ). The spice code looks like this -
    =============== =========
    R10 2 3 0.5e-3
    C11 4 6 0.2e-6
    L13 4 4 0.1e-9
    =============== ==========
    As you can see each line has 4 items -
    a character string followed by an integer then another integer and finally a floating point number.
    I need to return the appropriate token on a per line basis.

    I am attaching a tar ball called foo.zip that has the following files -
    1. netlist ( the source file to be parsed )
    2. spice.lex ( the lexer code )
    3. spice.y ( the grammar )
    4. Makefile

    To build the parser, simple type make ( on linux ). Then to run -
    %parser < netlist

    In the .y file I have the grammar rule where I expect $1 to return the character string, $2 to return the first integer, $3 to return the next integer value and finally $4 to return the floating point value on a per line basis. Mos of this works correctly, except $1 returns the entire line instead of the character string. Can anyone tell what's wrong in the token generator ( lex file ) or the grammar description ( .y file ) ?? Any help will be highly appreciated.

    Thanks,

    Rajat Mitra
    Attached Files
    Last edited by rajatkmitra; Jun 2 '10, 06:54 PM. Reason: Add attachment
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I can't read you files, but if your data is as yuou say it is then:

    Code:
    I typed in: R10 2 3 0.5e-3
    
    int main()
    {
       char str[4];
       int var1;
       int var2;
       float var3;
    
       scanf("%s%d%d%f\n", str, &var1, &var2, &var3);
    
       printf("%s\n%d\n%d\n%f\n", str, var1, var2, var3);
    }
    works fine. Just use fscanf with your file.

    Comment

    • rajatkmitra
      New Member
      • Jun 2010
      • 5

      #3
      Originally posted by weaknessforcats
      I can't read you files, but if your data is as yuou say it is then:

      Code:
      I typed in: R10 2 3 0.5e-3
      
      int main()
      {
         char str[4];
         int var1;
         int var2;
         float var3;
      
         scanf("%s%d%d%f\n", str, &var1, &var2, &var3);
      
         printf("%s\n%d\n%d\n%f\n", str, var1, var2, var3);
      }
      works fine. Just use fscanf with your file.
      =============== =============== =============== ===========
      Hi weaknessforcats ,
      Many thanks for your reply. Unfortunately while the fscanf works very reliably, this parser is going to have to grow to support additional ( optional ) parameters on a per line basis ( or even multiline )and needs to be written using lex/yacc.
      Can you unzip the archive that I have attached the source files are in there for you to take a look at ???

      Thanks again for your help,
      Rajat
      Attached Files

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Are you required to implement this parser with lex/yacc?
        I think it would be a lot easier to do this in perl.

        Comment

        • rajatkmitra
          New Member
          • Jun 2010
          • 5

          #5
          Originally posted by donbock
          Are you required to implement this parser with lex/yacc?
          I think it would be a lot easier to do this in perl.
          yeah it has to be lex/yacc to fit in with the rest of the software my colleague is developing. you are correct perl would be definitely the easier way yo to do...but we have legacy code that is hooked into the lex/yacc structure.

          Comment

          • shankarke
            New Member
            • Jan 2008
            • 1

            #6
            Originally posted by rajatkmitra
            Hi Folks,
            I am writing a simple parser to parse SPICE code ( Spice is a transistor level simulator ). The spice code looks like this -
            =============== =========
            R10 2 3 0.5e-3
            C11 4 6 0.2e-6
            L13 4 4 0.1e-9
            =============== ==========
            As you can see each line has 4 items -
            a character string followed by an integer then another integer and finally a floating point number.
            I need to return the appropriate token on a per line basis.

            I am attaching a tar ball called foo.zip that has the following files -
            1. netlist ( the source file to be parsed )
            2. spice.lex ( the lexer code )
            3. spice.y ( the grammar )
            4. Makefile

            To build the parser, simple type make ( on linux ). Then to run -
            %parser < netlist

            In the .y file I have the grammar rule where I expect $1 to return the character string, $2 to return the first integer, $3 to return the next integer value and finally $4 to return the floating point value on a per line basis. Mos of this works correctly, except $1 returns the entire line instead of the character string. Can anyone tell what's wrong in the token generator ( lex file ) or the grammar description ( .y file ) ?? Any help will be highly appreciated.

            Thanks,

            Rajat Mitra
            you would need to do a strdup(yytext) in the code to get it working

            Comment

            • rajatkmitra
              New Member
              • Jun 2010
              • 5

              #7
              Originally posted by shankarke
              you would need to do a strdup(yytext) in the code to get it working
              That most definitely worked !!! Thanks a ton...

              -Rajat

              Comment

              Working...