PROGRAM 4:

Program to recognize a valid variable which starts with a letter followed by any number of letters or digits.


ALGORITHM:

1. Start the program.
2. Read the input.
3. Check whether the given input is valid according to the rule for variable using yacc.
4. Print the result after the validation check
5. Stop the program.



PROGRAM:

LEX PART

%{

    #include "y.tab.h"

%}

%%

[a-zA-Z_][a-zA-Z_0-9]* return letter;

[0-9]                       return digit;

.                      return yytext[0];

\n                     return 0;

%%

int yywrap()

{

return 1;


}

YACC PART



%{

    #include<stdio.h>

    int valid=1;

%}

%token digit letter

%%

start : letter s

s :     letter s

      | digit s

      |

      ;

%%

int yyerror()

{

    printf("\n Its not a variable !\n");

    valid=0;

    return 0;

}

int main()

{

    printf("\n Enter the input: ");

    yyparse();

    if(valid)

    {

        printf("\n It is a variable !\n");

    }

}


COMPILATION

lex filename.l
yacc -d filename.y
cc lex.yy.c y.tab.c -w
./a.out

OUTPUT



Enter the input: abc

It is a variable !

Enter the input: a1

It is a variable !

Enter the input:131-a


Its not a variable !