yacc program to check the validity of the identifier

 id.lex

********************************

%{

#include "id.tab.h"

%}

%option noyywrap

%%

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

[0-9]     return DIGIT;

[ \t\n]+                { /* Ignore whitespace */ }

.                       { return yytext[0]; }

%%

id.y
*****************************

%{
#include <stdio.h>
#include <ctype.h>
void yyerror();
int valid=1;
%}

%token LETTER DIGIT

%%
input:
    | LETTER  line
    ;
line:
    LETTER line
    |DIGIT line
    |
    ;
%%

int main() {
    printf("Enter identifiers to check their validity:\n");
    yyparse();
   if (valid)
    printf("It is a valid identifier \n");
    return 0;
}

void yyerror() {
    fprintf(stderr, "Invalid Identifier:\n");
    valid=0;
}

Execution
$ bison -d id.y
$ lex id.lex
$ gcc lex.yy.c id.tab.c
$./a.out
Enter identifiers to check their validity:
dkj23
It is a valid identifier
$ ./a.out
Enter identifiers to check their validity:
12dd
Invalid Identifier:
$ ./a.out
Enter identifiers to check their validity:
_dhd23
It is a valid identifier


Comments

Popular posts from this blog

KTU Compiler Lab CSL411 - Dr Binu V P

lexical analyzer for a c program

13.Precedence and conflict resolution in yacc