Chaos Project

General => Academics => Topic started by: KK20 on June 08, 2013, 03:18:08 am

Title: Questions relating to 'Introduction to Compilers' class
Post by: KK20 on June 08, 2013, 03:18:08 am
I may have some more questions regarding this class I am taking right now, so I'll be making a thread just to make sure.

Anyways, my friend discovered in the course text a possible error. It says that the regular expression for a legal floating-point constant in Pascal is as follows:

(ε|+|-)(0|d*).(0|d*)

ε = null character
d = 0..9
The text says this ensures that a number exists on both the left and right sides of the decimal point. But how can that be?

If I choose ε and d* in both instances and set them to have zero repetitions, that would leave me with only the decimal point (from what I saw, this caused an error in Pascal).

Thus, I proposed this:

(ε|+|-)((d+.d*)|(d*.d+))

This ensures that we have at least one digit on the left or right sides of the decimal, right? Is there a better way to do this?
Title: Re: Questions relating to 'Introduction to Compilers' class
Post by: Blizzard on June 08, 2013, 04:07:18 am
I agree, the first one seems like it's possible to just get a dot character and your regular expression seems to solve that problem.
Title: Re: Questions relating to 'Introduction to Compilers' class
Post by: KK20 on June 09, 2013, 03:29:30 am
Cool, thanks for the confirmation. It looks like we have something to address to the teacher, seeing as this is an instructor's text.

No other problems found so far. Got the DFSM for the lexer working. And they said it would take 20 hours to do... (half my time was spent figuring out how to use C++ again :P)
Title: Re: Questions relating to 'Introduction to Compilers' class
Post by: KK20 on June 13, 2013, 05:10:12 pm
Bleh, that was a brutal first exam. Here are two questions I struggled with:

1.) Given that the alphabet is {a,b}, write a regular expression that contains all the strings that have exactly 2 a's and at least 1 b.
My Answer after 20 minutes: ShowHide

b+ab*ab* | ab*ab+ | ab+ab*


2.) Remove left recursion and backtracking in the following grammar:

X -> XaY | XbY
X -> Y
Y -> YcB
Y -> a | B
B -> X