Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. grammar language;
  2.  
  3.  
  4. compilationUnit
  5. : statement+
  6. ;
  7.  
  8. statement
  9. : declaringStatement
  10. | nonDeclaringStatement
  11. ;
  12.  
  13. declaringStatement
  14. : variableDeclaration
  15. ;
  16.  
  17. nonDeclaringStatement
  18. : ifStatement
  19. | whileStatement
  20. | assignStatement
  21. | returnStatement
  22. ;
  23.  
  24. returnStatement
  25. : 'return' expression?
  26. ;
  27.  
  28. assignStatement
  29. : reference '=' expression
  30. ;
  31.  
  32. variableDeclaration
  33. : mutability=('var' | 'let') name=Identifier ':' type '=' expression
  34. ;
  35.  
  36. ifStatement
  37. : 'if' '(' condition=expression ')' '{' statement* '}' elseBlock?
  38. | 'if' '(' condition=expression ')' nonDeclaringStatement elseBlock?
  39. ;
  40.  
  41. elseBlock
  42. : 'else' '{' statement* '}'
  43. | 'else' nonDeclaringStatement
  44. ;
  45.  
  46. whileStatement
  47. : 'while' '(' condition=expression ')' '{' statement* '}'
  48. | 'while' '(' condition=expression ')' nonDeclaringStatement
  49. ;
  50.  
  51. //====Expressions====
  52.  
  53. expression
  54. : logical
  55. ;
  56.  
  57. logical
  58. : logical op=('&&' | '||') comparison
  59. | comparison
  60. ;
  61.  
  62. comparison
  63. : comparison op = ('==' | '!=' | '>' | '<' | '>=' | '<=' ) addition+
  64. | addition
  65. ;
  66.  
  67. addition
  68. : addition op=('+' | '-') multiplication
  69. | multiplication
  70. ;
  71.  
  72. multiplication
  73. : multiplication op=('*' | '/' | '%') unary
  74. | unary
  75. ;
  76.  
  77. unary
  78. : '-' primary
  79. | '!' primary
  80. | primary
  81. ;
  82.  
  83. primary
  84. : literal
  85. | '(' expression ')'
  86. | reference
  87. ;
  88.  
  89. reference
  90. : Identifier
  91. ;
  92.  
  93. literal
  94. : IntLiteral #intLiteral
  95. | FloatLiteral #floatLiteral
  96. | BoolLiteral #boolLiteral
  97. ;
  98.  
  99. IntLiteral
  100. : '0'
  101. | [1-9][0-9]*
  102. ;
  103.  
  104. FloatLiteral
  105. : [0-9]+'.'[0-9]+
  106. ;
  107.  
  108. BoolLiteral
  109. : 'true' | 'false'
  110. ;
  111.  
  112. type
  113. : primitiveType
  114. ;
  115.  
  116. primitiveType
  117. : 'int'
  118. | 'float'
  119. | 'bool'
  120. ;
  121.  
  122. Identifier
  123. : [a-zA-Z_][a-zA-Z_0-9]*
  124. ;
  125.  
  126. WS: [ \t\r\n] -> skip;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement