Skip to main content

2. Implement a Lexical Analyzer for a given program using Lex Tool.

 Let's assume we have a simple programming language with keywords `if`, `else`, `while`, and identifiers (variable names) consisting of letters and digits. We want to tokenize a given program written in this language.


Here's the Lex specification file (`lexer.l`) along with explanations:


```lex

%{

#include <stdio.h>

%}


%option noyywrap


%%

if      { printf("IF\n"); }

else    { printf("ELSE\n"); }

while   { printf("WHILE\n"); }

[a-zA-Z][a-zA-Z0-9]* { printf("IDENTIFIER: %s\n", yytext); }

[ \t\n]  ; // Skip whitespace

.        { printf("UNKNOWN CHARACTER: %s\n", yytext); }

%%


int main() {

    yylex();

    return 0;

}

```


Explanation of each section:


- `%{ ... %}`: This section is used for including any necessary header files and declaring global variables or definitions. In this case, we include `stdio.h` for printing messages.


- `%option noyywrap`: This option indicates that the `yywrap` function won't be used. It's commonly used to signal the end of input in Lex programs.


- `%%`: This delimiter separates the Lex rules from the user code.


- `if`, `else`, `while`: These are keywords in our language. For each keyword, we specify a regular expression followed by an action to be taken when a match is found. In this case, we print the corresponding token name.


- `[a-zA-Z][a-zA-Z0-9]*`: This regular expression matches identifiers. It starts with a letter (uppercase or lowercase) and can be followed by letters or digits. When an identifier is matched, we print its value using `yytext`.


- `[ \t\n]`: This regular expression matches whitespace characters (spaces, tabs, newlines). We skip these characters.


- `.`: This regular expression matches any character that didn't match any of the previous patterns. When an unknown character is encountered, we print its value using `yytext`.


- The final section (`main()`) initializes the Lexical Analyzer using `yylex()`.


To compile and run the program:


1. Save the Lex specification in a file named `lexer.l`.

2. Open a terminal and navigate to the directory containing `lexer.l`.

3. Run the following commands:

   - `lex lexer.l` (compiles the Lex specification)

   - `gcc lex.yy.c -o lexer -ll` (compiles the Lex-generated code)

   - `./lexer` (runs the program)


Now, you can provide input text (your program) to the compiled lexer, and it will tokenize the input and display the corresponding tokens.


Comments

Popular posts from this blog

Program 6: Finding Prime Numbers in a Range

  ** Program 6: Finding Prime Numbers in a Range ** ```assembly .model small .stack 100h .data     start dw 1     end dw 20 .code     main proc         mov ax, start           ; Load 'start' into AX              find_primes:         inc ax                  ; Move to the next number         cmp ax, end             ; Compare with 'end'         jg end_prog             ; Jump to end if AX > 'end'                  mov cx, ax              ; Copy AX to CX   ...

4. Write a LEX Program to convert the substring abc to ABC from the given input string.

Sure, here's a LEX program that converts the substring "abc" to "ABC" in the given input string: ```lex %{ #include <stdio.h> %} %% .*abc.*    { printf("%s", yytext); }    /* Match and print everything containing 'abc' */ .         ;                            /* Ignore other characters */ %% int main() {     yylex();     return 0; } ``` Now, let's break down the code line by line: 1. `%{` and `%}`: These are used to enclose C code that should be included in the generated C code. In this case, it includes the standard input-output library (`<stdio.h>`). 2. `.*abc.*`: This is a regular expression pattern. It matches any sequence of characters that contains the substring "abc". 3. `{ printf("%s", yytext); }`: This is the action associated with the pattern. When the pattern is matched, the `yytext` variable contains the matched te...

ktu 2019 Cse Complier programing Lab Syllabus

 1Design and implement a lexical analyzer using C language to recognize all valid tokens  in the input program. The lexical analyzer should ignore redundant spaces, tabs and  newlines. It should also ignore comments. 2. Implement a Lexical Analyzer for a given program using Lex Tool. 3. Write a lex program to display the number of lines, words and characters in an input text. 4. Write a LEX Program to convert the substring abc to ABC from the given input string. 5. Write a lex program to find out the total number of vowels and consonants from the given  input string. 6. Generate a YACC specification to recognize a valid arithmetic expression that uses  operators +, – , *,/ and parenthesis. 7. Generate a YACC specification to recognize a valid identifier which starts with a letter  followed by any number of letters or digits.   8. Implementation of Calculator using LEX and YACC  9. Convert the BNF rules into YACC form and write code to generat...