Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.62 KB | None | 0 0
  1. /* Pair Submission
  2. Sean Schaffer
  3. Aaditya Manirajan
  4. */
  5.  
  6. package lab3.autogen;
  7. import lab3.*;
  8. import java_cup.runtime.*;
  9.  
  10. import java.util.Vector;
  11. import java.util.Enumeration;
  12.  
  13. // $Id: jmm.cup 33 2010-08-09 21:47:19Z cytron $
  14.  
  15. /*
  16. * Java minus minus grammar
  17. *
  18. */
  19.  
  20. /*
  21. * This is a standard Java grammar, modified for our use. Many features
  22. * are noted as "extra credit". You must negotiate
  23. * the extra credit with the professor.
  24. *
  25. * Some features are added too: look for comments to that effect.
  26. * RC
  27. */
  28.  
  29. action code {:
  30.  
  31. /** Code that is included with the action blocks
  32. *
  33. */
  34.  
  35. /* Need some classes that extend AbstractNode? Here's an example */
  36. /* The TemporaryNode is just a place holder, and is good for development but
  37. * should eventually go away.
  38. */
  39.  
  40. class Example extends AbstractNode {
  41. public String getName() { return "Example"; }
  42. }
  43.  
  44. class TemporaryNode extends AbstractNode {
  45.  
  46. private String s;
  47.  
  48. public TemporaryNode(String s) {
  49. this.s = s;
  50. }
  51.  
  52. public String getName() { return s; }
  53.  
  54. }
  55.  
  56. class IntegerNode extends AbstractNode {
  57. private Integer val;
  58. public IntegerNode(Integer val) { this.val = val; }
  59. public String getName() { return "Integer " + val; }
  60. }
  61.  
  62. /* Factory methods to make nodes
  63. * Add ones here that make it easy for you.
  64. The ones given here are temporary placeholders
  65. */
  66.  
  67.  
  68. public AbstractNode makeNode(Symbol s) { return new TemporaryNode(symString.symToString[s.sym]); }
  69. public AbstractNode makeNode(String s) { return new TemporaryNode(s); }
  70. public AbstractNode makeNode(Integer i) { return new IntegerNode(i); }
  71.  
  72.  
  73. :};
  74.  
  75. /*
  76. * Almost all of these can just be Symbol types, used for parsing. Occasionally,
  77. * a terminal has semantic information of use, as was the case for number in hw2.
  78. * In those cases, declare the Symbol appropriately but you'll have to modify the
  79. * Scanner to return the right type. I've done this for integer and string types below
  80. */
  81.  
  82. terminal Symbol OP_GE, OP_LE, OP_EQ, OP_NE, OP_GT, OP_LT;
  83. terminal Symbol OP_LAND, OP_LOR;
  84. terminal Symbol INSTANCEOF;
  85. terminal Symbol HAT, TILDE;
  86. terminal Symbol BOOLEAN;
  87. terminal Symbol CLASS;
  88. terminal Symbol ELSE;
  89. terminal Symbol IF, INT;
  90. terminal Symbol NEW, NULL;
  91. terminal Symbol PRIVATE, PUBLIC;
  92. terminal Symbol RETURN;
  93. terminal Symbol STATIC, SUPER;
  94. terminal Symbol THIS;
  95. terminal Symbol VOID;
  96. terminal Symbol WHILE;
  97. terminal Symbol ASS_ADD;
  98. terminal Symbol LPAREN, RPAREN, LBRACE, RBRACE, EQUALS;
  99. terminal Symbol PERIOD, COLON, SEMICOLON, COMMA, PIPE, AND, ASTERICK;
  100. terminal Symbol PLUSOP, MINUSOP, RSLASH, PERCENT, QUESTION;
  101. terminal Symbol BANG;
  102.  
  103. terminal String IDENTIFIER, LITERAL;
  104. terminal Integer INTNUMBER;
  105.  
  106.  
  107. /* To save you typing, I've made all these AbstracNode types. You will want
  108. * to customize them as you go.
  109. */
  110.  
  111. non terminal AbstractNode CompilationUnit;
  112. non terminal AbstractNode FieldVariableDeclaration;
  113. non terminal AbstractNode MethodDeclaration;
  114. non terminal AbstractNode MethodDeclarator;
  115. non terminal AbstractNode ParameterList, Parameter;
  116. non terminal AbstractNode MethodBody, ConstructorDeclaration;
  117. non terminal AbstractNode StaticInitializer;
  118. non terminal AbstractNode Block;
  119. non terminal AbstractNode LocalVariableDeclarationsAndStatements;
  120. non terminal AbstractNode LocalVariableDeclarationOrStatement;
  121. non terminal AbstractNode LocalVariableDeclarationStatement ;
  122. non terminal AbstractNode Statement, EmptyStatement;
  123. non terminal AbstractNode MatchedStatement, UnMatchedStatement;
  124. non terminal AbstractNode ExpressionStatement;
  125. non terminal AbstractNode MatchedSelectionStatement, UnMatchedSelectionStatement;
  126. non terminal AbstractNode MatchedIterationStatement, UnMatchedIterationStatement;
  127. non terminal AbstractNode PrimaryExpression;
  128. non terminal AbstractNode NotJustName, ComplexPrimary, ComplexPrimaryNoParenthesis;
  129. non terminal AbstractNode FieldAccess, MethodCall, MethodReference;
  130. non terminal AbstractNode SpecialName, ArgumentList, AllocationExpression;
  131. non terminal AbstractNode PostfixExpression;
  132. non terminal AbstractNode UnaryExpression, LogicalUnaryExpression;
  133. non terminal AbstractNode LogicalUnaryOperator, ArithmeticUnaryOperator;
  134. non terminal AbstractNode CastExpression, MultiplicativeExpression;
  135. non terminal AbstractNode AdditiveExpression, ShiftExpression, RelationalExpression;
  136. non terminal AbstractNode EqualityExpression, AndExpression, ExclusiveOrExpression;
  137. non terminal AbstractNode InclusiveOrExpression, ConditionalAndExpression;
  138. non terminal AbstractNode ConditionalOrExpression;
  139. non terminal AbstractNode ConditionalExpression, AssignmentExpression;
  140. non terminal AbstractNode AssignmentOperator;
  141. non terminal AbstractNode Expression;
  142. non terminal AbstractNode ReturnStatement;
  143.  
  144. non terminal AbstractNode Identifier;
  145. non terminal AbstractNode Literal;
  146. non terminal AbstractNode Number;
  147. non terminal AbstractNode DeclaratorName;
  148. non terminal AbstractNode FieldVariableDeclaratorName;
  149. non terminal AbstractNode MethodDeclaratorName;
  150. non terminal AbstractNode LocalVariableDeclaratorName;
  151. non terminal AbstractNode TypeDeclarations;
  152. non terminal AbstractNode TypeDeclaration;
  153. non terminal AbstractNode ClassDeclaration;
  154. non terminal AbstractNode ClassBody;
  155. non terminal AbstractNode Modifiers;
  156. non terminal AbstractNode FieldDeclarations;
  157. non terminal AbstractNode FieldDeclaration;
  158. non terminal AbstractNode FieldVariableDeclarators;
  159. non terminal AbstractNode LocalVariableDeclarators;
  160. non terminal AbstractNode QualifiedName;
  161. non terminal AbstractNode TypeName, TypeSpecifier;
  162. non terminal AbstractNode PrimitiveType;
  163.  
  164. start with CompilationUnit;
  165.  
  166.  
  167. CompilationUnit
  168. ::= TypeDeclarations:td
  169. {:
  170. AbstractNode prog = makeNode("Program").adoptChildren(td);
  171.  
  172. System.out.println("\n\n");
  173. prog.walkTree(new PrintTree(System.out));
  174. :}
  175. ;
  176.  
  177. /*
  178. * Simple node magic to link nodes together as siblings. Covered
  179. * in class -- you have to be aware of how the list is growing
  180. * These children will be adopted by CompilationUnit rule above.
  181. */
  182.  
  183. TypeDeclarations
  184. ::= TypeDeclaration:td
  185. {: RESULT = td; :}
  186. | TypeDeclarations:tds TypeDeclaration:td
  187. {: RESULT = tds.makeSibling(td); :}
  188. ;
  189.  
  190. /*
  191. * Extra credit: interfaces, but classes are all we'll deal with by default
  192. */
  193. TypeDeclaration
  194. ::= ClassDeclaration:rhs
  195. {:
  196. RESULT = rhs;
  197. :}
  198. ;
  199.  
  200.  
  201. ClassDeclaration
  202. ::= Modifiers:mods CLASS:cl Identifier:id ClassBody:clb
  203. {:
  204. RESULT = makeNode(String.format("DECLARATION OF %s Class %s",mods ,id)).adoptChildren(clb);
  205. :}
  206. ;
  207.  
  208. /*
  209. * Process bottom-up to figure out whether the Modifier
  210. * is static or not
  211. * is public or not
  212. * A pair of booleans, like IntPair could be used, or IntPair could be used
  213. * if you know what I mean.
  214. */
  215. Modifiers
  216. ::= PUBLIC
  217. {: RESULT = makeNode("Public"); :}
  218. | PRIVATE
  219. {: RESULT = makeNode("Private"); :}
  220. | STATIC
  221. {: RESULT = makeNode("Static"); :}
  222. | Modifiers:mds PUBLIC
  223. {: RESULT = makeNode("Public " + mds); :}
  224. | Modifiers:mds PRIVATE
  225. {: RESULT = makeNode("Private " + mds); :}
  226. | Modifiers:mds STATIC
  227. {: RESULT = makeNode("Static" + mds); :}
  228. ;
  229.  
  230.  
  231. /*
  232. * Extra credit: other types
  233. */
  234. PrimitiveType
  235. ::= BOOLEAN:tok
  236. {: RESULT = makeNode(tok); :}
  237. | INT:tok
  238. {: RESULT = makeNode(tok); :}
  239. | VOID:tok
  240. {: RESULT = makeNode(tok); :}
  241. ;
  242.  
  243. /*
  244. * You need a nice structure to represent this list of identifiers.
  245. * You might consider java.util.Vector
  246. */
  247. QualifiedName
  248. ::= Identifier:id
  249. {: RESULT = id;:}
  250. | QualifiedName:qn PERIOD Identifier:id
  251. {: RESULT = makeNode(String.format("%s/%s",qn.getName(), id.getName())); :}
  252. ;
  253.  
  254. /*
  255. * In a given program, FieldDeclarations can occur in any order.
  256. * But we would like them grouped together.
  257. * So, structure your AST so that the items coming back from
  258. * FieldDeclarations are grouped by:
  259. *
  260. * fields, statics, constructors, methods, inner classes
  261. *
  262. * (run the class solution if confused)
  263. */
  264. ClassBody
  265. ::= LBRACE FieldDeclarations:fds RBRACE
  266. {:
  267. RESULT = makeNode("BODY:").adoptChildren(fds);
  268. :}
  269. | LBRACE RBRACE
  270. ;
  271.  
  272. FieldDeclarations
  273. ::= FieldDeclaration:fd
  274. {: RESULT = fd; :}
  275. | FieldDeclarations:fds FieldDeclaration:fd
  276. {: RESULT = fds.makeSibling(fd); :}
  277. ;
  278.  
  279. FieldDeclaration
  280. ::= FieldVariableDeclaration:fvd SEMICOLON
  281. {: RESULT = makeNode("FIELD: ").adoptChildren(fvd); :}
  282. | MethodDeclaration:rhs'
  283. {: RESULT = makeNode("METHOD: ").adoptChildren(rhs); :}
  284. | ConstructorDeclaration:rhs
  285. {: RESULT = makeNode("CONSTRUCTOR: ").adoptChildren(rhs); :}
  286. | StaticInitializer:rhs
  287. {: RESULT = makeNode("STATIC: ").adoptChildren(rhs); :}
  288. | ClassDeclaration
  289. {: RESULT = makeNode("INNER CLASS: "); :}
  290. ;
  291.  
  292. /*
  293. * This isn't structured so nicely for a bottom up parse. Recall
  294. * the example I did in class for Digits, where the "type" of the digits
  295. * (i.e., the base) is sitting off to the side. You'll have to do something
  296. * here to get the information where you want it, so that the declarations can
  297. * be suitably annotated with their type and modifier information.
  298. */
  299. FieldVariableDeclaration
  300. ::= Modifiers:m TypeSpecifier:t FieldVariableDeclarators:fvds
  301. {: RESULT = makeNode(String.format("%s %s", m, t)).adoptChildren(fvds); :}
  302. ;
  303.  
  304. TypeSpecifier
  305. ::= TypeName:rhs
  306. {: RESULT = rhs; :}
  307. ;
  308.  
  309. TypeName
  310. ::= PrimitiveType:rhs
  311. {: RESULT = rhs; :}
  312. | QualifiedName:rhs
  313. {: RESULT = rhs; :}
  314. ;
  315.  
  316. FieldVariableDeclarators
  317. ::= FieldVariableDeclaratorName:v
  318. {: RESULT = v; :}
  319. | FieldVariableDeclarators:fds COMMA FieldVariableDeclaratorName:v
  320. {: RESULT = fds.makeSibling(v); :}
  321. ;
  322.  
  323. /*
  324. * We require modifiers, extra credit for package stuff
  325. */
  326. MethodDeclaration
  327. ::= Modifiers:m TypeSpecifier:t MethodDeclarator:md MethodBody:rhs
  328. {:
  329. AbstractNode method = makeNode(String.format("%s %s %s",m ,t, md.getName()));
  330. AbstractNode params = makeNode("PARAMS: ").adoptChildren(md);
  331. method.adoptChildren(params);
  332. method.adoptChildren(rhs);
  333. RESULT = method;
  334. :}
  335. ;
  336.  
  337. MethodDeclarator
  338. ::= MethodDeclaratorName:dn LPAREN ParameterList:pl RPAREN
  339. {: RESULT = dn.adoptChildren(pl); :}
  340. | MethodDeclaratorName:dn LPAREN RPAREN
  341. {: RESULT = dn; :}
  342. ;
  343.  
  344. ParameterList
  345. ::= Parameter:rhs
  346. {: RESULT = rhs; :}
  347. | ParameterList:spine COMMA Parameter:rhs
  348. {: RESULT = spine.makeSibling(rhs); :}
  349. ;
  350.  
  351. Parameter
  352. ::= TypeSpecifier:t DeclaratorName:dn
  353. {: RESULT = makeNode(String.format("PARAM: %s %s",t ,dn)); :}
  354. ;
  355.  
  356. DeclaratorName
  357. ::= Identifier:in
  358. {: RESULT = in; :}
  359. ;
  360.  
  361. MethodDeclaratorName
  362. ::= Identifier:in
  363. {: RESULT = in; :}
  364. ;
  365.  
  366. FieldVariableDeclaratorName
  367. ::= Identifier:in
  368. {: RESULT = in; :}
  369. ;
  370.  
  371. LocalVariableDeclaratorName
  372. ::= Identifier:in
  373. {: RESULT = in; :}
  374. ;
  375.  
  376. MethodBody
  377. ::= Block:rhs
  378. {: RESULT = rhs; :}
  379. ;
  380.  
  381. ConstructorDeclaration
  382. ::= Modifiers:m MethodDeclarator:md Block:rhs
  383. {: AbstractNode type = m;
  384. AbstractNode paramBlock = md.adoptChildren(rhs);
  385. RESULT = type.adoptChildren(paramBlock);
  386. :}
  387. ;
  388.  
  389. StaticInitializer
  390. ::= STATIC Block:rhs
  391. {: RESULT = rhs; :}
  392. ;
  393.  
  394. /*
  395. * These can't be reorganized, because the order matters.
  396. * For example: int i; i = 5; int j = i;
  397. */
  398. Block
  399. ::= LBRACE LocalVariableDeclarationsAndStatements:stmts RBRACE
  400. {: RESULT = makeNode("BLOCK: ").adoptChildren(stmts); :}
  401. | LBRACE RBRACE
  402. ;
  403.  
  404. LocalVariableDeclarationsAndStatements
  405. ::= LocalVariableDeclarationOrStatement:rhs
  406. {: RESULT = rhs; :}
  407. | LocalVariableDeclarationsAndStatements:lvds
  408. LocalVariableDeclarationOrStatement:rhs
  409. {: RESULT = lvds.makeSibling(rhs); :}
  410. ;
  411.  
  412. LocalVariableDeclarationOrStatement
  413. ::= LocalVariableDeclarationStatement:rhs
  414. {: RESULT = rhs; :}
  415. | Statement:rhs
  416. {: RESULT = rhs; :}
  417. ;
  418.  
  419. LocalVariableDeclarationStatement
  420. ::= TypeSpecifier:t LocalVariableDeclarators:rhs SEMICOLON
  421. {: RESULT = makeNode(String.format("LOCAL %s:",t)).adoptChildren(rhs); :}
  422. | ClassDeclaration /* Inner classes */
  423. ;
  424.  
  425. LocalVariableDeclarators
  426. ::= LocalVariableDeclaratorName:v
  427. {: RESULT = v; :}
  428. | LocalVariableDeclarators:fds COMMA LocalVariableDeclaratorName:v
  429. {: RESULT = fds.makeSibling(v); :}
  430. ;
  431.  
  432. Statement
  433. ::= MatchedStatement:ms
  434. {: RESULT = ms; :}
  435. | UnMatchedStatement:us
  436. {: RESULT = us; :}
  437. ;
  438.  
  439. UnMatchedStatement
  440. ::= UnMatchedSelectionStatement:uss
  441. {: RESULT = uss; :}
  442. | UnMatchedIterationStatement:uis
  443. {: RESULT = uis; :}
  444. ;
  445.  
  446. MatchedStatement
  447. ::= EmptyStatement:es
  448. {: RESULT = es; :}
  449. | ExpressionStatement:rhs SEMICOLON
  450. {: RESULT = rhs; :}
  451. | MatchedSelectionStatement:mss
  452. {: RESULT = makeNode("IF: ").adoptChildren(mss); :}
  453. | MatchedIterationStatement:mis
  454. {: RESULT = makeNode("WHILE: ").adoptChildren(mis); :}
  455. | ReturnStatement:rs
  456. {: RESULT = makeNode("RETURN: ").adoptChildren(rs); :}
  457. | Block:rhs
  458. {: RESULT = rhs; :}
  459. ;
  460.  
  461. EmptyStatement
  462. ::= SEMICOLON
  463. {: RESULT = makeNode("Empty Statement"); :}
  464.  
  465. ;
  466.  
  467. ExpressionStatement
  468. ::= Expression:rhs
  469. {:RESULT = rhs; :}
  470. ;
  471.  
  472. /*
  473. * You will eventually have to address the shift/reduce error that
  474. * occurs when the second IF-rule is uncommented.
  475. *
  476. */
  477.  
  478. MatchedSelectionStatement
  479. ::= IF LPAREN Expression:exp RPAREN MatchedStatement:ms ELSE MatchedStatement:rhs
  480. {:
  481. AbstractNode cond = makeNode("CONDITION: ").adoptChildren(exp);
  482. AbstractNode block = makeNode("BLOCK: ").adoptChildren(ms);
  483. AbstractNode other = makeNode("ELSE: ").adoptChildren(rhs);
  484. RESULT = cond.makeSibling(block).makeSibling(other);
  485. :}
  486. ;
  487.  
  488. UnMatchedSelectionStatement
  489. ::= IF LPAREN Expression:exp RPAREN MatchedStatement:ms ELSE UnMatchedStatement:rhs
  490. {:
  491. AbstractNode cond = makeNode("CONDITION: ").adoptChildren(exp);
  492. AbstractNode block = makeNode("BLOCK: ").adoptChildren(ms);
  493. AbstractNode other = makeNode("ELSE: ").adoptChildren(rhs);
  494. RESULT = cond.makeSibling(block).makeSibling(other);
  495. :}
  496. | IF LPAREN Expression:exp RPAREN Statement:stmt
  497. {:
  498. AbstractNode cond = makeNode("CONDITION: ").adoptChildren(exp);
  499. AbstractNode block = makeNode("BLOCK: ").adoptChildren(stmt);
  500. RESULT = cond.makeSibling(block);
  501. :}
  502. ;
  503.  
  504. /*
  505. * Extra Credit: FOR statement, DO statement
  506. */
  507. MatchedIterationStatement
  508. ::= WHILE LPAREN Expression:exp RPAREN MatchedStatement:rhs
  509. {:
  510. AbstractNode cond = makeNode("CONDITION: ").adoptChildren(exp);
  511. AbstractNode block = makeNode("BLOCK: ").adoptChildren(rhs);
  512. RESULT = cond.makeSibling(block);
  513. :}
  514. ;
  515.  
  516. UnMatchedIterationStatement
  517. ::= WHILE LPAREN Expression:exp RPAREN UnMatchedStatement:rhs
  518. {:
  519. AbstractNode cond = makeNode("CONDITION: ").adoptChildren(exp);
  520. AbstractNode block = makeNode("BLOCK: ").adoptChildren(rhs);
  521. RESULT = cond.makeSibling(block);
  522. :}
  523. ;
  524.  
  525. ReturnStatement
  526. ::= RETURN Expression:exp SEMICOLON
  527. {: RESULT = exp; :}
  528. | RETURN SEMICOLON
  529. ;
  530.  
  531. PrimaryExpression
  532. ::= QualifiedName:t
  533. {: RESULT = t; :}
  534. | NotJustName:rhs
  535. {: RESULT = rhs; :}
  536. /*
  537. * You will eventually have to explain the conflicts that arise when the rule below
  538. * is uncommented.
  539. * This rule lets a block ( { .... } ) serve anywhere a primary expression could.
  540. * So you could write a = { while (h>5) h = h -k; };
  541. *
  542. * | Block:rhs
  543. */
  544. ;
  545.  
  546. NotJustName
  547. ::= SpecialName:sn
  548. {: RESULT = sn; :}
  549. | AllocationExpression:ae
  550. {: RESULT = makeNode("ALLOCATION: ").adoptChildren(ae); :}
  551. | ComplexPrimary:rhs
  552. {: RESULT = rhs; :}
  553.  
  554. ;
  555.  
  556. ComplexPrimary
  557. ::= LPAREN Expression:rhs RPAREN
  558. {: RESULT = rhs; :}
  559. | ComplexPrimaryNoParenthesis:rhs
  560. {: RESULT = rhs; :}
  561. ;
  562.  
  563. ComplexPrimaryNoParenthesis
  564. ::= Literal:rhs
  565. {: RESULT = rhs; :}
  566. | Number:rhs
  567. {: RESULT = rhs; :}
  568. | FieldAccess:fa
  569. {: RESULT = fa; :}
  570. | MethodCall:mc
  571. {: RESULT = mc; :}
  572. ;
  573.  
  574. FieldAccess
  575. ::= NotJustName:njn PERIOD Identifier:id
  576. {: RESULT = makeNode("ACCESS: ").adoptChildren(njn).adoptChildren(id); :}
  577. ;
  578.  
  579.  
  580. MethodCall
  581. ::= MethodReference:lhs LPAREN ArgumentList:rhs RPAREN
  582. {: RESULT = makeNode("CALL: ").adoptChildren(lhs).adoptChildren(rhs); :}
  583. | MethodReference:lhs LPAREN RPAREN
  584. {: RESULT = makeNode("CALL: ").adoptChildren(lhs); :}
  585. ;
  586.  
  587. MethodReference
  588. ::= ComplexPrimaryNoParenthesis:rhs
  589. {: RESULT = makeNode("METHOD: ").adoptChildren(rhs); :}
  590. | QualifiedName:rhs
  591. {: RESULT = makeNode("METHOD: ").adoptChildren(rhs); :}
  592. | SpecialName:rhs
  593. {: RESULT = makeNode("METHOD: ").adoptChildren(rhs); :}
  594. ;
  595.  
  596. SpecialName
  597. ::= THIS
  598. {: RESULT = makeNode("THIS"); :}
  599. | NULL
  600. {: RESULT = makeNode("NULL"); :}
  601. | SUPER
  602. {: RESULT = makeNode("SUPER"); :}
  603. ;
  604.  
  605. ArgumentList
  606. ::= Expression:exp
  607. {: RESULT = makeNode("ARGUMENTS: ").adoptChildren(exp); :}
  608. | ArgumentList:lhs COMMA Expression:exp
  609. {: RESULT = lhs.makeSibling(exp); :}
  610. ;
  611.  
  612. /*
  613. * Extra credit: anonymous subclasses
  614. */
  615. AllocationExpression
  616. ::= NEW TypeName:t LPAREN ArgumentList:al RPAREN
  617. {: RESULT = t.adoptChildren(al); :}
  618. | NEW TypeName:t LPAREN RPAREN
  619. {: RESULT = t; :}
  620. ;
  621.  
  622. /*
  623. * Extra credit, add post increment and decrement
  624. */
  625. PostfixExpression
  626. ::= PrimaryExpression:rhs
  627. {: RESULT = rhs; :}
  628. ;
  629.  
  630. Expression
  631. ::= AssignmentExpression:rhs
  632. {: RESULT = rhs; :}
  633. ;
  634.  
  635. /*
  636. * Here we go. Following are a bunch of rules to handle the right priority and
  637. * associativity of operators. These rules can be treated fairly uniformly
  638. * for now
  639. * However, be aware that down the road, you will want subclassees that
  640. * can distinguish
  641. * the nodes by type, so that you can generate different code for
  642. * plus vs. minus, for example.
  643. */
  644.  
  645. /*
  646. * What kind of associativity do we get for assignment expressions - why?
  647. */
  648.  
  649. AssignmentExpression
  650. ::= ConditionalExpression:rhs
  651. {: RESULT = rhs; :}
  652. | UnaryExpression:lhs AssignmentOperator:op AssignmentExpression:rhs
  653. {: RESULT = makeNode(op + ": ").adoptChildren(lhs).adoptChildren(rhs); :}
  654. ;
  655.  
  656. AssignmentOperator
  657. ::= EQUALS:tok
  658. {: RESULT = makeNode("ASSIGNMENT"); :}
  659. | ASS_ADD:tok /* There are more of these if you're interested */
  660. {: RESULT = makeNode("ASSIGNMENT ADD"); :}
  661. ;
  662.  
  663. ConditionalExpression
  664. ::= ConditionalOrExpression:coe
  665. {: RESULT = coe; :}
  666. | ConditionalOrExpression:coe QUESTION Expression:exp COLON ConditionalExpression:rhs
  667. {: RESULT = makeNode("TERNARY:").adoptChildren(coe).adoptChildren(exp).adoptChildren(rhs); :}
  668. ;
  669.  
  670. ConditionalOrExpression
  671. ::= ConditionalAndExpression:cae
  672. {: RESULT = cae; :}
  673. | ConditionalOrExpression:left OP_LOR:op ConditionalAndExpression:right /* short-circuit OR */
  674. {: RESULT = makeNode("LOR:").adoptChildren(left).adoptChildren(right); :}
  675. ;
  676.  
  677. ConditionalAndExpression
  678. ::= InclusiveOrExpression:rhs
  679. {: RESULT = rhs; :}
  680. | ConditionalAndExpression:left OP_LAND:op InclusiveOrExpression:right /* short-circuit AND */
  681. {: RESULT = makeNode("LAND:").adoptChildren(left).adoptChildren(right); :}
  682. ;
  683.  
  684. InclusiveOrExpression
  685. ::= ExclusiveOrExpression:rhs
  686. {: RESULT = rhs; :}
  687. | InclusiveOrExpression:left PIPE:op ExclusiveOrExpression:right
  688. {: RESULT = makeNode("OR:").adoptChildren(left).adoptChildren(right); :}
  689. ;
  690.  
  691. ExclusiveOrExpression
  692. ::= AndExpression:rhs
  693. {: RESULT = rhs; :}
  694. | ExclusiveOrExpression:left HAT:op AndExpression:right
  695. {: RESULT = makeNode("XOR:").adoptChildren(left).adoptChildren(right); :}
  696. ;
  697.  
  698. AndExpression
  699. ::= EqualityExpression:rhs
  700. {: RESULT = rhs; :}
  701. | AndExpression:left AND:op EqualityExpression:right
  702. {: RESULT = makeNode("AND:").adoptChildren(left).adoptChildren(right); :}
  703. ;
  704.  
  705. EqualityExpression
  706. ::= RelationalExpression:rhs
  707. {: RESULT = rhs; :}
  708. | EqualityExpression:left OP_EQ:op RelationalExpression:right
  709. {: RESULT = makeNode("EQUAL TO:").adoptChildren(left).adoptChildren(right); :}
  710. | EqualityExpression:left OP_NE:op RelationalExpression:right
  711. {: RESULT = makeNode("NOT EQUAL TO:").adoptChildren(left).adoptChildren(right); :}
  712. ;
  713.  
  714. RelationalExpression
  715. ::= ShiftExpression:rhs
  716. {: RESULT = rhs; :}
  717. | RelationalExpression:left OP_GT:op ShiftExpression:right
  718. {: RESULT = makeNode("GREATER THAN:").adoptChildren(left).adoptChildren(right); :}
  719. | RelationalExpression:left OP_LT:op ShiftExpression:right
  720. {: RESULT = makeNode("LESS THAN:").adoptChildren(left).adoptChildren(right); :}
  721. | RelationalExpression:left OP_LE:op ShiftExpression:right
  722. {: RESULT = makeNode("LESS THAN OR EQUAL:").adoptChildren(left).adoptChildren(right); :}
  723. | RelationalExpression:left OP_GE:op ShiftExpression:right
  724. {: RESULT = makeNode("GREATER THAN OR EQUAL:").adoptChildren(left).adoptChildren(right); :}
  725. | RelationalExpression:left INSTANCEOF:op TypeSpecifier:right
  726. {: RESULT = makeNode("INSTANCE OF:").adoptChildren(left).adoptChildren(right); :}
  727. ;
  728.  
  729. /*
  730. * Extra credit: shift expressions
  731. */
  732. ShiftExpression
  733. ::= AdditiveExpression:rhs
  734. {: RESULT = rhs; :}
  735. ;
  736.  
  737. AdditiveExpression
  738. ::= MultiplicativeExpression:rhs
  739. {: RESULT = rhs; :}
  740. | AdditiveExpression:lhs PLUSOP:op MultiplicativeExpression:rhs
  741. {: RESULT = makeNode("ADDITION: ").adoptChildren(lhs).adoptChildren(rhs);:}
  742. | AdditiveExpression:lhs MINUSOP:op MultiplicativeExpression:rhs
  743. {: RESULT = makeNode("SUBSTRACTION: ").adoptChildren(lhs).adoptChildren(rhs);:}
  744. ;
  745.  
  746. MultiplicativeExpression
  747. ::= CastExpression:rhs
  748. {: RESULT = rhs; :}
  749. | MultiplicativeExpression:lhs ASTERICK:op CastExpression:rhs
  750. {: RESULT = makeNode("MULTIPLICATION: ").adoptChildren(lhs).adoptChildren(rhs); :}
  751. | MultiplicativeExpression:lhs RSLASH:op CastExpression:rhs
  752. {: RESULT = makeNode("DIVISION: ").adoptChildren(lhs).adoptChildren(rhs); :}
  753. | MultiplicativeExpression:lhs PERCENT:op CastExpression:rhs /* remainder */
  754. {: RESULT = makeNode("MODULO: ").adoptChildren(lhs).adoptChildren(rhs); :}
  755. ;
  756.  
  757. /*
  758.  
  759. * Be sure to introduce an explicit cast operator
  760. */
  761. CastExpression
  762. ::= UnaryExpression:rhs /* no cast */
  763. {: RESULT = rhs; :}
  764. | LPAREN PrimitiveType:s RPAREN CastExpression:lue /* More casts coming */
  765. {: RESULT = makeNode(String.format("CAST %s:",s)).adoptChildren(lue); :}
  766. | LPAREN Expression:exp RPAREN LogicalUnaryExpression:lue /* Final cast */
  767. {: RESULT = lue.adoptChildren(exp); :}
  768. ;
  769.  
  770. /*
  771. * Extra credit: pre-increment and pre-decrement
  772. */
  773. UnaryExpression
  774. ::= LogicalUnaryExpression:rhs
  775. {: RESULT = rhs; :}
  776. | ArithmeticUnaryOperator:op CastExpression:exp
  777. {: RESULT = makeNode("ARITH"); :}
  778. ;
  779.  
  780. ArithmeticUnaryOperator
  781. ::= PLUSOP:pl
  782. {: RESULT = makeNode(pl); :}
  783. | MINUSOP:ms
  784. {: RESULT = makeNode(ms); :}
  785. ;
  786.  
  787. LogicalUnaryExpression
  788. ::= PostfixExpression:rhs
  789. {: RESULT = rhs; :}
  790. | LogicalUnaryOperator:op UnaryExpression:uexp
  791. {: RESULT = makeNode(op + ":").adoptChildren(uexp); :}
  792. ;
  793.  
  794. LogicalUnaryOperator
  795. ::= BANG:bg
  796. {: RESULT = makeNode(bg); :}
  797. | TILDE:td
  798. {: RESULT = makeNode(td); :}
  799. ;
  800.  
  801. Identifier
  802. ::= IDENTIFIER:id
  803. {: RESULT = makeNode(id); :}
  804. ;
  805.  
  806. Literal
  807. ::= LITERAL:lit
  808. {: RESULT = makeNode(lit); :}
  809. ;
  810.  
  811. Number
  812. ::= INTNUMBER:n
  813. {: RESULT = makeNode(n); :}
  814. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement