Ex_13 & 14

Embed Size (px)

Citation preview

  • 8/11/2019 Ex_13 & 14

    1/7

    Ex.No: 13

    Implementation Of Lexical Analyser Using Lex Tool

    AIM:

    To write a lex program to implement the lexical analyzer.

    ALGORITHM:

    1. Start the program

    2. Open a file file.c in read and include the yylex() tool for input scanning.

    3. Define the alphabets and numbers.

    4. Print the preprocessor, function, keyword using yytext.lex tool.

    5. Print the relational, assignment and all the operator using yytext() tool.

    6.

    Also scan and print where the loop ends and begins.

    7. Use yywrap() to enter an error.

    8. Stop the program.

    PROGRAM:

    %{int COMMENT=0;

    %}identifier [_a-zA-Z][_a-zA-Z0-9]*%%#.* {printf("\n%s is a PREPROCESSOR DIRECTIVE",yytext);}int |float |char |while |for |do |if |

    break |continue |void |switch |case |long |struct |const |typedef |return |

  • 8/11/2019 Ex_13 & 14

    2/7

    else |goto {printf("\n\t%s is a KEYWORD",yytext);}"/*" {COMMENT=1;

    printf("\n\n\t COMMENT ENDS\n");}{identifier}\( {if(!COMMENT)

    printf("\n\nFUNCTION \n\t%s",yytext);}\{ {if(!COMMENT) printf("\n BLOCK BEGINS");}\} {if(!COMMENT) printf("\n BLOCK ENDS");}{identifier}(\[[0-9]*\])? {if(!COMMENT) printf("\n %s IDENTIFIER",yytext);}\".*\" {if(!COMMENT) printf("\n %s is a STRING",yytext);}[0-9]+ {if(!COMMENT) printf("\n %s is a NUMBER",yytext);}\)(\;)? {if(!COMMENT) PRINTF("\N\T");ECHO; printf("\n");}\( ECHO;= {if(!COMMENT) printf("\n\t%s is an ASSIGNMENT OPERATOR",yytext);}\= |\< |== |\> {if(!COMMENT) printf("\n\t%s is a RELATIONAL OPERATOR",yytext);}

    |\n%%

    int main(int argc,char **argv){if(argc > 1){FILE *file;file=fopen(argv[1],"r");if(!file){

    printf("could not open %s\n",argv[1]);exit(0);}yyin=file;}yylex();printf("\n\n");return 0;}int yywrap(){

    return 0;}

  • 8/11/2019 Ex_13 & 14

    3/7

    INPUT (area.c):

    #include#includedouble area_of_circle(double r);int main(int argc,char *argv[]){if(argc < 2)

    {printf("usage: %s radius\n",argv[0]);{exit(1);}else {double radius=atof(argv[1]);double area=area_of_circle(radius);printf("Area of circle with radius %f=%f\n",radius,area);}return 0;

    }

    RESULT:

    Thus the above the program is executed and the required output is obtained.

  • 8/11/2019 Ex_13 & 14

    4/7

    Ex.No: 14

    Implementation Of Calculation Using Lex And Yacc

    AIM:

    To write a program to implement calculator using lex and yacc.

    ALGORITHM:

    1. Start the program.

    2. Perform the calculation using both the lex and yacc.

    3. In the lex tool, if the given expression contains numbers and letters then they are displayed

    4. In the same way, the digits, letters and uminus are identified and displayed using yacc

    5. The calculation is performed and the result is displayed.

    6. Stop the program.

    PROGRAM:

    USING LEX TOOL:

    %{#include#include"y.tab.h"

    int c;extern int yylaval;%}%%"";[a-z]{c=yytext[0];yylaval=c-'a';return(LETTER);}[0-9]{c=yytext[0];yylaval=c-'0';return(DIGIT);}[a-z0-9/b]{c=yytext[0];return(c);}

  • 8/11/2019 Ex_13 & 14

    5/7

    USING YACC TOOL:%{#includeint regs[26];int base;%}%start list%token DIGIT LETTER%left'|'%left'&'%left'+''-'%left'*''/''%'%left UMINUS%%list:|list stat'\n'|list error'\n'{yyerror();};stat:expr{printf("%d\n",$1);}|LETTER'='expr{regs[$1]=$3;};expr:'('expr')'{$$=$2;}|expr'*'expr{$$=$1*$3;}|

    expr'/'expr{$$=$1/$3;}|expr'%'expr{$$=$1%$3;}|

  • 8/11/2019 Ex_13 & 14

    6/7

    expr'+'expr{$$=$1+$3;}|expr'-'expr{$$=$1-$3;}|expr'&'expr{$$=$1&$3;}|expr'|'expr{$$=$1|$3;}

    |'-'expr%prec UMINUS{$$=-$2;}|LETTER{$$=regs[$1];}|

    number;number:DIGIT{$$=$1;base=($1==0)?8:10;}|number DIGIT{$$=base*$1+$2;

    }%%main(){return(yyparse());}yyerror(s)char*s;{fprintf(stderr,"%s\n",s);

  • 8/11/2019 Ex_13 & 14

    7/7

    }yywrap(){return(1);}

    OUTPUT:

    RESULT:

    Thus the above the program is executed and the required output is obtained.