Posts

Showing posts from June, 2024

KTU Compiler Lab CSL411 - Dr Binu V P

Preamble:  This course aims to offer students hands-on experience on compiler design concepts.Students will be able to familiarize with tools such as LEX and YACC and automate different phases of a compiler. This course helps the learners to enhance the capability to design and implement a compiler. Prerequisite: A sound knowledge in C programming, Data Structures, Formal languages and Automata Theory and Compiler design. About Me Syllabus and practise questions Lex yacc tutorial by Tom Neimann How to Install  flex and Bison Introduction to lex/flex Format of lex input files Lex Regular Expressions Input Matching in Lex Actions in Lex File Start Conditions in Lex Values available to the user in action section Reading and writing files Lex/Flex  Programs     Simple lexical analyser in C     count number of lines ,words and characters     check for valid identifiers     count the identifiers     converting the substring "abc" to "ABC"          count vowels and consona

count frequency of occurrence of a word - lex program

 LEX code to count the frequency of the given word(sachin)  in an input.txt file *************************************************** %{ #include<stdio.h> #include<string.h> char word [] = "sachin"; int count = 0; %} %option noyywrap /* Rule Section */ %% [a-zA-Z]+ { if(strcmp(yytext, word)==0)                                 count++; } .      ; \n      ; %% /* code section */ int main() {                 extern FILE *yyin, *yyout;                 /* open the input file                 in read mode */                 yyin=fopen("input.txt", "r");                 yylex();                 printf("No of occurance of the word %s=%d\n",word, count); } Execution the input.txt file is $ cat input.txt this is a test file to check frequncy of the word sachin it will count how many times word sachin occures in the file and count the word sachin $ flex freq.lex $ gcc lex.yy.c $ ./a.out No of occurance of the word sachin=3

lexical analyzer for a c program

 This program identifies tokens in a C program ignoring white spaces and comments.The input C program is given as argument ******************************* %{ #include <stdio.h> %} %option noyywrap /* Define regular expressions for different token types */ DIGIT      [0-9] LETTER     [a-zA-Z] IDENTIFIER {LETTER}({LETTER}|{DIGIT})* NUMBER     {DIGIT}+ WHITESPACE [ \t\n]+ COMMENT1    "//".* COMMENT2  "/*"([^*]|\*+[^/*])*\*+"/" %% {WHITESPACE}        { /* Ignore whitespace */ } {COMMENT1}           { /* Ignore comments */ } {COMMENT2}           { /* Ignore comments */ } "int"               { printf("TOKEN_INT\n"); } "return"            { printf("TOKEN_RETURN\n"); } "if"                { printf("TOKEN_IF\n"); } "else"              { printf("TOKEN_ELSE\n"); } "while"             { printf("TOKEN_WHILE\n"); } "for"               { printf("TOKEN_

count vowels and consonants lex program

 %{ #include <stdio.h> int vowel_count = 0; int consonant_count = 0; %} %option noyywrap %% [aAeEiIoOuU]      { vowel_count++; } [A-Za-z]          { consonant_count++; } [^a-zA-Z]         { /* ignore non-alphabetic characters */ } %% int main() {     yylex();    printf("Number of vowels: %d\n", vowel_count);    printf("Number of consonants: %d\n", consonant_count);    return 0; } Execution $flex vow.lex $ gcc lex.yy.c $ ./a.out this is test Number of vowels: 3 Number of consonants: 7

Convert the substring abc to ABC lex program

The program will read a line of text and changes all occurrence of the substring "abc" with "ABC" *************************************** %option noyywrap %% abc    { printf("ABC"); } .|\n   { printf("%s", yytext); } %% int main() {     yylex();     return 0; } Execution $flex abc.lex $ gcc lex.yy.c $ ./a.out this is a testabc djd abc djd this is a testABC djd ABC djd djdjd ajdjd abc djdabc abdf abcj abdc abc djdjd ajdjd ABC djdABC abdf ABCj abdc ABC

Simple Lexical Analyser in C

#include <stdio.h> #include <ctype.h> #include <string.h> #include <stdlib.h> #define MAX_TOKEN_LENGTH 100 // Token types typedef enum {     TOKEN_KEYWORD,     TOKEN_IDENTIFIER,     TOKEN_NUMBER,     TOKEN_STRING_LITERAL,     TOKEN_CHAR_LITERAL,     TOKEN_OPERATOR,     TOKEN_PUNCTUATION,     TOKEN_UNKNOWN,     TOKEN_END } TokenType; // Token structure typedef struct {     TokenType type;     char value[MAX_TOKEN_LENGTH]; } Token; // Keywords in C const char *keywords[] = {     "auto", "break", "case", "char", "const", "continue", "default",     "do", "double", "else", "enum", "extern", "float", "for", "goto",     "if", "int", "long", "register", "return", "short", "signed",     "sizeof", "static", "struct", "swi

counting valid identifiers lex program

The program will count and print the  number of valid identifiers.Users can input identifiers line by line and press ctrl-D for terminating the input. ********************************************** digit  [0-9] letter [A-Za-z_] %{     int count=0; %} %option noyywrap %%     /* match identifier */ {letter}({letter}|{digit})*  count++; .*    ; \n   ; %% int main(void) {     yylex();     printf("number of identifiers = %d\n", count);     return 0; } Execution $ flex id.lex $ gcc lex.yy.c $ ./a.out hi this 2dkd _djd d34 dkd-dfjf number of identifiers = 4

lex program to implement a simple calculator.

 /*lex program to implement  a simple calculator.*/ %{ int op = 0,i; float a, b; %} %option noyywrap dig [0-9]+|([0-9]*)"."([0-9]+) add "+" sub "-" mul "*" div "/" pow "^" ln \n %% {dig} {compute();} {add} {op=1;} {sub} {op=2;} {mul} {op=3;} {div} {op=4;} {pow} {op=5;} {ln} {printf("\n The Answer :%f\n\n",a);} %% compute() { if(op==0) /* atof() is used to convert  the ASCII input to float */ a=atof(yytext); else { b=atof(yytext); switch(op) { case 1:a=a+b;         break; case 2:a=a-b;         break; case 3:a=a*b;         break; case 4:a=a/b;         break; case 5:for(i=a;b>1;b--)         a=a*i;         break; } op=0; } } main() { yylex(); } Execution $flex cal.lex $ gcc lex.yy.c $ ./a.out 2+3 The Answer :5.000000 2.2/1.2  The Answer :1.833333 2.54-1.23  The Answer :1.310000 2/3  The Answer :0.666667

Lex program to search for a word in a file

Lex program to search a word in a file - This program will search a word in the file input.txt and display the message FOUND or NOT FOUND ************************************* %{ #include<string.h> void search(char *); %} %option noyywrap /* Rule Section */ %% [a-zA-Z]+ search(yytext); %% int main() {         // The function that starts the analysis         yylex();         return 0; } void search(char *str) {/* fp as pointer         of File type */         FILE *fp;         char temp[30];         /* fp points to the file input.txt         and opens it in read mode */         fp=fopen("input.txt", "r");         while((fscanf(fp, "%s", temp))!=EOF)         {                 if(!(strcmp(temp, str)))                 {                         printf("FOUND\n");                         return;                 }         }         printf("NOT FOUND\n");                 return; } Execution input.txt file $ cat input.txt binu biju biji bin

lex program to determine identifiers

lex program to determine valid identifiers.Users can enter an identifier and the program will display whether it is valid or not. ******************************************************************************** /*lex code to determine whether input is an identifier or not*/ %option noyywrap %% ^[a-zA-Z_][a-zA-Z0-9_]*           printf("Valid Identifier"); ^[^a-zA-Z _]                                  printf("Invalid Identifier"); .*                                                          printf("Invalid Indentifier"); %% main() {         yylex(); } Execution $ flex id3.lex $ gcc lex.yy.c $ ./a.out 2ghdd Invalid Indentifier 46shs Invalid Indentifier dre45 Valid Identifier dhd_dgd Valid Identifier _dghd Valid Identifier 252 Invalid Indentifier dgd$ Invalid Indentifier _dhd Valid Identifier eye-hd Invalid Indentifier

lex program to count number of lines , words and characters

lex program to count number of lines, words  and characters *******************************************  %{ int num_lines=0,num_words=0,num_chars=0; %} %option noyywrap %% \n            ++num_lines; ++num_chars;++num_words; " "            ++num_words;++num_chars; .               ++num_chars; %% main() { yylex(); printf( "No of lines = %d, No of words=%d,No of chars = %d\n",num_lines,num_words, num_chars); }

Lex program to add line numbers to a given file and create a new file

Lex program to add line numbers to a given file and create a new file ************************************************** %{ int line_number = 1; %} %option noyywrap line .*\n %% {line} { printf("%10d %s", line_number, yytext); fprintf(yyout,"%10d %s",line_number,yytext);line_number++; } %% int main() { yyin = fopen("test.c","r"); yyout=fopen("testnew.c","w"); yylex(); return 0; } Execution $cat test.c # input file #include <stdio.h> void main() {  int a=2,b=4;  printf("%d",a+b); } $flex lnumber.lex $ gcc lex.yy.c $ ./a.out          1 #include <stdio.h>          2 void main()          3 {          4  int a=2,b=4;          5  printf("%d",a+b);          6 } $cat testnew.c # new file created             1 #include <stdio.h>          2 void main()          3 {          4  int a=2,b=4;          5  printf("%d",a+b);          6 }

lex program to find the largest words and its length

lex code to find the largest words and its length  *************************** %{ int wordlen = 0,i=0; char lwords[100][100]; %} %option noyywrap %% [a-zA-Z]+ {         if (yyleng > wordlen)                        wordlen = yyleng;         strcpy(lwords[i++],yytext);         } %% int main() { int j; yylex(); printf("Maximum  length is %d ", wordlen); printf("largest length words\n"); for ( j=0;j<i;j++)  if(strlen(lwords[j])==wordlen)     printf("%s\n",lwords[j]); return 0; } Execution flex lword.lex $ gcc lex.yy.c $ ./a.out this is a program to find largest words from the input Maximum  length is 7 largest length words program largest

Converting BNF rules for arithmetic expression into yacc form and generating trees

 st.lex ****************** %{         #include <bits/stdc++.h>         #include "st.tab.h" %} %option noyywrap %% [a-zA-Z_][a-zA-Z_0-9]*  yylval.str=strdup(yytext);return ID ; ([0-9]*\.?[0-9]*)|(\".*?\")|(\'.\')     yylval.str=strdup(yytext);return VAL ; ";"             yylval.str=strdup(yytext);return SC ; "+"             yylval.str=strdup(yytext);return PL ; "-"             yylval.str=strdup(yytext);return MI ; "*"             yylval.str=strdup(yytext);return MUL ; "/"             yylval.str=strdup(yytext);return DIV ; "="             yylval.str=strdup(yytext);return EQ ; "("             yylval.str=strdup(yytext);return OP ; ")"             yylval.str=strdup(yytext);return CL ; "^"             yylval.str=strdup(yytext);return POW ; [\n \t]+        ; .               yylval.str=strdup(yytext);return UNR; %% st.y ****************** %{         #include<bits/stdc+

lex yacc tutorial by Tom Niemann

 

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 id

simple calculator yacc program

cal.lex **************************  %{ /* Definition section */ #include<stdio.h> #include "cal.tab.h" extern int yylval; %} %option noyywrap /* Rule Section */ %% [0-9]+ {                 yylval=atoi(yytext);                 return NUMBER;         } [\t] ; [\n] return 0; . return yytext[0]; %% cal.y ********************************************* %{ /* Definition section */ #include<stdio.h> int flag=0; void yyerror(); %} %token NUMBER %left '+' '-' %left '*' '/' '%' %left '(' ')' /* Rule Section */ %% S: E{                 printf("\nResult=%d\n", $$);                 return 0;                 }; E:E'+'E {$$=$1+$3;}         |E'-'E {$$=$1-$3;}         |E'*'E {$$=$1*$3;}         |E'/'E {$$=$1/$3;}         |E'%'E {$$=$1%$3;}         |'('E')' {$$=$2;}         | NUMBER {$$=$1;} ; %% //driver code void main() { printf("\nEnter Any Arithmetic Expr

binary to decimal conversion - yacc program

 bindec.lex *************************************** %{ /* Definition section */ #include<stdio.h> #include<stdlib.h> #include"bindec.tab.h" extern int yylval; %} %option noyywrap /* Rule Section */ %% 0 {yylval=0;return ZERO;} 1 {yylval=1;return ONE;} [ \t] {;} \n return 0; . return yytext[0]; %% bindec.y ******************************************** %{ /* Definition section */ #include<stdio.h> #include<stdlib.h> %} %token ZERO ONE /* Rule Section */ %% N: L {printf("The decimal is=%d\n", $$);} L: L B {$$=$1*2+$2;}    | B {$$=$1;} B: ZERO {$$=$1;}    |ONE {$$=$1;}; %% //driver code int main() { while(yyparse()); } int yyerror(char *s) { printf("\n Invalid Expression \n"); } Execution ************************* $ bison -d bindec.y $ flex bindec.lex $ gcc lex.yy.c bindec.y.c $ ./a.out 101101 The decimal is=45

arithmetic expression evaluation flex - bison or lex - yacc program

 arith.lex  ********************* %{         /* Definition section*/         #include "arith.tab.h"         extern yylval; %} %option noyywrap %% [0-9]+ {                         yylval = atoi(yytext);                         return NUMBER;                         } [a-zA-Z]+ { return ID; } [ \t]+           ; /*For skipping whitespaces*/ \n      return 0; .       { return yytext[0]; } %% arith.y yacc/bison program ****************************************** %{         /* Definition section */ #include <stdio.h> %} %token NUMBER ID // setting the precedence // and associativity of operators %left '+' '-' %left '*' '/' /* Rule Section */ %% E : T    {                                 printf("Result = %d\n", $$);                                 return 0;                         } T :     T '+' T { $$ = $1 + $3; }         | T '-' T { $$ = $1 - $3; }         | T '*' T { $$ = $1 * $3; }         | T '/' T

c program to find e-closure of NFA

program to find e-closure in an NFA. States are represented as q0,q1,q2 etc. The input symbols are {0,1,e}.The transition table is read from the file automta.txt *************************************** #include<stdio.h> #include<string.h> char result[20][20], copy[3], states[20][20]; void add_state(char a[3], int i) {   strcpy(result[i], a); } void display(int n) {   int k = 0;   printf("\n  Epsilon closure of %s = { ", copy);   while (k < n) {     printf(" %s", result[k]);     k++;   }   printf(" }\n"); } int main() {   FILE * INPUT;   INPUT = fopen("automata.txt", "r");   char state[3];   int end, i = 0, n, k = 0;   char state1[3], input[3], state2[3];   printf("\n Enter the no of states: ");   scanf("%d",&n);   printf("\n Enter the states max =20 \n");   for (k = 0; k < n; k++) {     scanf("%s", states[k]);   }   for (k = 0; k < n; k++) {     i = 0;     strcpy(state, st