syntax.lpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  3. Exceptions. See /LICENSE for license information.
  4. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  5. */
  6. %{
  7. #include <cstdlib>
  8. #include "executable_semantics/syntax.tab.h"
  9. %}
  10. /* Turn off legacy bits we don't need */
  11. %option noyywrap nounput nodefault noinput
  12. /* maintains the number of the current line read from input in the
  13. global variable yylineno.
  14. */
  15. %option yylineno
  16. AND "and"
  17. ARROW "->"
  18. AUTO "auto"
  19. BOOL "Bool"
  20. BREAK "break"
  21. CASE "case"
  22. CHOICE "choice"
  23. COMMENT \/\/[^\n]*\n
  24. CONTINUE "continue"
  25. DBLARROW "=>"
  26. DEFAULT "default"
  27. ELSE "else"
  28. EQUAL "=="
  29. FALSE "false"
  30. FN "fn"
  31. FNTY "fnty"
  32. IF "if"
  33. INT "Int"
  34. MATCH "match"
  35. NOT "not"
  36. OR "or"
  37. RETURN "return"
  38. STRUCT "struct"
  39. TRUE "true"
  40. TYPE "Type"
  41. VAR "var"
  42. WHILE "while"
  43. identifier [A-Za-z_][A-Za-z0-9_]*
  44. integer_literal [0-9]+
  45. %%
  46. {AND} { return AND; }
  47. {ARROW} { return ARROW; }
  48. {AUTO} { return AUTO; }
  49. {BOOL} { return BOOL; }
  50. {BREAK} { return BREAK; }
  51. {CASE} { return CASE; }
  52. {CHOICE} { return CHOICE; }
  53. {COMMENT} ;
  54. {CONTINUE} { return CONTINUE; }
  55. {DBLARROW} { return DBLARROW; }
  56. {DEFAULT} { return DEFAULT; }
  57. {ELSE} { return ELSE; }
  58. {EQUAL} { return EQUAL; }
  59. {FALSE} { return FALSE; }
  60. {FN} { return FN; }
  61. {FNTY} { return FNTY; }
  62. {IF} { return IF; }
  63. {INT} { return INT; }
  64. {MATCH} { return MATCH; }
  65. {NOT} { return NOT; }
  66. {OR} { return OR; }
  67. {RETURN} { return RETURN; }
  68. {STRUCT} { return STRUCT; }
  69. {TRUE} { return TRUE; }
  70. {TYPE} { return TYPE; }
  71. {VAR} { return VAR; }
  72. {WHILE} { return WHILE; }
  73. {identifier} {
  74. int n = strlen(yytext);
  75. yylval.str = reinterpret_cast<char*>(malloc((n + 1) * sizeof(char)));
  76. strncpy(yylval.str, yytext, n + 1);
  77. return identifier;
  78. }
  79. {integer_literal} {
  80. yylval.num = atof(yytext);
  81. return integer_literal;
  82. }
  83. [ \t\n]+ ;
  84. . { return yytext[0]; }
  85. %%