Advertisement
Guest User

Untitled

a guest
Dec 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 2.05 KB | None | 0 0
  1. (* Scanner for AP++ compiler *)
  2.  
  3. { open Parser }
  4.  
  5. let digit = ['0' - '9']
  6. let digits = digit+
  7. let esc_regex = '\\' ['\\' ''' '"' 'n' 'r' 't']
  8. let ascii_regex = ([' '-'!' '#'-'[' ']'-'~'])
  9.  
  10. rule token = parse
  11.  [' ' '\t' '\r' '\n'] { token lexbuf } (* Whitespace *)
  12. | "/*"    { comment lexbuf }            (* Multi-line Comment *)
  13. | "//" { line_comment lexbuf }          (* Single-line Comment *)
  14. | '('     { LPAREN }
  15. | ')'     { RPAREN }
  16. | '{'     { LBRACE }
  17. | '}'     { RBRACE }
  18. | '['     { LBRACK }
  19. | ']'     { RBRACK }
  20. | ':'     { COLON }
  21. | ';'     { SEMICOLON }
  22. | ','     { COMMA }
  23. | "++"    { PLUSPLUS }
  24. | "--"    { MINUSMINUS }
  25. | '+'     { PLUS }
  26. | '-'     { MINUS }
  27. | '*'     { TIMES }
  28. | '/'     { DIVIDE }
  29. | '%'     { MOD }
  30. | '='     { ASSIGN }
  31. | "=="    { EQ }
  32. | "!="    { NEQ }
  33. | "&&"    { AND }
  34. | "||"    { OR }
  35. | '!'     { NOT }
  36. | "<="    { LEQ }
  37. | ">="    { GEQ }
  38. | "if"    { IF }
  39. | "else"  { ELSE }
  40. | "while" { WHILE }
  41. | "for"   { FOR }
  42. | "return" { RETURN }
  43. | "int"   { INT }
  44. | "bool"  { BOOL }
  45. | "float" { FLOAT }
  46. | "void"  { VOID }
  47. | "<"     { LT }
  48. | ">"     { GT }
  49. | "#"     { HASH }
  50. | "list_push"   { LIST_PUSH }
  51. | "list_get"    { LIST_GET }
  52. | "list_set"    { LIST_SET }
  53. | "list_pop"    { LIST_POP }
  54. | "list_size"   { LIST_SIZE }
  55. | "list_slice"  { LIST_SLICE }
  56. | "list_clear"  { LIST_CLEAR }
  57. | "list_rev"    { LIST_REVERSE }
  58. | "list_insert" { LIST_INSERT }
  59. | "list_remove" { LIST_REMOVE }
  60. | "list_find"   { LIST_FIND }
  61. | "list"        { LIST }
  62. | digits as lit { ILITERAL(int_of_string lit) }
  63. | "true"    { BLITERAL(true) }
  64. | "false"   { BLITERAL(false) }
  65. | digits '.'  digit* ( ['e' 'E'] ['+' '-']? digits )? as lit { FLITERAL(float_of_string lit) }
  66. | '"' ((ascii_regex | esc_regex)* as lit)'"' { SLITERAL(lit) }
  67. | ['a'-'z' 'A'-'Z']['a'-'z' 'A'-'Z' '0'-'9' '_']* as id_name { ID(id_name) }
  68. | eof     { EOF }
  69. | _ as char { raise (Failure("illegal character " ^ Char.escaped char)) }
  70.  
  71. and line_comment = parse
  72. '\n' { token lexbuf }
  73. | _ { line_comment lexbuf }
  74.  
  75. and comment = parse
  76.  "*/"    { token lexbuf }
  77. | _       { comment lexbuf }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement