trieFA.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* File - trieFA.h
  2. *
  3. * This file contains code to be included in the scanner file using a
  4. * generated trie-based FA.
  5. */
  6. typedef struct { /* An entry in the FA state table */
  7. short def; /* If this state is an accepting state then*/
  8. /* this is the definition, otherwise -1. */
  9. short trans_base; /* The base index into the transition table.*/
  10. long mask; /* The transition mask. */
  11. }TrieState ;
  12. typedef struct { /* An entry in the FA transition table. */
  13. short c; /* The transition character (lowercase).*/
  14. short next_state; /* The next state. */
  15. }TrieTrans ;
  16. #ifdef UNDERLINE
  17. static long CharMask[28] = {
  18. 0x0000001, 0x0000000, 0x0000004, 0x0000008,
  19. 0x0000010, 0x0000020, 0x0000040, 0x0000080,
  20. 0x0000100, 0x0000200, 0x0000400, 0x0000800,
  21. 0x0001000, 0x0002000, 0x0004000, 0x0008000,
  22. 0x0010000, 0x0020000, 0x0040000, 0x0080000,
  23. 0x0100000, 0x0200000, 0x0400000, 0x0800000,
  24. 0x1000000, 0x2000000, 0x4000000, 0x8000000,
  25. };
  26. #define IN_MASK_RANGE(C) (islower(C) || ((C) == '_'))
  27. #define MASK_INDEX(C) ((C) - '_')
  28. #else
  29. static long CharMask[26] = {
  30. 0x0000001, 0x0000002, 0x0000004, 0x0000008,
  31. 0x0000010, 0x0000020, 0x0000040, 0x0000080,
  32. 0x0000100, 0x0000200, 0x0000400, 0x0000800,
  33. 0x0001000, 0x0002000, 0x0004000, 0x0008000,
  34. 0x0010000, 0x0020000, 0x0040000, 0x0080000,
  35. 0x0100000, 0x0200000, 0x0400000, 0x0800000,
  36. 0x1000000, 0x2000000
  37. };
  38. #define IN_MASK_RANGE(C) islower(C)
  39. #define MASK_INDEX(C) ((C) - 'a')
  40. #endif
  41. static short TFA_State;
  42. /* TFA_Init:
  43. *
  44. * Initialize the trie FA.
  45. */
  46. #define TFA_Init() TFA_State = 0
  47. /* TFA_Advance:
  48. *
  49. * Advance to the next state (or -1) on the lowercase letter c.
  50. */
  51. #define TFA_Advance(C) { \
  52. char c = C; \
  53. if (TFA_State >= 0) { \
  54. if (isupper((int)c)) \
  55. c = (char)tolower((int)c); \
  56. else if (! IN_MASK_RANGE(c)) { \
  57. TFA_State = -1; \
  58. goto TFA_done; \
  59. } \
  60. if (TrieStateTbl[TFA_State].mask & CharMask[MASK_INDEX(c)]) { \
  61. short i = TrieStateTbl[TFA_State].trans_base; \
  62. while (TrieTransTbl[i].c != c) \
  63. i++; \
  64. TFA_State = TrieTransTbl[i].next_state; \
  65. } \
  66. else \
  67. TFA_State = -1; \
  68. } \
  69. TFA_done:; \
  70. } /* end of TFA_Advance. */
  71. /* TFA_Definition:
  72. *
  73. * Return the definition (if any) associated with the current state.
  74. */
  75. #define TFA_Definition() \
  76. ((TFA_State < 0) ? -1 : TrieStateTbl[TFA_State].def)