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

  public static int count;

  %%

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

  %x QUOTED_STR_STATE

  %%

  <YYINITIAL>{QUOTE}          { BEGIN(QUOTED_STR_STATE);
                                count = 0;
                              }

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

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

  <QUOTED_STR_STATE>{QUOTE}
                              { BEGIN(INITIAL);
			        return Counter.count;
                              }