Advertisement
Mikescher

oberon.scan [1]

Nov 26th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.03 KB | None | 0 0
  1. SCANNER oberon_scan
  2.  
  3. EXPORT {
  4. /* code to be put intp Scanner.h */
  5. # include "Position.h"
  6.  
  7. /* Token Attributes.
  8.  * For each token with user defined attributes, we need a typedef for the
  9.  * token attributes.
  10.  * LPP extracts the token-attribute declaration from the parser specification.
  11.  * They are inserted here.
  12.  */
  13. INSERT tScanAttribute
  14. }
  15.  
  16. GLOBAL {
  17. /* code to be put into Scanner.c */
  18. # include <stdlib.h>
  19. # include "Errors.h"
  20.  
  21. /* Insert the routine computing "error-values" of attributes, in case the
  22.  * parser decides during error repair to insert a token.
  23.  */
  24. INSERT ErrorAttribute
  25. }
  26.  
  27. LOCAL {
  28. /* user-defined local variables of the generated GetToken routine */
  29. # define MAX_STRING_LEN 2048
  30. char string[MAX_STRING_LEN];
  31. int len;
  32. }
  33.  
  34. DEFAULT {
  35.   /* What happens if no scanner rule matches the input */
  36.   MessageI ("Illegal character",
  37.         xxError, oberon_scan_Attribute.Position,
  38.         xxCharacter, oberon_scan_TokenPtr);
  39. }
  40.  
  41. EOF {
  42.   /* What should be done if the end-of-input-file has been reached? */
  43.  
  44.   /* E.g.: check hat strings and comments are closed. */
  45.   switch (yyStartState) {
  46.   case STD:
  47.     /* ok */
  48.     break;
  49.   default:
  50.     Message ("OOPS: that should not happen!!",
  51.          xxFatal, oberon_scan_Attribute.Position);
  52.     break;
  53.   }
  54.  
  55.   /* implicit: return the EofToken */
  56. }
  57.  
  58. DEFINE /* some abbreviations */
  59.   digit     = {0-9}     .
  60.   hexdigit  = {0-9A-F}  .
  61.   character = {A-Za-z}  .
  62.   strchrs = - {"\\\n\r\f} .
  63.  A = {Aa}.
  64.  B = {Bb}.
  65.  C = {Cc}.
  66.  D = {Dd}.
  67.  E = {Ee}.
  68.  F = {Ff}.
  69.  G = {Gg}.
  70.  H = {Hh}.
  71.  I = {Ii}.
  72.  J = {Jj}.
  73.  K = {Kk}.
  74.  L = {Ll}.
  75.  M = {Mm}.
  76.  N = {Nn}.
  77.  O = {Oo}.
  78.  P = {Pp}.
  79.  Q = {Qq}.
  80.  R = {Rr}.
  81.  S = {Ss}.
  82.  T = {Tt}.
  83.  U = {Uu}.
  84.  V = {Vv}.
  85.  W = {Ww}.
  86.  X = {Xx}.
  87.  Y = {Yy}.
  88.  Z = {Zz}.
  89.  
  90. START OBSTR
  91.  
  92. RULES
  93.  
  94. /*####################################*/
  95. INSERT RULES #STD#
  96. /*####################################*/
  97.  
  98. /**********************************************************************/
  99.  
  100. #STD# character (character | digit)* :
  101. {
  102.  oberon_scan_Attribute.tok_identifier.Value = malloc(oberon_scan_TokenLength + 1);
  103.  oberon_scan_GetWord (oberon_scan_Attribute.tok_identifier.Value);
  104.  return tok_identifier;
  105. }
  106.  
  107. /**********************************************************************/
  108.  
  109. /* integer */
  110. #STD# digit* | digit hexdigit* "H":
  111. {
  112.  oberon_scan_GetWord (string);
  113.  oberon_scan_Attribute.int_const.Value = atol (string);
  114.  return int_const;
  115. }
  116.  
  117. /**********************************************************************/
  118.  
  119. /* real digit */
  120. #STD# digit digit* "." digit* (("E"|"D") ("+"|"-")? digit+ )?:
  121. {
  122.  oberon_scan_GetWord (string);
  123.  oberon_scan_Attribute.real_const.Value = atof (string);
  124.  return real_const;
  125. }
  126.  
  127. /**********************************************************************/
  128.  
  129. /* char constant */
  130. #STD# digit hexdigit* "X" :
  131. {
  132.  oberon_scan_GetWord (string);
  133.  oberon_scan_Attribute.char_const.Value = (char) strtol(string, (void*)0, 0);
  134.  return char_const;
  135. }
  136.  
  137. #STD# \" character \" :
  138. {
  139.  oberon_scan_GetWord (string);
  140.  oberon_scan_Attribute.char_const.Value = string[1];         /* Not sure ... */
  141.  return char_const;
  142. }
  143.  
  144. /**********************************************************************/
  145.  
  146. /* string */
  147. #STD# \" :
  148.  {
  149.    yyStart (OBSTR);
  150.    len = 0;
  151.  }
  152. #OBSTR# strchrs* :
  153.  {
  154.    len += oberon_scan_GetWord (&string[len]);
  155.  }
  156. #OBSTR# \" :
  157.  {
  158.    yyStart(STD);
  159.    string[len] = '\0';
  160.    oberon_scan_Attribute.string_const.Value = malloc (len);
  161.    strcpy (oberon_scan_Attribute.string_const.Value, string);
  162.    return string_const;
  163.  }
  164. #OBSTR# \\ \" :
  165.  {
  166.    string[len++] = '"';
  167.  }
  168. #OBSTR# \\ n :
  169.  {
  170.    string[len++] = '\n';
  171.  }
  172. #OBSTR# \\ :
  173.  {
  174.    string[len++] = '\\';
  175.  }
  176. #OBSTR# \n :
  177.  {
  178.    yyStart(STD);
  179.    yyEol (0);
  180.    oberon_scan_Attribute.string_const.Value = "";
  181.    return string_const;
  182.  }
  183.  
  184. /**********************************************************************/
  185.  
  186.  
  187.  
  188. /**********************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement