Sunday 24 April 2016

Lex specification program that generates a C program which takes a string “abcd” and prints the pattern

/*Write a Lex specification program that generates a C program which takes a string
“abcd” and prints the following output.
        abcd
        abc
        ab
        a
*/

%{ #include<stdio.h>
char ch;
char i,j;
%}

%%
[a-z]* { for(i='d';i>=1;--i)
      {
           for(j='a';j<=i;++j)
           {
              printf("%c ",j);
           }
          printf("\n");
      }
}
%%

int main()
{
 yylex();
 return 0;
}

//Output of the above program
Lex specification program that generates a C program which takes a string “abcd” and prints the pattern
Lex specification program that generates a C program which takes a string “abcd” and prints the pattern

1 comment:

  1. Thanks for uploading all the practicals of System Programming

    The appropriate code for this practical question is below :

    %{
    int i,j;
    %}

    %%
    [a-z]* { printf("\n");
    for(i=0; i<yyleng;i++ )
    {
    for(j=0;j<yyleng-i;j++)
    {
    printf("%c",yytext[j]);
    }
    printf("\n");
    }
    }
    %%

    /* This program will run for all small alphabets */

    ReplyDelete