Skip to main content

1, Design 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

#include <stdio.h>

#include <ctype.h>


int main() {

    char c;

    while ((c = getchar()) != EOF) {

        if (isspace(c)) {

            // Ignore whitespace, tabs, and newlines

            continue;

        }

        if (c == '/') {

            // Check for comments

            char nextChar = getchar();

            if (nextChar == '/') {

                while ((nextChar = getchar()) != '\n');

            } else {

                putchar(c);

                putchar(nextChar);

            }

        } else {

            putchar(c);

        }

    }

    return 0;

}

```


Now, let's explain each line of the code:


1. `#include <stdio.h>`: This includes the standard input/output library for input and output operations.


2. `#include <ctype.h>`: This includes the ctype library, which provides functions to classify and transform characters.


3. `int main() {`: The main function, where the program execution begins.


4. `char c;`: Declares a character variable `c` to hold the current character read from input.


5. `while ((c = getchar()) != EOF) {`: This starts a loop that reads characters from input until the end of the file (EOF) is reached.


6. `if (isspace(c)) {`: Checks if the current character is a whitespace character (space, tab, newline).


7. `continue;`: If the current character is a whitespace character, the loop continues to the next iteration, ignoring the whitespace.


8. `if (c == '/') {`: Checks if the current character is a slash.


9. `char nextChar = getchar();`: Reads the next character from input after the slash.


10. `if (nextChar == '/') {`: Checks if the next character is also a slash, indicating the start of a single-line comment.


11. `while ((nextChar = getchar()) != '\n');`: Skips characters until a newline is encountered, effectively ignoring the single-line comment.


12. `} else {`: If the next character is not a slash, this block handles other cases.


13. `putchar(c);`: Outputs the current character to the standard output.


14. `putchar(nextChar);`: Outputs the next character (after the slash) to the standard output.


15. `} else {`: If the current character is neither a whitespace nor a slash, this block handles other characters.


16. `putchar(c);`: Outputs the current character to the standard output.


17. `}`: Closes the inner `else` block.


18. `}`: Closes the loop.


19. `return 0;`: Exits the program with a success status.


This code reads characters from input, processes them according to the rules for ignoring whitespace and comments, and outputs valid tokens. Keep in mind that this is a basic example, and in real-world scenarios, lexical analyzers are more complex and capable of recognizing various token types and patterns.

Comments

Popular posts from this blog

6. Generate a YACC specification to recognize a valid arithmetic expression that uses operators +, – , *,/ and parenthesis.

Below is a YACC specification for recognizing valid arithmetic expressions that involve operators (+, -, *, /) and parentheses. I'll explain each line of the code afterward: ```yacc %{ #include <stdio.h> %} %token NUMBER %left '+' '-' %left '*' '/' %% expression: expression '+' expression            | expression '-' expression            | expression '*' expression            | expression '/' expression            | '(' expression ')'            | NUMBER            ; %% int yylex() {     // Code for tokenizing input and returning tokens } void yyerror(const char *s) {     fprintf(stderr, "Error: %s\n", s); } int main() {     yyparse();     return 0; } ``` Now, let's break down the YACC specification line by line: 1. `%{` ... `%}`: This is the C code section where y...

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...