lexer.mll 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. {
  2. open Lexing
  3. open Parser
  4. exception SyntaxError of string
  5. let next_line lexbuf =
  6. let pos = lexbuf.lex_curr_p in
  7. lexbuf.lex_curr_p <- {
  8. pos with pos_bol = lexbuf.lex_curr_pos;
  9. pos_lnum = pos.pos_lnum + 1
  10. }
  11. let linenum = ref 0
  12. let filename = ref ""
  13. }
  14. rule token = parse
  15. | '#'' '+['0'-'9']+' '+'"'[^'"']+'"'(' '+['1'-'4'])*'\n' as marker {
  16. (*
  17. * The C preprocessor inserts so called ``line markers'' into the output.
  18. * These markers have the following form:
  19. *
  20. * # <linenum> <filename> <flags>
  21. *
  22. * This marker indicates that all lines after this marker up to the next
  23. * marker come from the file <filename> starting at line <linenum>. After
  24. * the file name can be zero or more flags, these flags are 1, 2, 3, 4.
  25. * These flags can be ignored.
  26. *)
  27. Scanf.sscanf marker "# %d \"%s\"" (fun i s -> linenum := i - 1;
  28. filename := String.copy s);
  29. token lexbuf
  30. }
  31. (*| ['('')''['']''{''}'';'','] as literal { literal }*)
  32. | '(' { LPAREN }
  33. | ')' { RPAREN }
  34. | '[' { LBRACK }
  35. | ']' { RBRACK }
  36. | '{' { LBRACE }
  37. | '}' { RBRACE }
  38. | ';' { SEMICOL }
  39. | ',' { COMMA }
  40. | '=' { ASSIGN }
  41. | '!' { NOT }
  42. | '+' { ADD }
  43. | '-' { SUB }
  44. | '*' { MUL }
  45. | '/' { DIV }
  46. | '%' { MOD }
  47. | "<=" { LE }
  48. | "<" { LT }
  49. | ">=" { GE }
  50. | ">" { GT }
  51. | "==" { EQ }
  52. | "!=" { NE }
  53. | "&&" { AND }
  54. | "||" { OR }
  55. | "if" { IF }
  56. | "else" { ELSE }
  57. | "do" { DO }
  58. | "while" { WHILE }
  59. | "for" { FOR }
  60. | "return" { RETURN }
  61. | "extern" { EXTERN }
  62. | "export" { EXPORT }
  63. | "int" { INT }
  64. | "bool" { BOOL }
  65. | "float" { FLOAT }
  66. | "void" { VOID }
  67. | "true" { BOOL_CONST true }
  68. | "false" { BOOL_CONST false }
  69. | ['0'-'9']+ as i { INT_CONST (int_of_string i) }
  70. | ['0'-'9']+'.'['0'-'9']+ as f { FLOAT_CONST (float_of_string f) }
  71. | ['A'-'Z''a'-'z']['A'-'Z''a'-'z''0'-'9''_']* as id { ID id }
  72. | '\r'|'\n'|"\r\n" { next_line lexbuf; token lexbuf }
  73. | [' ''\t']+ { token lexbuf }
  74. | "//"_* | "/*"_*"*/" { token lexbuf }
  75. | eof { EOF }
  76. | _ as chr { raise (SyntaxError ("Unexpected char: " ^ Char.escaped chr)) }