doubt in c parser coding

This is a discussion on doubt in c parser coding within the C Programming forums, part of the General Programming Boards category; hello, i have a doubt in the parser design program using flex and bison. can someone please help me find ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    3

    doubt in c parser coding

    hello,

    i have a doubt in the parser design program using flex and bison.
    can someone please help me find the error.

    my scan.l code:

    Code:
    %{
    /* a pure (reentrant) parser and scanner */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int my_yy_flex_debug;    /* my flex debug flag */
    
    extern int fileno(FILE *);      /* avoids gcc warning */
    
    static int number(char *, YYSTYPE *);
    static int string(char *, YYSTYPE *);
    
    
    static char *estrdup( const char *s)
    { 
        char *r = malloc( strlen(s) + 1);
        if( r == NULL) 
        abort(); 
        return strcpy( r, s);
    }
    
    %}
    
    D	[0-9]
    E	[Ee][+\-]?{D}+
    A     [A-Za-z_][A-Za-z_0-9]*	
    
    
    %%
    
    %{
    	yy_flex_debug = my_yy_flex_debug;
    %}
    
    
    [ \t\v\f]			{ /* ignore whitespace */ }
    
    [()=+*/\-\n]	        	{ return yytext[0]; }
    
    
    
    {D}+({E})?			{ return number(yytext, yylval);   }
    {D}*"."{D}+({E})?		{ return number(yytext, yylval);   }
    {D}+"."{D}*({E})?		{ return number(yytext, yylval);   }  
    
    '+'                             { return PLUS;                     }
    '-'                             { return MINUS;                    }
    '*'                             { return MULT;                     }
    "/"                             { return DIVI;                     }
    "reshape"                       { return RESHAPE;                  }
    ".+"                            { return DPLUS;                    }
    ".-"                            { return DMINUS;                   }
    ".*"                            { return DMULT ;                   }
    "./"                            { return DDIVI;                    }
    \"[^"]*\"		        { return string( yytext, yylval); }
    {A}({A}|{D})*		        { yylval->string = estrdup( yytext); return ID; }
    '='                               { return EQUAL;                    }
    
    abs				{ return ABS;  }
    avg				{ return AVG;  }
    
    "+/"                            { return SPLUS;                    } 
    "*/"                            { return SMULT;                    }
    "-/"                            { return SMINUS;                   } 
    "//"                            { return SDIVI;                    } 
    
    
    
    "#".*				{ /* ignore comments */            }
    
    .				{ return yytext[0]; /* bad token */ }
    
    %%
    
    
    void (*jj_junk)(int,char *,yyscan_t) = yyunput; /* avoids gcc warning */
    
    int number( char *s, YYSTYPE *y)
    {
      if( sscanf( s, "%lf", &(y->number) ) != 1 ) return 0;
      return NUMBER;
    }
    
    int string( char *s, YYSTYPE *y)
    {
      y->string = estrdup( s);
      return STRING;
    }
    
    extern int yyparse( yyscan_t scanner);
    
    
    int main( int argc, char *argv[])
    {
      yyscan_t scanner;
      
    #if YYDEBUG
      yydebug = argc > 1;
    #endif
      my_yy_flex_debug = argc > 1;
      
      yylex_init( &scanner);
    
      yyparse( scanner);
     
      yylex_destroy( scanner);
      
      return 0;
    }
    
    /* called at EOF */
    
    int yywrap( yyscan_t scanner)
    {
      return 1;
    }
      
    /* called for parser syntax error */
    parse.y code:

    Code:
    %{
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    typedef void *yyscan_t;
    
    %}
    
    %union{
      int count;
      double number;
      char *string;
    }
    
    %token <number> NUMBER
    %token <string> ID
    
    %token PLUS MINUS DIVI MULT SPLUS SMINUS SMULT SDIVI DPLUS DMINUS DDIVI DMULT EQUAL ABS AVG RESHAPE
    
    %type <count> elist
    
    %{
    %}
    
    %%
    
    expr  : pri 
          | MINUS expr	       { printf( "unary - \n");           }
          | expr MULT expr         { printf( "binary  * \n");         }
          | expr DIVI expr         { printf( "binary  / \n");         }
          | expr MINUS expr        { printf( "binary  - \n");         }
    	| expr PLUS expr         { printf( "binary  + \n");         }
          | expr DPLUS expr        { printf( "binary  .+ \n");        } 
          | expr DMINUS expr       { printf( "binary  .- \n");        }
          | expr DMULT expr        { printf( "binary  .* \n");        }         
          | expr DDIVI expr        { printf( "binary  ./ \n");        } 
          | expr SPLUS expr        { printf( "unary  +/ \n");         } 
          | expr SMINUS expr       { printf( "unary  -/ \n");         }         
          | expr SMULT expr        { printf( "unary  */ \n");         } 
          | expr SDIVI expr        { printf( "umary  // \n");         } 
           |expr RESHAPE expr          { printf( "binary reshape \n");    }
          | ABS expr               { printf( "unary abs\n");          } 
          | AVG expr               { printf( "unary avg\n");          } 
          | ID EQUAL expr        { printf( "binary  = \n");       } 	
           ;
    
    
    elist	: expr			{ $$ = 1; }
    	| elist ',' expr	      { $$ += 1; }
    	;
    
    pri
    	: NUMBER		{ printf( "push: %f\n", $1);}
    	| '(' expr ')'
    	| '{' 			{ printf( "list start:\n"); }
                elist '}'		{ printf( "list end \n"); }
    	| ID
    	  {
    	    printf( "push: %s\n", $1);
              }
    	  ;
    
    %%
    errors:
    gcc -ansi -pedantic -Wall -DDEBUG=1 -c -o scan.o scan.c
    scan.l:12: parse error before "YYSTYPE"
    scan.l:13: parse error before "YYSTYPE"
    scan.l: In function `yylex':
    scan.l:44: `yylval' undeclared (first use in this function)
    scan.l:44: (Each undeclared identifier is reported only once
    scan.l:44: for each function it appears in.)
    scan.l:48: `PLUS' undeclared (first use in this function)
    scan.l:49: `MINUS' undeclared (first use in this function)
    scan.l:50: `MULT' undeclared (first use in this function)
    scan.l:51: `DIVI' undeclared (first use in this function)
    scan.l:52: `RESHAPE' undeclared (first use in this function)
    scan.l:53: `DPLUS' undeclared (first use in this function)
    scan.l:54: `DMINUS' undeclared (first use in this function)
    scan.l:55: `DMULT' undeclared (first use in this function)
    scan.l:56: `DDIVI' undeclared (first use in this function)
    scan.l:58: `ID' undeclared (first use in this function)
    scan.l:59: `EQUAL' undeclared (first use in this function)
    scan.l:61: `ABS' undeclared (first use in this function)
    scan.l:62: `AVG' undeclared (first use in this function)
    scan.l:64: `SPLUS' undeclared (first use in this function)
    scan.l:65: `SMULT' undeclared (first use in this function)
    scan.l:66: `SMINUS' undeclared (first use in this function)
    scan.l:67: `SDIVI' undeclared (first use in this function)
    scan.l: At top level:
    scan.l:78: parse error before "yyscan_t"
    scan.l:80: parse error before "YYSTYPE"
    scan.l: In function `number':
    scan.l:82: `s' undeclared (first use in this function)
    scan.l:82: `y' undeclared (first use in this function)
    scan.l:83: `NUMBER' undeclared (first use in this function)
    scan.l: At top level:
    scan.l:86: parse error before "YYSTYPE"
    scan.l: In function `string':
    scan.l:88: `y' undeclared (first use in this function)
    scan.l:88: `s' undeclared (first use in this function)
    scan.l:89: `STRING' undeclared (first use in this function)
    scan.l: At top level:
    scan.l:92: parse error before "scanner"
    scan.l: In function `main':
    scan.l:97: `yyscan_t' undeclared (first use in this function)
    scan.l:97: parse error before "scanner"
    scan.l:104: warning: implicit declaration of function `yylex_init'
    scan.l:104: `scanner' undeclared (first use in this function)
    scan.l:108: warning: implicit declaration of function `yylex_destroy'
    scan.l: At top level:
    scan.l:115: parse error before "scanner"


    can some someone please help me out with this one.

    Thanx a lot

    Regards,
    Akshara

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    When you run bison, give the -d flag. This will produce a header file called parse.tab.h. Include this file in your scan.l at the beginning:

    Code:
    %{
    /* a pure (reentrant) parser and scanner */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "parse.tab.h"
    That should fix a lot of those errors, maybe not all of them though.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    i have a doubt
    No you don't. You can't have a doubt, you can only be in doubt. "doubt" is not a thing, it is a state.
    What you can have is a question! Please stop misusing English, and tell the person that taught you incorrectly that they are wrong.
    Now go ahead and ask a question, and use a question mark (?) too!

    No I'm not having a bad day, I only complain about this in about 1 out of 10 cases and you just happened to be zero, mod ten.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    i am sorry to refer my extended question as a doubt.i will make sure to use the correct terms from future.

    i dint understand the reply.i run this program in secure shell environment where i type "make" to run my program.where should i give the -d flag?

    Regards,
    Akshara

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Examine the Makefile that make is using. There will probably be a line like
    Code:
    BISON = bison
    If there is, make that line look like this:
    Code:
    BISON = bison -d
    If you can't find that original line, insert the second line near the beginning of the file.

    Alternatively, you could type this:
    Code:
    make "BISON=bison -d"
    I think that's the right syntax.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  2. Parser Help
    By Barnzey in forum C++ Programming
    Replies: 10
    Last Post: 10-26-2005, 12:10 PM
  3. Problem with a file parser.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 03-17-2005, 08:54 AM
  4. Parser - need help!
    By thelma in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 08:06 PM
  5. Coding Contest....
    By Koshare in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-14-2001, 04:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21