System software PROGRAMS

download System software PROGRAMS

of 31

Transcript of System software PROGRAMS

  • 7/29/2019 System software PROGRAMS

    1/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 1

    PART-ALEX PROGRAMS

    1a). PROGRAM TO COUNT THE NUMBER OF CHARACTERS, WORDS, SPACES

    AND LINES IN A GIVEN INPUT FILE.

    %{int ch=0,wo=0,bl=0,li=0;%}

    word [^ \t \n]+blank [ ]col \n%%

    {word} {wo++;ch+=yyleng;}{blank} {bl++;ch++;}

    {col} {li++;ch++;}. {ch++;}%%

    int main(int argc,char *argv[]){if (argc!=2){

    printf("Enter the filename\n");exit(0);}yyin=fopen(argv[1],"r");

    yylex();printf("\nChar count:%d\n Word count:%d\n Blank count:%d\n Linecount:%d\n",ch,wo,bl,li);}

    OUTPUT:

    $vi ab.txtWelcome to jssateBangalore

    System software

    $vi 1a.l$lex 1a.l$cc lex.yy.c ll$./a.out k.txt

    Char count:45Word count:6Blank count:3Line count:4

  • 7/29/2019 System software PROGRAMS

    2/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 2

    1b). PROGRAM TO COUNT THE NUMBER OF COMMENT LINES IN A GIVEN CPROGRAM. ALSO ELIMINATE THEM AND COPY THE RESULTING PROGRAMINTO SEPARATE FILE.

    %{#include int comnt=0,mf=0,b=0,valid=0;%}

    m "main()"bs "{"be "}"%s dfn%%

    [/][/].* {comnt++;fprintf(yyout," ");}

    "/*" {BEGIN(dfn);}"*/" {BEGIN(0);comnt++;fprintf(yyout, " ");}\n ;. ;{m} {mf++;fprintf(yyout,"main()");}{bs} {b++;valid=0;fprintf(yyout,"{");}{be} {b--;valid=1;fprintf(yyout,"%s",yytext);}%%

    int main(int argc,char *argv[]){if (argc!=3)

    {printf("enter the source and destination files\n");exit(0);}yyin=fopen(argv[1],"r");yyout=fopen(argv[2],"w");yylex();if (mf==1){if (!b && valid){

    printf("\n Valid no. of comments->%d\n",comnt);

    }elseprintf("invalid");}else

    printf("No single main found");}

    OUTPUT:

    $vi 1b.l$lex 1b.l

    $cc lex.yy.c ll

  • 7/29/2019 System software PROGRAMS

    3/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 3

    $vi input.c

    /*Program to display text*/#includevoid main(){clrscr(); //clear output terminal

    printf(system software lab); //display text}

    $./a.out input.c output.c

    Valid no. of comments->3

    $cat output.c

    #includevoid main(){clrscr();

    printf(system software lab);}

  • 7/29/2019 System software PROGRAMS

    4/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 4

    2a). PROGRAM TO RECOGNIZE A VALID ARITHMETIC EXPRESSION AND TORECOGNIZE THE IDENTIFIERS AND OPERATORS PRESENT. PRINT THEMSEPATELY.

    %{

    int a=0,s=0,m=0,d=0;int count=0,valid=1,br=0;%}id[a-zA-Z][a-zA-Z0-9]*digits[0-9]+%%

    {id} {count++; printf("\n%s is an identifier\n",yytext);}{digits} {count++;}[+] {a++;count--; if(count!=0) valid=0;}[-] {s++;count--; if(count!=0) valid=0;}[*] {m++;count--; if(count!=0) valid=0;}

    [/] {d++;count--; if(count!=0) valid=0;}[(] {if(count==0)br++; else valid=0;}[)] {if(count==1)br--; else valid=0;}[\n] {return 0;}%%

    int main(){

    printf("\nEnter the arithmetic expression\n");yylex();if (valid==0 || count!=1 || br!=0){

    printf("\nInvalid expression\n");return 0;}else{

    printf("\nValid expression\n");printf("\nNo. of '+' operators are->%d\n",a);printf("\nNo. of '-' operators are->%d\n",s);printf("\nNo. of '*' operators are->%d\n",m);printf("\nNo. of '/' operators are->%d\n",d);}}

    OUTPUT:

    $vi 2a.l$lex 2a.l$cc lex.yy.c ll$./a.out

    Enter the arithmetic expressionb-c*de/g+a+f

    b is an identifier

    c is an identifier

  • 7/29/2019 System software PROGRAMS

    5/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 5

    de is an identifierg is an identifiera is an identifierf is an identifier

    Valid expression

    No. of '+' operators are->2No. of '-' operators are->1No. of '*' operators are->1No. of '/' operators are->1

    2b). PROGRAM TO RECOGNIZE WHETHER A GIVEN SENTENCE IS SIMPLE ORCOMPOUND.

    {int flag=0;%}%%

    " "(([aA][nN][dD])|([bB][uU][tT])|([oO][rR]))" " {flag=1;}. { }\n {return 0;}%%

    main(){

    printf("enter the sentence\n");yylex();if(flag)

    printf("\nCompound sentence\n");else

    printf("\nSimple sentence\n");}

    OUTPUT:

    $vi 2b.l$lex 2b.l

    $cc lex.yy.c ll$./a.out

    enter the sentencedo or dieCompound sentence

    $./a.outenter the sentenceorder

    Simple sentence

  • 7/29/2019 System software PROGRAMS

    6/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 6

    3). PROGRAM TO RECOGNIZE AND COUNT THE NUMBER OF IDENTIFIERS IN AGIVEN INPUT FILE.

    %{#includeint count=0;%}letter [a-zA-Z]digit [0-9]id {letter}({letter}|{digit})*%%

    [ \t\n]+ { }("int")|("float")|("char")|("double")|("printf")|("scanf"){printf("\n%s is a keyword\n",yytext);}{id} {printf("%20s is an identifier\n",yytext); count++;}

    . { }%%

    main(){FILE *fp;char file[10];

    printf("\nEnter the file name: ");scanf("%s",file);yyin=fopen(file,"r");yylex();

    printf("\nNumber of identifiers=%d\n",count);

    return 0;}

    OUTPUT:$vi ab.c

    void main(){clrscr();

    printf(system software lab);}

    $cc lex.yy.c ll$./a.out

    Enter the file name: ab.cvoid is an identifier

    main is an identifierclrscr is an identifier

    printf is a keywordsystem is an identifiersoftware is an identifierlab is an identifier

    Number of identifiers=6

  • 7/29/2019 System software PROGRAMS

    7/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 7

    YACC PROGRAMS

    4a). PROGRAM TO RECOGNIZE A VALID ARITHMETIC EXPRESSION THATUSES OPERATORS +,-,* AND /

    4a.l

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

    [0-9]+ {return NUM;}[a-zA-Z][a-zA-Z0-9]* {return ID;}[ \t\n] {return 0;}. {return yytext[0];}%%

    4a.y

    %{#include%}%token NUM ID%left '+' '-'%left '/' '*'%%

    E:E'+'E|E'-'E|E'*'E|E'/'E|'(' E ')'|'-' ID|'-' NUM|ID|NUM

    ;%%yyerror(){

    printf("\nInvalid expression\n");exit(0);}

    main(){

    printf("enter the expression\n");

    yyparse();

  • 7/29/2019 System software PROGRAMS

    8/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 8

    printf("expression is valid\n");return 0;}

    OUTPUT:

    $vi 4a.l$vi 4a.y$lex 4a.l$yacc d 4a.y$cc y.tab.c lex.yy.c ll$./a.out

    enter the expressiond+c*e/f-w

    expression is valid

    $./a.outenter the expression44-3*3+3expression is valid

  • 7/29/2019 System software PROGRAMS

    9/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 9

    4b). PROGRAM TO RECOGNIZE VALID EXPRESSION WHICH STARTS WITHA LETTER FOLLOWED BY ANY NUMBER OF LETTERS OR DIGITS.

    4b.l

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

    [0-9] {return DIGIT;}[a-zA-Z] {return ALPHA;}[ \t\n] {return 0;}. {return yytext[0];}%%

    4b.y

    %{#include#include%}%token DIGIT ALPHA%%

    var:ALPHA|var ALNUMALNUM:DIGIT|ALPHA%%

    yyerror(){

    printf("\nInvalid variable!\n");exit(0);}

    main()

    {printf("enter the variable\n");yyparse();

    printf("This is valid variable\n");return 0;}

  • 7/29/2019 System software PROGRAMS

    10/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 10

    OUTPUT:

    $vi 4b.l$vi 4b.y$lex 4b.l

    $yacc d 4b.y$cc y.tab.c lex.yy.c ll$./a.out

    enter the variablesd13

    This is valid variable

    $./a.out

    enter the variable56inputs

    Invalid variable!

  • 7/29/2019 System software PROGRAMS

    11/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 11

    5a). PROGRAM TO EVALUATE AN ARITHMETIC EXPRESSION INVOLVINGOPERATORS +,-,* AND /.

    5a.l%{

    #include "y.tab.h"extern int yylval;%}%%

    [0-9]+ {yylval=atoi(yytext); return NUM;}[\n] {return 0;}. {return yytext[0];}%%

    5a.y%{#include%}%token NUM%left '+''-'%left '*''/'%left unaryminus%%

    stmt:exp {printf("\nValid expression\n Value=%d\n",$1);}

    ;exp:exp'+'exp {$$=$1+$3;}| exp'-'exp {$$=$1-$3;}| exp'*'exp {$$=$1*$3;}| exp'/'exp {if($3==0)yyerror("divide by zero\n");else$$=$1/$3; }|'(' exp ')' {$$=$2;}| NUM| '-'exp %prec unaryminus {$$=-$2;}

    ;%%

    int main(){

    printf("enter the expression\n");yyparse();return 0;}int yyerror(char *msg){

  • 7/29/2019 System software PROGRAMS

    12/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 12

    printf("%s\n",msg);printf("Invalid expr\n");exit(0);}

    OUTPUT:

    $vi 5a.l$vi 5a.y$lex 5a.l$yacc d 5a.y$cc y.tab.c lex.yy.c ll$./a.out

    enter the expression-10

    Valid expressionValue=-10

    $./a.out

    enter the expression23-3

    Valid expression

    Value=20

  • 7/29/2019 System software PROGRAMS

    13/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 13

    5b). PROGRAM TO RECOGNIZE STRINGS aaab, abbb, ab and a USINGTHE GRAMMAR (anbn, n>=0)

    5b.l%{

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

    [a] {return A;}[b] {return B;}[ \t\n] {return 0;}. {return yytext[0];}%%

    5b.y%{#include%}%token A B%%

    S:A S|S1;S1:S1 B

    |;%%

    yyerror(){

    printf("\ninvalid expression\n");exit(0);}

    int main()

    {printf("enter the expression\n");yyparse();

    printf("valid expression\n");return (0);}

  • 7/29/2019 System software PROGRAMS

    14/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 14

    OUTPUT:

    $vi 5b.l$vi 5b.y$lex 5b.l

    $yacc d 5b.y$cc y.tab.c lex.yy.c ll$./a.out

    enter the expressionabbb

    valid expression

    $./a.out

    enter the expressionab

    valid expression

    $./a.out

    enter the expressionbaaa

    invalid expression

    enter the expressionbba

    invalid expression

  • 7/29/2019 System software PROGRAMS

    15/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 15

    6). PROGRAM TO RECOGNIZE THE GRAMMAR (anb, n>=10).

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

    %}%%

    [a] {return A;}[b] {return B;}[ \t\n] {return 0;}. {return yytext[0];}%%

    6.y

    %{#include%}%token A B%%

    S:A A A A A A A A A A S1 BS1:A S1|;%%

    yyerror(){

    printf("\ninvalid expression\n");exit(0);}

    int main(){

    printf("Enter the expression such that min 10 a's followedwith 1 b\n");

    yyparse();printf("valid expression\n");return 0;}

  • 7/29/2019 System software PROGRAMS

    16/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 16

    OUTPUT:

    $vi 6.l$vi 6.y$lex 6.l

    $yacc d 6.y$cc y.tab.c lex.yy.c ll$./a.out

    Enter the expression such that min 10 a's followed with 1 baaaaaaaaaaaaaaaaaab

    valid expression

    $./a.out

    Enter the expression such that min 10 a's followed with 1 baaaaaaaaaabb

    invalid expression

  • 7/29/2019 System software PROGRAMS

    17/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 17

    PART-BUNIX PROGRAMS

    1a). NON-RECURSIVE SHELL SCRIPT THAT ACCEPTS ANY NO. OF

    ARGUMENTS AND PRINTS THEM IN THE REVERSE ORDER.

    #!/bin/shi=$#if [ $i -eq 0 ]thenecho "no arguments"elseecho "the reverse arguments are:"

    while [ $i -ne 0 ]do

    eval echo \$$ii=`expr $i - 1`donefi

    OUTPUT:

    $ sh s1.sh a b cthe reverse arguments are:c

    b

    a

    $ sh s1.sh "e r q" "z y x"the reverse arguments are:z y xe r q

  • 7/29/2019 System software PROGRAMS

    18/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 18

    1b). C PROG THAT CREATES A CHILD PROCESS TO READ COMMANDSFROM THE STANDARD INPUT AND EXECUTE THEM.

    #include#include

    int main(){int pid;char cmd[20];

    printf("\t\t SHELL SIMULATION\n");pid=fork();if(!pid){while(strcmp(cmd,"exit")){printf("Enter the command\n");

    gets(cmd);system(cmd);}

    printf("\nChild process exits\n");}else{

    wait();printf("Parent process exits\n");}

    OUTPUT:$vi 1b.c$cc 1b.c$./a.out

    SHELL SIMULATIONEnter the commandcal

    April 2012Su Mo Tu We Th Fr Sa1 2 3 4 5 6 7

    8 9 10 11 12 13 1415 16 17 18 19 20 2122 23 24 25 26 27 2829 30

    Enter the commanddateFri Apr 2 10:11:10 IST 2012

    Enter the commandls *.sh

  • 7/29/2019 System software PROGRAMS

    19/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 19

    3aa.sh z4a.sh q1.sh a2.sh ga.shEnter the commandexit

    Child process exits

    Parent process exits

    2a). SHELL SCRIPT THAT ACCEPTS 2 FILES AS ARGUMENTS CHOOSE IFTHE PERMISSIONS FOR THESE FILES ARE IDENTICAL AND IF THE PERMARE IDENTICAL, OUTPUTS THE COMMON PERM, OTHERWISE OUTPUTSEACH FILE NAME FOLLOWED BY ITS PERMISSION.

    #!/bin/shif [ $# -eq 2 ]then

    c1=`ls -l $1 | cut -c 1-10`c2=`ls -l $2 | cut -c 1-10`if [ $c1 = $c2 ]thenecho "permissions are same"echo "permission of both $c1"elseecho "permissions are different"echo "permission of $1 is $c1"echo "permission of $2 is $c2"fi

    elseecho "invalid no of arguments"fi

    OUTPUT:

    $ sh ds2.sh a3.sh s4.sh

    permissions are samepermission of both -rwxrw-rw-

    $ sh ds2.sh a.out a3.sh

    permissions are differentpermission of a.out is -rwxr-xr-xpermission of a3.sh is -rwxrw-rw-

    $ sh ds2.sh abc.txt xz.txt

    permissions are differentpermission of abc.txt is -rw-rw-r--permission of xz.txt is -rw-rw-r--

  • 7/29/2019 System software PROGRAMS

    20/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 20

    2b). C PROGRAM TO CREATE A FILE WITH 16 BYTES OF ARBITRARY DATAFROM THE BEGINNING & ANOTHER 16 BYTES OF ARBITRARY DATAFROM AN OFFSET OF 48. DISPLAY THE FILE CONTENTS TO DEMOSTRATEHOW THE HOLE IN FILE IS HANDLED.

    #include#include#includeint main(){char buf[20],mes[]="write into a file\n";int fd,n;fd=open("B2bip.txt",O_RDWR|O_TRUNC);if(fd

  • 7/29/2019 System software PROGRAMS

    21/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 21

    $ od -c B2bip.txt0000000 s y s t e m s o f t w a r e s s0000020 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0*0000100 w e l c o m e t o j s s a t e b0000120

    3a). SHELL FUNCTION THAT TAKES A VALID DIRECTORY NAMES AS ANARGUMENT AND RECURSIVELY DESCENDS ALL THE SUBDIRECTORIES,FINDS THE MAXIMUM LENGTH OF ANY FILE IN THAT HIERARCHY AND

    WRITES THIS MAXIMUM VALUE TO THE STANDARD O/P.

    #!/bin/shecho -e "\nEnter a directory name"read dirif [ ! -d $dir ]thenecho "Invalid directory"exitfilarge=0

    for file in `find $dir -type f`dosize=`stat -c %s $file`echo "size of $file is $size"if [ $size -gt $large ]thenlarge=$sizefidoneecho -e "\nMax value is $large"

    OUTPUT:

    $ sh 3a.sh

    Enter a directory namexysize of xy/qss.c is 453size of xy/wq/awq/w/prog3.c is 7751

    Max value is 7751

  • 7/29/2019 System software PROGRAMS

    22/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 22

    3b). C PROG THAT ACCEPTS VALID FILE NAMES AS COMMAND LINEARGUMENTS AND FOR EACH OF THE ARGUMENTS, PRINTS THE TYPE OFTHE FILE.

    #include

    #include#include#include#includeint main(int argc,char *argv[]){if (argc==1)

    printf("usage:./a.out \n");else{struct stat buf;

    int i;for(i=1;i

  • 7/29/2019 System software PROGRAMS

    23/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 23

    OUTPUT:

    $ mkdir p3a //TO CREATE DIRECTORY$ mkfifo p3ew //TO CREATE FIFO FILE$ cat>p3t.txt //TO CREATE A REGULAR FILE

    $ ln -s acd p3ss //TO CREATE SYMBOLIC LINK FILE$ mknod p3ut b 0 1 //TO CREATE BLOCK SPECIAL DEVICE FILE$ mknod p3y c 0 1 //TO CREATE CHAR SPECIAL DEVICE FILE$ ./a.out p3*

    p3a: Directory filep3ew: FIFO named pipe filep3t.txt: Regular filep3ss: Symbolic filep3ut: Block special file device filep3y: Character special file device file

  • 7/29/2019 System software PROGRAMS

    24/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 24

    4a). Shell script that accepts file names specified as arguments and creates a shellscript that contains this file as well as the code to recreate these files. Thus if thescript generated by your script is executed, it would recreate the original files.

    #!/bin/sh

    echo "# to bundle,sh this file"for i in $*doecho "echo $i 1 > $2"echo "cat>$i

  • 7/29/2019 System software PROGRAMS

    25/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 25

    4b). C program to do the following: using fork() create a child process. The childprocess prints its own process-id and id of its parent and then exits. The parentprocess waits for its child to finish and prints its own process-id and the id of itschild process and then exits.

    #includeint main(){

    int pid=fork();if(pid==0){

    printf("\nCHILD PROCESS\n");printf("Process Id=%d\n",getpid());printf("Parent Process Id=%d\n",getppid());

    }else

    {wait(0);printf("\nPARENT PROCESS\n");printf("Process Id=%d\n",getpid());printf("Child Process Id=%d\n",pid);

    }return 0;

    }

    OUTPUT:

    $ ./a.out

    CHILD PROCESSProcess Id=1301Parent Process Id=1302

    PARENT PROCESSProcess Id=1302Child Process Id=1301

    $ ./a.out

    CHILD PROCESSProcess Id=345Parent Process Id=346

    PARENT PROCESSProcess Id=346Child Process Id=345

  • 7/29/2019 System software PROGRAMS

    26/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 26

    COMPILER DESIGN

    5). C PROGRAM TO IMPLEMENT THE SYNTAX-DIRECTED DEFINITION OFIF E THEN S1 AND IF E THEN S1 ELSE S2.

    /* Input to the program is assumed to be syntacticallycorrect. The expression of if statement, for true condition

    and statement for false condition are enclosed in parenthesis*/

    #include #include #include

    int parsecondition(char[],int,char*,int);

    void gen(char [],char [],char[],int);int main(){int counter = 0,stlen =0,elseflag=0;char stmt[60]; // contains the input statementchar strB[54]; // holds the expression for 'if' conditionchar strS1[50]; // holds the statement for true conditionchar strS2[45]; // holds the statement for false condition

    printf("Format of if statement \n Example...\n");printf("if (a

  • 7/29/2019 System software PROGRAMS

    27/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 27

    elseflag = 1;printf("\n Parsing the input statement....");gen(strB,strS1,strS2,elseflag);return 0;

    }

    return 0;}

    /* Function : parseconditionDescription : This function parses the statementfrom the given index to get the statement enclosedin ()Input : Statement, index to begin search, stringto store the condition, total string lengthOutput : Returns 0 on failure, Non zero countervalue on success

    */

    int parsecondition(char input[],int cntr,char*dest,int totallen)

    {int index = 0,pos = 0;

    while(input[cntr]!= '(' && cntr = totallen)return 0;

    index = cntr;

    while (input[cntr]!=')')cntr++;

    if(cntr >= totallen)return 0;

    while(index

  • 7/29/2019 System software PROGRAMS

    28/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 28

    printf("\n\tIf %s goto %d",B,Bt);printf("\n\tgoto %d",Bf);printf("\n%d: ",Bt);printf("%s",S1);if(!elsepart)

    printf("\n%d: ",Bf);else{printf("\n\tgoto %d",Sn);printf("\n%d: %s",Bf,S2);printf("\n%d:",Sn);}

    }

    OUTPUT:

    Format of if statementExample ...if (a

  • 7/29/2019 System software PROGRAMS

    29/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 29

    6). Write a yacc program that accepts a regular expression as input and produce itsparse tree as output.

    %{/**

    Yacc program to recognise a regular expressionand produce a parse tree as output

    */#include #include #include #include

    /* To store the productions */#define MAX 100

    int getREindex ( const char* );

    signed char productions[MAX][MAX];int count = 0 , i , j;char temp[MAX + MAX] , temp2[MAX + MAX];

    %}

    %token ALPHABET

    %left '|'%left '.'

    %nonassoc '*' '+'

    %%S : re '\n'{printf ( "This is the rightmost derivation\n" );for ( i = count - 1 ; i >= 0 ; --i ) {

    if ( i == count - 1 ) {printf ( "\nre => " );strcpy ( temp , productions[i] );

    printf ( "%s" , productions[i] );

    }else

    {printf ( "\n => " );j = getREindex ( temp );temp[j] = '\0';sprintf ( temp2 , "%s%s%s" , temp ,

    productions[i] , (temp + j + 2) );printf ( "%s" , temp2 );strcpy ( temp , temp2 );

    }

  • 7/29/2019 System software PROGRAMS

    30/31

    1JS10CS4 NAME

    Dept. of CSE, JSSATE 30

    }printf ( "\n" );exit ( 0 );

    }re : ALPHABET

    {temp[0] = yylval; temp[1] = '\0';strcpy ( productions[count++] , temp );

    }| '(' re ')'

    {strcpy ( productions[count++] , "(re)" );

    }| re '*'

    {strcpy ( productions[count++] , "re*" );

    }| re '+'

    {strcpy ( productions[count++] , "re+" );

    }| re '|' re

    {strcpy ( productions[count++] , "re | re" );

    }| re '.' re

    {

    strcpy ( productions[count++] , "re . re" );}

    ;%%int main ( int argc , char **argv ){/*

    Parse and output the rightmost derivation,from which we can get the parse tree

    */yyparse();

    return 0;}

    yylex(){signed char ch = getchar();yylval = ch;if ( isalpha ( ch ) )return ALPHABET;

    return ch;

  • 7/29/2019 System software PROGRAMS

    31/31

    1JS10CS4 NAME

    }

    yyerror(){fprintf(stderr , "Invalid Regular Expression!!\n");

    exit ( 1 );}

    int getREindex ( const char *str ){int i = strlen ( str ) - 1;for ( ; i >= 0 ; --i ){if ( str[i] == 'e' && str[i-1] == 'r' )return i-1;

    }

    }

    OUTPUT$vi d6.y$yacc d d6.y

    $./a.outx|y.z

    This is the rightmost derivation

    re=>re|re=>re|re.re=>re|re.z=>re|b.z=>x|y.z

    $./a.out

    a+|b*|(b.c*)This is the rightmost derivation

    re => re | re=> re | (re)=> re | (re . re)=> re | (re . re*)=> re | (re . c*)=> re | (b . c*)=> re | re | (b . c*)=> re | re* | (b . c*)=> re | b* | (b . c*)=> re+ | b* | (b . c*)=> a+ | b* | (b . c*)