Posts

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