Homework 1
CS536-S24 Intro to PLs and Compilers

Question 1 | Question 2


Question 1

Consider the following non-deterministic finite state machine M:

Note: in the diagram ε is denoted using $.

Part a

What is L(M), i.e., the language defined by M?

Part b

For each state S in M, determine eclose(S).

Part c

Convert the NFA M to an equivalent NFA M* containing no ε-transitions.

Part d

Convert your NFA M* containing no ε-transitions from Part c to an equivalent DFA M**. Make sure to optimize M** to remove any unreachable or dead states.


Question 2

Java (as well as C and C++) allows comments delimited by "/*" and "*/". This kind of comment can be defined in English as follows:

A comment consists of three parts:

  1. a slash followed by a star, followed by

  2. the body of the comment, followed by

  3. a star followed by a slash.

The body of the comment can be empty, or can contain any characters except the two-character sequence "*/".

Note that the body of a comment can include stars and slashes, just not "*/".

Assume that the following JLex macros have been defined:

SLASH  = [/]
STAR   = [*]

Part a

Below are 5 incorrect JLex patterns for the kind of comment defined above. For each pattern, explain what is wrong. For example, you might say The pattern does not allow the comment body to include stars, or The pattern does allow the comment body to include */. If the pattern both disallows some "good" comments and allows some "bad" comments, give two explanations, one for each problem with the pattern. Then, for each problem with the pattern, give a string that illustrates the problem; i.e., give a string that is not matched by the pattern but is a "good" comment and/or give a string that is matched by the pattern but is a "bad" comment. Be sure that it is clear whether your example strings are intended to be "good" or "bad" comments.

{SLASH}{STAR}[^(*/)]*{STAR}{SLASH}

{SLASH}{STAR}(.)*{STAR}{SLASH}

{SLASH}{STAR}([^*]*{STAR}+[^*/])*{STAR}+{SLASH}

{SLASH}{STAR}([^*]|[^/])+{STAR}{SLASH}

{SLASH}{STAR}[^*]*{STAR}+{SLASH}+

Part b

Give a correct JLex pattern for comments as defined above. You may define and use additional macros. Make your pattern as readable as possible.


Last Updated: 2/5/2024     © 2024 CS 536