AA-Testcode.tex 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. \chapter{Action language grammar}
  2. \label{chapt:grammar}
  3. The following is the grammar file used by the action language. It is meant for the Lark\cite{Erez2020Lark} parser library.
  4. \lstset{basicstyle=\ttfamily\footnotesize,breaklines=true}
  5. \begin{lstlisting}
  6. %import common.WS
  7. %ignore WS
  8. %import common.ESCAPED_STRING
  9. // Expression parsing
  10. // We use the same operators and operator precedence rules as Python
  11. ?expr: or_expr
  12. ?or_expr: and_expr
  13. | or_expr OR and_expr -> binary_expr
  14. ?and_expr: not_expr
  15. | and_expr AND not_expr -> binary_expr
  16. ?not_expr: comp_expr
  17. | NOT comp_expr -> unary_expr
  18. ?comp_expr: add_expr
  19. | comp_expr compare_operator add_expr -> binary_expr
  20. ?add_expr: mult_expr
  21. | add_expr add_operator mult_expr -> binary_expr
  22. ?mult_expr: unary
  23. | mult_expr mult_operator unary -> binary_expr
  24. ?unary: exponent
  25. | MINUS exponent -> unary_expr
  26. ?exponent: atom
  27. | atom EXP exponent -> binary_expr
  28. ?atom: IDENTIFIER -> identifier
  29. | "(" expr ")" -> group
  30. | literal
  31. | func_call
  32. | func_decl
  33. | array
  34. IDENTIFIER: /[A-Za-z_][A-Za-z_0-9]*/
  35. func_call: atom "(" param_list ")"
  36. param_list: ( expr ("," expr)* )? -> params
  37. func_decl: "func" params_decl stmt
  38. params_decl: ( "(" param_decl ("," param_decl)* ")" )?
  39. ?param_decl: IDENTIFIER ":" type_annot
  40. type_annot: TYPE_INT | TYPE_STR | TYPE_DUR | TYPE_FLOAT
  41. | "func" param_types? return_type? -> func_type
  42. param_types: "(" type_annot ( "," type_annot )* ")"
  43. ?return_type: "->" type_annot
  44. TYPE_INT: "int"
  45. TYPE_STR: "str"
  46. TYPE_DUR: "dur"
  47. TYPE_FLOAT: "float"
  48. array: "[" (expr ("," expr)*)? "]"
  49. ?literal: ESCAPED_STRING -> string_literal
  50. | INT -> int_literal
  51. | FLOAT -> float_literal
  52. | bool_literal
  53. | duration_literal
  54. ?compare_operator: EQ | NEQ | GT | GEQ | LT | LEQ
  55. ?add_operator: PLUS | MINUS
  56. ?mult_operator: MULT | DIV | FLOORDIV | MOD
  57. AND: "and"
  58. OR: "or"
  59. EQ: "=="
  60. NEQ: "!="
  61. GT: ">"
  62. GEQ: ">="
  63. LT: "<"
  64. LEQ: "<="
  65. PLUS: "+"
  66. MINUS: "-"
  67. MULT: "*"
  68. DIV: "/"
  69. FLOORDIV: "//"
  70. MOD: "%"
  71. EXP: "**"
  72. NOT: "not"
  73. bool_literal: TRUE | FALSE
  74. TRUE: "True"
  75. FALSE: "False"
  76. INT: /[0-9]+/
  77. FLOAT: /[0-9]+\.[0-9]*/
  78. duration_literal: (INT duration_unit)+
  79. ?duration_unit: TIME_H | TIME_M | TIME_S | TIME_MS | TIME_US | TIME_NS | TIME_PS | TIME_FS | TIME_D
  80. TIME_H: "h"
  81. TIME_M: "m"
  82. TIME_S: "s"
  83. TIME_MS: "ms"
  84. TIME_US: "us"
  85. TIME_NS: "ns"
  86. TIME_PS: "ps"
  87. TIME_FS: "fs"
  88. TIME_D: "d" // for zero-duration
  89. // Statement parsing
  90. ?block: (stmt)*
  91. ?stmt: assignment ";"
  92. | expr ";" -> expression_stmt
  93. | "return" expr ";" -> return_stmt
  94. | "{" block "}" -> block
  95. | "if" "(" expr ")" stmt ("else" stmt)? -> if_stmt
  96. assignment: lhs assign_operator expr
  97. increment: lhs "+=" expr
  98. ?lhs: IDENTIFIER -> identifier
  99. ?assign_operator: ASSIGN | INCREMENT | DECREMENT | MULTIPLY | DIVIDE
  100. ASSIGN: "="
  101. INCREMENT: "+="
  102. DECREMENT: "-="
  103. MULTIPLY: "*="
  104. DIVIDE: "/="
  105. FLOORDIVIDE: "//="
  106. COMMENT: "#" /(.)*/ "\n"
  107. %ignore COMMENT
  108. \end{lstlisting}