Lex and yacc program

This is a discussion on Lex and yacc program within the Linux Programming forums, part of the Platform Specific Boards category; Hi, i have few questions in the program which is given bellow , there are two file first one is ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    1

    Lex and yacc program

    Hi, i have few questions in the program which is given bellow ,
    there are two file first one is lex and second is yacc file.
    Here is the OUTPUT:
    student@localhoststudent]$ yacc -d cal2.y
    [student@localhoststudent]$ lex cal1.l
    [student@localhoststudent]$ cc lex.yy.c y.tab.c -ll -lm
    [student@localhoststudent]$ ./a.out
    3+2
    5.000000
    Code:
    //Lex program starts here
    %{
    /* LEX FILE*/
    /*TO RECOGNIZE INFIX EXPRESSION AND EVALUATE IT.*/
    #include"y.tab.h"
    #include<stdio.h>
    void yyerror(char *);
    %}
    
    
    %%
    (([0-9]+)|([0-9]*\.[0-9]+))   {
        yylval.dval=atof(yytext);                
        return NUM;
    }
    
    
    [+-/*\n,~()]  {return (*yytext);}
    
    
    sin   return SIN;
    cos   return COS;
    tan   return TAN;
    sqrt  return SQRT;
    exit  exit(0);
    [ \t]  /*ignore*/
    %%
    
    
    
    
    int yywrap(void)
    {
        return 1;
    }
    
    //End of the program
    
    (([0-9]+)|([0-9]*\.[0-9]+))   {  
        yylval.dval=atof(yytext);                
        return NUM;
    }
    //What happens in abouve block.
    // "\." why this slash is given before "."  
    
    
    [+-/*\n,~()]  {return (*yytext);}
    // what this line is doing?
    
    //Yacc program stars here
    %{
    /* YACC FILE */
    /*LEX AND YACC PROGRAM*/
    /*TO RECOGNIZE INFIX EXPRESSION AND EVALUATE IT.*/
    
    
    #include<stdio.h>
    #include<math.h>
    void yyerror(char *);
    %}
    
    
    %union 
    {
        double dval;
    }
    
    
    %token <dval> NUM
    %token SIN COS TAN SQRT
    %right '~'
    %left '+' '-'
    %left '*' '/'
    %type <dval> expression
    
    
    %%
    program: program statement'\n'
        | 
        ;
    
    
    statement: 
          expression  { printf("%lf\n", $1); }
          ;
    
    
    expression:  
            NUM
            | expression '+' expression  {$$ = $1 + $3;}
            | expression '-' expression  {$$ = $1 - $3;}
            | expression '*' expression  {$$ = $1 * $3;}
            | expression '/' expression  {$$ = $1 / $3;}             
            | '~' expression  {$$ = -(1) * $2;}
            | SQRT'('expression')'  {$$ = sqrt( $3 );}
            | SIN'('expression')'   {$$ = sin ($3*3.142/180);}
            | COS'('expression')'   {$$ = cos ($3*3.142/180);}
            | TAN'('expression')'   {$$ = tan ($3*3.142/180);}
            ;
    %%
    
    
     main()
    {
        yyparse();
    }
    
    
    void yyerror(char *s)
    {
        fprintf(stderr,"%s\n",s);
    }
    
    
    //End of the program
    
    // how is the execution of following block
    %%
    program: program statement'\n'
        | 
        ;
    
    
    statement: 
          expression  { printf("%lf\n", $1); }
          ;
    
    
    expression:  
            NUM
            | expression '+' expression  {$$ = $1 + $3;}
            | expression '-' expression  {$$ = $1 - $3;}
            | expression '*' expression  {$$ = $1 * $3;}
            | expression '/' expression  {$$ = $1 / $3;}             
            | '~' expression  {$$ = -(1) * $2;}
            | SQRT'('expression')'  {$$ = sqrt( $3 );}
            | SIN'('expression')'   {$$ = sin ($3*3.142/180);}
            | COS'('expression')'   {$$ = cos ($3*3.142/180);}
            | TAN'('expression')'   {$$ = tan ($3*3.142/180);}
            ;
    %%
    Please any suggestions are highly appreciated! Thanks a lot for all your time and support!
    Last edited by Lanpan; 11-03-2011 at 09:47 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I haven't actually made use of lex or yacc before (shame), but:

    Quote Originally Posted by Lanpan View Post
    Hi, i have few questions in the program which is given bellow ,

    Code:
    (([0-9]+)|([0-9]*\.[0-9]+))   {
        yylval.dval=atof(yytext);                
        return NUM;
    }
    //What happens in abouve block.
    // "\." why this slash is given before "."
    The bold red bit is a regular expression. I presume these are fall-thrus used by the lexer in matching elements. What that will match is

    ([0-9]+)
    1 or more (+) digits

    |
    OR

    ([0-9]*\.[0-9]+)

    Zero or more (*) digits, then a literal . (\ is an escape used since . by itself has a special meaning (any single character)), then 1 or more digits.

    Ie., it is matching numbers in any of these forms:
    123
    5.6
    .777
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C struct and a bit of yacc
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 11-27-2009, 10:25 AM
  2. Anybody into lex and yacc ?
    By spank in forum Tech Board
    Replies: 8
    Last Post: 07-20-2007, 03:30 AM
  3. Yacc and Lex
    By Chronom1 in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2005, 07:03 AM
  4. Lex and Yacc
    By Mystic_Skies in forum Linux Programming
    Replies: 2
    Last Post: 12-05-2004, 03:06 PM
  5. Yacc Interpreter
    By discostu in forum C Programming
    Replies: 4
    Last Post: 06-14-2003, 10:27 PM

Tags for this Thread


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