syntax.lpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. %option yylineno
  11. AND "and"
  12. ARROW "->"
  13. AUTO "auto"
  14. BOOL "Bool"
  15. BREAK "break"
  16. CASE "case"
  17. CHOICE "choice"
  18. COMMENT \/\/[^\n]*\n
  19. CONTINUE "continue"
  20. DBLARROW "=>"
  21. DEFAULT "default"
  22. ELSE "else"
  23. EQUAL "=="
  24. FALSE "false"
  25. FN "fn"
  26. FNTY "fnty"
  27. IF "if"
  28. INT "Int"
  29. MATCH "match"
  30. NOT "not"
  31. OR "or"
  32. RETURN "return"
  33. STRUCT "struct"
  34. TRUE "true"
  35. TYPE "Type"
  36. VAR "var"
  37. WHILE "while"
  38. identifier [A-Za-z_][A-Za-z0-9_]*
  39. integer_literal [0-9]+
  40. %%
  41. {AND} { return AND; }
  42. {ARROW} { return ARROW; }
  43. {AUTO} { return AUTO; }
  44. {BOOL} { return BOOL; }
  45. {BREAK} { return BREAK; }
  46. {CASE} { return CASE; }
  47. {CHOICE} { return CHOICE; }
  48. {COMMENT} ;
  49. {CONTINUE} { return CONTINUE; }
  50. {DBLARROW} { return DBLARROW; }
  51. {DEFAULT} { return DEFAULT; }
  52. {ELSE} { return ELSE; }
  53. {EQUAL} { return EQUAL; }
  54. {FALSE} { return FALSE; }
  55. {FN} { return FN; }
  56. {FNTY} { return FNTY; }
  57. {IF} { return IF; }
  58. {INT} { return INT; }
  59. {MATCH} { return MATCH; }
  60. {NOT} { return NOT; }
  61. {OR} { return OR; }
  62. {RETURN} { return RETURN; }
  63. {STRUCT} { return STRUCT; }
  64. {TRUE} { return TRUE; }
  65. {TYPE} { return TYPE; }
  66. {VAR} { return VAR; }
  67. {WHILE} { return WHILE; }
  68. {identifier} {
  69. int n = strlen(yytext);
  70. yylval.str = reinterpret_cast<char*>(malloc((n + 1) * sizeof(char)));
  71. strncpy(yylval.str, yytext, n + 1);
  72. return identifier;
  73. }
  74. {integer_literal} {
  75. yylval.num = atof(yytext);
  76. return integer_literal;
  77. }
  78. [ \t\n]+ ;
  79. . { return yytext[0]; }
  80. %%
  81. int yywrap() { return 1; }