Skip to content

Sushmithaa9/Ex-4-CD-Lab

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

Ex-4-LETTER-FOLLOWED-BY-ANY-NUMBER-OF-LETTERS-OR-DIGITS-USING-YACC

RECOGNITION OF A VALID VARIABLE WHICH STARTS WITH A LETTER FOLLOWED BY ANY NUMBER OF LETTERS OR DIGITS USING YACC

Date:15.09.2025

Aim:

To write a YACC 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. Write a program in the vi editor and save it with .l extension.
  3. In the lex program, write the translation rules for the keywords int, float and double and for the identifier.
  4. Write a program in the vi editor and save it with .y extension.
  5. Compile the lex program with lex compiler to produce output file as lex.yy.c. eg $ lex filename.l
  6. Compile the yacc program with YACC compiler to produce output file as y.tab.c. eg $ yacc –d arith_id.y
  7. Compile these with the C compiler as gcc lex.yy.c y.tab.c
  8. Enter a statement as input and the valid variables are identified as output.

PROGRAM

expr4.l

%{
#include "y.tab.h"
#include <string.h>
%}

%%
[a-zA-Z][a-zA-Z0-9]*    { yylval.str = strdup(yytext); return IDENTIFIER; }
\n                      { return '\n'; }
.                       { return yytext[0]; }
%%

int yywrap() {
    return 1;
}

expr4.y

%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

extern int yylex();
void yyerror(const char *msg);

%}

%union {
    char *str;
}

%token <str> IDENTIFIER

%%
start:
    IDENTIFIER '\n' {
        printf("Valid variable: %s\n", $1);
        free($1);  // clean up strdup memory
    }
    ;
%%

int main() {
    printf("Enter a variable name:\n");
    return yyparse();
}

void yyerror(const char *msg) {
    printf("Invalid variable name\n");
}

Output

image

Result

A YACC program to recognize a valid variable which starts with a letter followed by any number of letters or digits is executed successfully and the output is verified.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published