Gema Exercises

Patterns

Write Gema rules to

  1. replace all occurrences of "brown cow" with "blue dog"

  2. replace colons, semicolons, dollar signs, and asterisks with "P"
  3. replace all three-letter words whose first letter is "t" and whose last letter is "n" with "xxx"
  4. find all strings that start with "a" and end with "n", and change the "a" to "b" and the "n" to "p" (alan becomes blap, aladdin becomes bladdip, etc.)
  5. replace all double characters with triple characters (fallen => falllen, bitter => bittter, x//y => x///y, etc.)
  6. invert all two-character strings that start with "t" (after => afetr, ton => otn, etc.)

Recognizers

Write Gema rules to

  1. put quotes around all punctuation characters
  2. replace all numbers with "NNN"
  3. delete all white space
  4. replace all upper-case letters with "U"
  5. invert all number-word pairs ( 3 tables => tables 3, 1 hour => hour 1, etc.)
  6. put square brackets around all file names (/usr/bin => [/usr/bin], etc.)
  7. put "ctl-" in front of all control characters
  8. delete all characters except lower-case letters
  9. put square brackets around all words of the form "b" + 0 or more letters + a digit (b3=>[b3], bit9 => [bit9], etc.)
  10. add an exclamation point after all 4-letter words

Escape Sequences

Write Gema rules to

  1. replace "Now" with "Then", but only at the beginning of a line
  2. put "Top" and "Bottom" at the beginning and end of a file, respectively
  3. replace all tab characters with three spaces
  4. replace "John Thomas" with "joHn tHomas", ignoring capitalization in the input
  5. replace "the" with "this", but only when "the" is a whole word
  6. delete Ada-style comments ("--" and everything after it on the same line)
  7. print all strings in the input that begin with "p" (given "pepper plants", print "pepper plants", "pper plants", "per plants", and "plants")
  8. put "s" after all words that are followed by white space

Functions

Write Gema rules to

  1. add 100 to every number
  2. count the number of words in the input file
  3. print every word by itself centered on a 50-character line
  4. replace all occurrences of "char" plus a number with the corresponding character ("char13" => a carriage return, etc.)
  5. replace all numbers greater than "100" with "BIG NUMBER"
  6. replace all words greater than "spoon" with "ZOOM" (ignoring capitalization)
  7. at the top of the file, print "Date" and the current date and time in international standard form (yyyy-mm-dd hh:mm:ss)
  8. replace all x,y number pairs with 10*x - y/3
  9. replace all identifiers with their lower-case equivalents (HotJava => hotjava, etc.)
  10. capitalize all words (the => The, etc.)
  11. print all lines that contain the word "zebra", along with the file name and the line number in the format "F <filename>, L <linenumber>"
  12. replace all printing characters with their numeric value
  13. replace all hex numbers of the form 0xDDDD with their decimal equivalents
  14. print all lines that are palindromes (i.e. are the same backwards and forwards)
  15. format the input file 40 characters wide with "# " at the beginning of each line
  16. print a list of all the words in the input file, with each of the words listed just once

Command-Line Arguments and Domains

Write Gema rules to

  1. define a domain that deletes vowels and make it the default domain; then define another domain that inherits from the first, but in addition deletes consonants
  2. accept a "-delete " command-line parameter, and delete the specified word from the input file
  3. define two domains, one named xyz that replaces "e" with "x", and one named abc that replaces "a" with "y"; then define a command-line parameter -domain which accepts the name of the domain to use on the input file