StudentLabManualdocx For CD - 2020 - 08 - 29 - 13 - 11 - 23

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

COMPILER DESIGN

(2170701)

2020-2021

STUDENT LAB MANUAL

INDEX

1
Experiment Page No
No Title
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.
2 Write a C program to identify whether a given line is a comment or
not.
3 Write a C program to test whether a given identifier is valid or not.
4 Write a C program to simulate lexical analyzer for validating
operators
5 To Study about Lexical Analyzer Generator(LEX) and Flex(Fast
Lexical Analyzer)
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.
b. Write a Lex program to count number of vowels and
consonants in a given input string.
7 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. Write a Lex program which adds line numbers to the
given file and display the same onto the standard output.
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.
9 Write a C program for implementing the functionalities of
predictive parser for the mini language.
10 Write a C program for constructing of LL (1) parsing.
11 Write a C program for constructing recursive descent parsing
12 Write a C program to implement LALR parsing.
13 Write a C program to implement operator precedence parsing.
14 To Study about Yet Another Compiler-Compiler(YACC).
15 Create Yacc and Lex specification files to recognizes arithmetic
expressions involving +, -, * and / .
16 Create Yacc and Lex specification files are used to generate a
calculator which accepts,integer and float type arguments.

2
Experiment 1

Title: 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.
Hint:

3
Experiment 2

Title: Write a C program to identify whether a given line is a comment or not.

HINT:
Read the input string.

Check whether the string is starting with ‘/’ and check next character is ‘/’ or’*’.

If condition satisfies print comment. Else not a comment.

4
Experiment 3

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

HINT:

Read the given input string.

Check the initial character of the string is numerical or any special character except ‘_’
then print it is not a valid identifier.

Otherwise print it as valid identifier if remaining characters of string doesn’t contains any
special characters except ‘_’.

5
Experiment :4

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

Hint:

Read the given input.

If the given input matches with any operator symbol. Then display in terms of words of
the particular symbol. Else print not a operator.

6
Experiment: 5

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

Study Practical

7
Experiment: 6

Title: 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.
Hint:

Read the input string.


Check whether the string is identifier/ keyword /symbol by using the rules of
identifier and keywords using LEX Tool

b. Write a Lex program to count number of vowels and consonants in a given


input string.

Hint:

Read the input string.


Check whether the string is identifier/ keyword /symbol by using the rules of identifier
and keywords using LEX Tool.

Computer Engineering Computer Engineering

8
Experiment: 7

Title: Implement following programs using Lex.


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

Hint:

Read the input string.


Check whether the string is identifier/ keyword /symbol by using the rules of identifier
and keywords using LEX Tool

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

Hint:

Read the input string.


Check whether the string is identifier/ keyword /symbol by using the rules of identifier
and keywords using LEX Tool
c. Write a Lex program which adds line numbers to the given file and display
the same onto the standard output.

Hint:

Read the input string.


Check whether the string is identifier/ keyword /symbol by using the rules of identifier
and keywords using LEX Tool

9
Experiment: 8

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

Hint: Read the input string.


Check whether the string is identifier/ keyword /symbol by using the rules of identifier
and keywords using LEX Tool

10
Experiment: 9

Title: Write a C program for implementing the functionalities of predictive parser for the
mini language.
Hint: Read the input string.

By using the FIRST AND FOLLOW values.

Verify the FIRST of non terminal and insert the production in the FIRST value

If we have any @ terms in FIRST then insert the productions in


FOLLOW values Constructing the predictive parser table

11
Experiment: 10

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

Hint : Read the input string.

Using predictive parsing table parse the given input using stack .

If stack [i] matches with token input string pop the token else shift it repeat the process
until it reaches to $.

12
Experiment: 11

Title: Write a C program for constructing recursive descent parsing

Hint : Read the input string.

Write procedures for the non terminals

Verify the next token equals to non terminals if it satisfies match the non terminal. If the
input string does not match print error.

13
Experiment : 12

Title: Write a C program to implement LALR parsing.

Hint: Read the input string.

Push the input symbol with its state symbols in to the stack by referring look aheads We
perform shift and reduce actions to parse the grammar.

Parsing is completed when we reach $ symbol.

14
Experiment :13

Title: Write a C program to implement operator precedence parsing.

Hint:

Read the arithmetic input string.

Verify the precedence between terminals and symbols

Find the handle enclosed in < . > and reduce it to production symbol. Repeat the process
till we reach the start node.

15
Experiment :14

Title: To Study about Yet Another Compiler-Compiler(YACC).

Study Practical

16
Experiment :15

Title: Create Yacc and Lex specification files to recognizes arithmetic expressions involving
+, -, * and / .
Hint : Reading an input file

Calculate the sum or multiplication of given expression.


Using expression rule print the result of the given values.

17
Experiment : 16
Title: Create Yacc and Lex specification files are used to generate a calculator which
accepts, integer and float type arguments.

Hint :Reading an input file


Calculate the sum or multiplication of given expression.
Using expression rule print the result of the given values.

18

You might also like