'Build Your Own Lisp' Solutions: 6.2

Q: Write a regular expression matching strings of consecutive a and b such as ababab or aba.


A:

/(ab)/



'Build Your Own Lisp' Solutions: 6.1

Q: Write a regular expression matching strings of all a or b such as aababa or bbaa.


A:

/^(a|b)+$/



'Build Your Own Lisp' Solutions: 5.4

Q: Describe textually a grammar for decimal numbers such as 0.01 or 52.221.


A:

// Using the same system he's given us to describe decimals:
"                                             \
 digit   : \"0\" | \"1\" \"2\" | \"3\" \"4\"  \
         | \"5\" | \"6\" \"7\" | \"8\" \"9\"; \
 number  : <digit>+;                          \
 decimal : <number>+\.<number>+;              \
"
/* The URL example is essentially the same, but with a all the letters 
 * of the alphabet. The rest of the  questions in the chapter are soft
 * enough to be left to the interested reader. */



Weekly Links - Second Death Of Latin Edition


'Build Your Own Lisp' Solutions: 5.3

Q: Why are there back slashes \ at the end of the line in the grammar?


A: C will ignore a combination of a newline followed by \, which lets you format long strings more nicely. An example:

#include <stdio.h>

int main(void)
{
  /* If you count the spaces here and in the resulting output, there are
   * 11. The \ and the newline are dropped. */
  printf("this is line one \
          and this is line two\n");

  return 0;
}
this is line one           and this is line two



'Build Your Own Lisp' Solutions: 5.2
(5.1 is a fun exercise and left to the reader)

Q: Why are there back slashes \ in front of the quote marks " in the grammar?


A: So the C compiler doesn't mistake them for quotation marks which end the string.



'Build Your Own Lisp' Solutions: 4.9

Q: If _WIN32 is defined on windows, what is defined for Linux or Mac?


A: __linux__, and __APPLE__, not to mention __ANDROID__ (which is a type of __linux__, so something like __linux__ && !__ANDROID__ is possible).



'Build Your Own Lisp' Solutions: 4.8

Q: What does the preprocessor command #define do?


A: It creates a macro, which is to say, it let's you associate a word with an expression. Before compiling your code, the precompiler will replace every instance of that word with that expression. While most macros you see are quite simple, you can do pretty amazing things with them. See here for a detailed reference.



'Build Your Own Lisp' Solutions: 4.7

Q: What does the preprocessor command #ifndef do?


A: Checks if a token has already been defined earlier in the file, or in a previous file, then includes anything between it and #else or #endif.