Advertisement
Guest User

Untitled

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