0% found this document useful (0 votes)
17 views

Compiler Lab

Uploaded by

Ashutosh Rana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Compiler Lab

Uploaded by

Ashutosh Rana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

INDEX

S no. Topic Date Remarks


01 Design a lexical analyzer for given language and the lexical
analyzer should ignore redundant spaces, tabs and new
lines. It should also ignore comments. Although the syntax
21/08/2024
specification states that identifiers can be arbitrarily long,
you may restrict the length to some reasonable value.
Simulate the same in C language
02 Write a C program to identify whether a given line is a
comment or not.
2/09/2024

03 Write a C program to test whether a given identifier is


valid or not.
11/09/2024

04 Write a C program to simulate lexical analyzer for


validating operators.
16/09/2024

05 To Study about Lexical Analyzer Generator(LEX) and


Flex(Fast Lexical Analyzer).
25/09/2024

06 Implement following programs using Lex: a) Create a


Lexer to take input from text file and count no of
characters, no. of lines & no. of words. b) Write a Lex 7/10/2024
program to count number of vowels and consonants in a
given input string.
07 Implement following programs using Lex. a) Write a Lex
program to print out all numbers from the given file. b)
Write a Lex program to printout all HTML tags in file. c) 16/10/2024
Write a Lex program which adds line numbers to the given
file and display the same onto the standard output
08 Write a Lex program to count the number of
comment lines in a given C program. Also
21/10/2024
eliminate them and copy that program into
separate file.
09 Write a C program for implementing the
functionalities of predictive parser for the mini 11/11/2024
language.
10 Write a C program for constructing of LL (1)
parsing 25/11/2024
Experiment 1
Design a lexical analyzer for given language and the lexical analyzer should ignore redundant spaces, tabs and new
lines. It should also ignore comments. Although the syntax specification states that identifiers can be arbitrarily long,
you may restrict the length to some reasonable value. Simulate the same in C language.
Output :
Experiment 2
Write a C program to identify whether a given line is a comment or not
Output :
Experiment 3

Write a C program to test whether a given identifier is valid or not.


Experiment 4

Write a C program to simulate lexical analyzer for validating operators.


Experiment 5

To Study about Lexical Analyzer Generator(LEX) and Flex(Fast Lexical Analyzer).

Lexical Analyzer (LEX)

 LEX is a tool used to generate lexical analyzers (scanners or tokenizers). These analyzers are programs
that process input text and convert it into a sequence of tokens, which are the building blocks for parsing
in compilers and interpreters.
 Purpose: To automate the creation of a lexical analyzer based on predefined patterns of tokens.
 Input: A specification file (typically .l extension) that defines:
 Tokens: Regular expressions that represent patterns in the text.
 Actions: Code snippets to execute when a token is recognized.
 Output: A C program (usually lex.yy.c) that can be compiled and used as a tokenizer.

Flex (Fast Lexical Analyzer)

 Flex is an improved, faster, and more modern version of LEX. It retains compatibility with LEX while
offering better performance and additional features.
 Advantages over LEX:
 Faster token recognition.
 Generates more efficient C code.
 Can interface seamlessly with tools like Bison (a parser generator).

Key Features of LEX/Flex

1. Regular Expressions: Define patterns for tokens.


o Examples:
 [0-9]+: Matches integers.
 [a-zA-Z_][a-zA-Z0-9_]*: Matches identifiers.
2. Actions: Specify what to do when a token is matched.
o Examples: Print token, count occurrences, or store in a data structure.
3. Built-in Variables:
o yytext: Points to the current token.
o yyleng: Length of the current token.
4. Integration with Parsers:
o Commonly used with YACC or Bison to build complete compilers.

Advantages of Flex

1. Faster scanning and better optimization.


2. Compatibility with modern C compilers and libraries.
3. Advanced debugging options.
4. Ability to generate reentrant scanners.

Applications

1. Compiler Design: Tokenize source code.


2. Data Parsing: Extract structured data from logs, files, or streams.
3. Scripting and Command Parsing: Parse commands in shells or interpreters.
Experiment 6

Implement following programs using Lex:


a) Create a Lexer to take input from text file and count no of characters, no. of lines & no. of words.

Bash ::::
b) Write a Lex program to count number of vowels and consonants in a given input string.

Bash :::::::::::::
Experiment 7

Implement following programs using Lex.


a) Write a Lex program to print out all numbers from the given file.

Bash::::::
b) Write a Lex program to printout all HTML tags in file.

Bash::::::::::::
c) Write a Lex program which adds line numbers to the given file and display the same onto the standard output.

Bash ::::::::::::::::::::
Experiment 8

Write a Lex program to count the number of comment lines in a given C program. Also eliminate them
and copy that program into separate file.
Experiment 9

Write a C program for implementing the functionalities of predictive parser for the mini language.
Experiment 10

Write a C program for constructing of LL (1) parsing.

You might also like