Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Lexical analysis is the first phase in the compilation process and plays a crucial role., Exercises of Compiler Design

Lexical analysis is the first phase in the compilation process and plays a crucial role in compiler design. This phase is responsible for reading the source code and breaking it down into a sequence of tokens. Tokens are the smallest units of meaning in the code, like keywords, identifiers, operators, and punctuation marks. The lexical analyzer, or scanner, processes the raw input text, identifies tokens, and ignores irrelevant elements like comments and whitespace.

Typology: Exercises

2024/2025

Uploaded on 11/07/2024

muni-kiran
muni-kiran 🇮🇳

3 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
LEXICAL PHASE
1. Develop a regular expression (RE) that detects the longest string over the alphabet {a-z}
with the following properties:
i). The string begins with an ‘a’ character and ends with a ‘z’ character;
ii). After the first ‘a’, the string can include an alternation of two sub sequences, namely
a sequence that begins with a ‘b’ character followed by zero or more ‘s’ characters ending with
an ‘e’ character and a subsequence that begins with a ‘c’ character but does not have any ‘s’
character until a terminating ‘t’ character.
iii). If at any point in the subsequence there is a ‘x’ character, that specific sequence is
considered terminated, i.e., the ‘x’ acts as the ‘e’ character in the first type of sequence and as
the ‘t’ character in the second type of sequence.
iv). The two subsequences cannot be nested but can be repeated in any alternating order.
As an example, the string “absssecaaabbbafghtbsez” is to be accepted as well as the string
“abssxcfftz”
2. Write the Regular Expression for all the strings over {a,b} such that the string contains equal
occurrence of the substrings ab and ba.
3. a) Suppose you have a machine M, a program P written in language L, and a compiler for L
that runs on M. Draw a tombstone diagram that shows how to run P on M.
b) Suppose you have a machine M, a program P written in language L1, an interpreter for L1
written in language L2, and an interpreter for L2 that runs on M. Draw a tombstone diagram
that shows how to run P on M.
c) Suppose you have two machines M1 and M2, a program P written in language L, an
interpreter for L that runs on M1, and a translator from M1 to M2 written in L. Draw a
tombstone diagram that shows how to run P on M2.
pf2

Partial preview of the text

Download Lexical analysis is the first phase in the compilation process and plays a crucial role. and more Exercises Compiler Design in PDF only on Docsity!

LEXICAL PHASE

  1. Develop a regular expression (RE) that detects the longest string over the alphabet {a-z} with the following properties: i). The string begins with an ‘a’ character and ends with a ‘z’ character; ii). After the first ‘a’, the string can include an alternation of two sub sequences, namely a sequence that begins with a ‘b’ character followed by zero or more ‘s’ characters ending with an ‘e’ character and a subsequence that begins with a ‘c’ character but does not have any ‘s’ character until a terminating ‘t’ character. iii). If at any point in the subsequence there is a ‘x’ character, that specific sequence is considered terminated, i.e., the ‘x’ acts as the ‘e’ character in the first type of sequence and as the ‘t’ character in the second type of sequence. iv). The two subsequences cannot be nested but can be repeated in any alternating order. As an example, the string “absssecaaabbbafghtbsez” is to be accepted as well as the string “abssxcfftz”
  2. Write the Regular Expression for all the strings over {a,b} such that the string contains equal occurrence of the substrings ab and ba.
  3. a) Suppose you have a machine M, a program P written in language L, and a compiler for L that runs on M. Draw a tombstone diagram that shows how to run P on M. b) Suppose you have a machine M, a program P written in language L1, an interpreter for L written in language L2, and an interpreter for L2 that runs on M. Draw a tombstone diagram that shows how to run P on M. c) Suppose you have two machines M1 and M2, a program P written in language L, an interpreter for L that runs on M1, and a translator from M1 to M2 written in L. Draw a tombstone diagram that shows how to run P on M2.
  1. Consider the regular expression below which can be used as part of a specification of the definition of exponents in floating-point numbers. Assume that the alphabet consists of numeric digits (‘0’ through ‘9’) and alphanumeric characters (‘a’ through ‘z’ and ‘A’ through ‘Z’) with the addition of a selected small set of punctuation and special characters (say in this example only the characters ‘+’ and ‘-‘ are relevant). Also, in this representation of regular expressions the character ‘.’ denotes concatenation. Exponent = (+ | - | ε). (E | e). (digit)+ For this regular expression, Derive an NFA capable of recognizing its language using the Thompson construction and construct the DFA using the subset construction followed by Hopcraft’s minimization procedure.
  2. Given the regular expression RE = (ab|b)(ba*) over the alphabet Σ= {a,b} do the following transformations: a). Derive a NFA using the Thompson construction capable of identifying the strings generated by this regular expression. b). Convert the NFA obtained in a) to a DFA. c). Minimize the resulting DFA using Hopcroft’s Algorithm.