Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. // package the generated Java class lives in; no need to change this
  2. %package "parser";
  3.  
  4. // name of the generated Java class; no need to change this
  5. %class "Parser";
  6.  
  7. // no need to change this either
  8. %embed {:
  9. // turn off automated error recovery
  10. @Override
  11. protected void recoverFromError(Symbol token, TokenStream in) throws java.io.IOException, Exception {
  12. super.recoverFromError(new Symbol(0), in);
  13. }
  14. :};
  15.  
  16. // the list of all terminals; no need to change this
  17. %terminals MODULE, INT, VOID, IF, ELSE, WHILE, RETURN, BREAK, BOOLEAN, PUBLIC, TRUE, FALSE,
  18. INT_LITERAL, STRING_LITERAL, ID, TYPE, IMPORT,
  19. PLUS, MINUS, TIMES, DIV, MOD, EQEQ, NEQ, LEQ, GEQ, LT, GT,
  20. LBRACKET, RPAREN, COMMA, RCURLY, LCURLY, SEMICOLON, RBRACKET, LPAREN, EQL;
  21.  
  22. // declaration of start symbol; no need to change this
  23. %goal Module;
  24.  
  25. // temporary declaration, you can remove this once you have written all other rules
  26. // %goal Dummy;
  27.  
  28. /* TODO: Flesh out the rule for Module, and add rules for other nonterminals. Here is an example
  29. of a rule you may want to add:
  30.  
  31. Accessibility = PUBLIC
  32. |
  33. ;
  34. */
  35.  
  36. Module = MODULE ID LCURLY Imports Declarations RCURLY
  37. ;
  38.  
  39. Imports = Import Imports
  40. |
  41. ;
  42.  
  43. Import = IMPORT ID SEMICOLON
  44. ;
  45.  
  46. Declarations = Declaration Declarations
  47. |
  48. ;
  49.  
  50. Declaration = FunctionDeclaration
  51. | FieldDeclaration
  52. | TypeDeclaration
  53. ;
  54.  
  55. FunctionDeclaration = AccessId TypeName ID LPAREN ParameterList RPAREN LCURLY Statements RCURLY
  56. ;
  57.  
  58. FieldDeclaration = AccessId TypeName ID SEMICOLON
  59. ;
  60.  
  61. TypeDeclaration = AccessId TYPE ID EQL STRING_LITERAL SEMICOLON
  62. ;
  63.  
  64. AccessId = PUBLIC
  65. |
  66. ;
  67.  
  68. TypeName = PrimitiveType
  69. | ArrayType
  70. | ID
  71. ;
  72.  
  73. PrimitiveType = VOID
  74. | BOOLEAN
  75. | INT
  76. ;
  77.  
  78. // Resolving Shift-Reduce conflict by selecting (LBRACKET: SHIFT; go to 148)
  79. // over (LBRACKET: REDUCE TypeName = ID) using precedence.
  80. // LALR conflict
  81.  
  82. ArrayType = ID LBRACKET RBRACKET
  83. | ArrayType LBRACKET RBRACKET
  84. | PrimitiveType LBRACKET RBRACKET
  85. ;
  86.  
  87. Parameter = TypeName ID
  88. ;
  89.  
  90. ParameterList = Parameter ParameterY
  91. |
  92. ;
  93.  
  94. ParameterY = COMMA Parameter ParameterY
  95. |
  96. ;
  97.  
  98. Statements = Statement Statements
  99. |
  100. ;
  101.  
  102. Statement = LocalVariableDeclaration
  103. | BlockStatement
  104. | IfStatement
  105. | WhileStatement
  106. | BreakStatement
  107. | ReturnStatement
  108. | ExpressionStatement
  109. ;
  110.  
  111. LocalVariableDeclaration = TypeName ID SEMICOLON
  112. ;
  113.  
  114. BlockStatement = LCURLY Statements RCURLY
  115. ;
  116.  
  117. // Resolving Shift-Reduce conflict by selecting (ELSE: SHIFT; go to 84)
  118. // over (ELSE: REDUCE IfStatement = IF LPAREN Expression RPAREN Statement) using precedence.
  119. IfStatement = IF LPAREN Expression RPAREN Statement
  120. | IF LPAREN Expression RPAREN Statement ELSE Statement
  121. ;
  122.  
  123. WhileStatement = WHILE LPAREN Expression RPAREN Statement
  124. ;
  125.  
  126. BreakStatement = BREAK SEMICOLON
  127. ;
  128.  
  129. // Common prefix
  130. // Resolving Shift-Reduce conflict by favouring shift over reduce.
  131. ReturnStatement = RETURN SEMICOLON
  132. | RETURN Expression SEMICOLON
  133. ;
  134.  
  135. ExpressionStatement = Expression SEMICOLON
  136. ;
  137.  
  138. Expression = Assignment
  139. | RhsExpression
  140. ;
  141.  
  142. Assignment = LhsExpression EQL Expression
  143. ;
  144.  
  145. LhsExpression = ID LhsExpressionY
  146. ;
  147.  
  148. LhsExpressionY = LBRACKET Expression RBRACKET LhsExpressionY
  149. |
  150. ;
  151.  
  152. // Common prefix
  153. // Resolving Shift-Reduce conflict by favouring shift over reduce
  154. RhsExpression = ArithmeticExpression
  155. | ArithmeticExpression ComparisonOperator ArithmeticExpression
  156. ;
  157.  
  158. ComparisonOperator = EQEQ
  159. | NEQ
  160. | LT
  161. | LEQ
  162. | GT
  163. | GEQ
  164. ;
  165.  
  166. ArithmeticExpression = Term ArithmeticExpressionY
  167. ;
  168.  
  169. ArithmeticExpressionY = AdditiveOperator Term ArithmeticExpressionY
  170. |
  171. ;
  172.  
  173. AdditiveOperator = PLUS
  174. | MINUS
  175. ;
  176.  
  177. Term = Factor TermY
  178. ;
  179.  
  180. TermY = MultiplicativeOperator Factor TermY
  181. |
  182. ;
  183.  
  184. MultiplicativeOperator = TIMES
  185. | DIV
  186. | MOD
  187. ;
  188.  
  189. Factor = MINUS Factor
  190. | PrimaryFactor
  191. ;
  192.  
  193. PrimaryFactor = LhsExpression
  194. | FunctionCall
  195. | ArrayExpression
  196. | STRING_LITERAL
  197. | INT_LITERAL
  198. | Boolean
  199. | LPAREN Expression RPAREN
  200. ;
  201.  
  202. Boolean = TRUE
  203. | FALSE
  204. ;
  205.  
  206. FunctionCall = ID LPAREN ExpressionListEmpty RPAREN
  207. ;
  208.  
  209. ExpressionListEmpty = ExpressionList
  210. |
  211. ;
  212.  
  213. ExpressionList = Expression ExpressionListY
  214. ;
  215.  
  216. ExpressionListY = COMMA Expression ExpressionListY
  217. |
  218. ;
  219.  
  220. ArrayExpression = LBRACKET ExpressionList RBRACKET
  221. ;
  222.  
  223.  
  224.  
  225.  
  226.  
  227. /**/
  228.  
  229. /* Dummy rule to make the lexer compile. Remove this once you have written all other rules. */
  230. // Dummy = MODULE INT VOID IF ELSE WHILE RETURN BREAK BOOLEAN PUBLIC TRUE FALSE INT_LITERAL STRING_LITERAL ID TYPE IMPORT
  231. // PLUS MINUS TIMES DIV MOD EQEQ NEQ LEQ GEQ LT GT LBRACKET RPAREN COMMA RCURLY LCURLY SEMICOLON RBRACKET LPAREN EQL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement