Answers to Self-Study Questions

Test Yourself #1

Question 1: The character class [a-zA-Z0-9] matches any letter or any digit.

Question 2: The pattern [a-zA-Z][a-zA-Z0-9]* matches any Pascal identifier (a sequence of one or more letters and/or digits, starting with a letter).

Question 3: The pattern [a-zA-Z_][a-zA-Z0-9_]* matches any C identifier (a sequence of one or more letters and/or digits and/or underscores, starting with a letter or underscore).

Question 4: The pattern ([a-zA-Z_][a-zA-Z0-9_]*[a-zA-Z0-9])|[a-zA-Z] matches any C identifier that does not end with an underscore.


Test Yourself #2

NOTSPECIAL = [^\n\"\\]


Test Yourself #3

  class Counter {
    public static int count;
  }

  %%

  QUOTE =               [\"]
  NOT_QUOTE_OR_NEWLINE= [^\"\n]

  %state QUOTED_STR_STATE

  %%

  <YYINITIAL>{QUOTE}          { yybegin(QUOTED_STR_STATE);
                                Counter.count = 0;
                              }

  <QUOTED_STR_STATE>{NOT_QUOTE_OR_NEWLINE}*
                              { // do nothing
                              }

  <QUOTED_STR_STATE>{QUOTE}{QUOTE}
                              { Counter.count++;
                              }

  <QUOTED_STR_STATE>{QUOTE}
                              { yybegin(YYINITIAL);
			        return Counter.count;
                              }