java compilerhttp://www.daniweb.com/forums/dani-images/icons/help.gif

Reply

Join Date: Dec 2007
Posts: 25
Reputation: sara_84 is an unknown quantity at this point 
Solved Threads: 0
sara_84 sara_84 is offline Offline
Light Poster

i need java code convert if to switch and for to while

 
0
  #1
Dec 18th, 2007
please, i need a java code that convert if statement to switch statement (Contrarily)
and convert for statement to while statement (Contrarily). ( deal with nested statement)
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,377
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: i need java code convert if to switch and for to while

 
0
  #2
Dec 18th, 2007
What are you talking about?

You want code to change code? Are you attempting to build some sort of parser or something?

Also, there is nothing "contrary" about the comparison between if/switch and for/while. Those statement groupings do the same things, not "contrary" things, which makes your post even less understandable.
Last edited by masijade; Dec 18th, 2007 at 3:43 am. Reason: typo
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 25
Reputation: sara_84 is an unknown quantity at this point 
Solved Threads: 0
sara_84 sara_84 is offline Offline
Light Poster

Re: i need java code convert if to switch and for to while

 
0
  #3
Dec 18th, 2007
it is a project, i need a program using java that when i enter if statement covert it to switch and when i enterd switch statement convet it to if (deal with nested loop), do the same with for and while statement
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,377
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: i need java code convert if to switch and for to while

 
0
  #4
Dec 18th, 2007
Well, what do you have so far, and what problems are you having with it. No one is going to write it for you. We will help you fix your mistakes, but not do it for you.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 25
Reputation: sara_84 is an unknown quantity at this point 
Solved Threads: 0
sara_84 sara_84 is offline Offline
Light Poster

Re: i need java code convert if to switch and for to while

 
0
  #5
Dec 26th, 2007
this is code,
  1. package dex.compiler.parser;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Collections;
  6. import java.util.List;
  7.  
  8. import dex.compiler.model.base.Place;
  9. import dex.compiler.model.expression.Call;
  10. import dex.compiler.model.expression.DirectCall;
  11. import dex.compiler.model.expression.Expression;
  12. import dex.compiler.model.expression.IdentifierExpression;
  13. import dex.compiler.model.expression.Infix;
  14. import dex.compiler.model.statement.Assignment;
  15. import dex.compiler.model.statement.Block;
  16. import dex.compiler.model.statement.CallStatement;
  17. import dex.compiler.model.statement.CatchBlock;
  18. import dex.compiler.model.statement.Comment;
  19. import dex.compiler.model.statement.Declaration;
  20. import dex.compiler.model.statement.DecrementStatement;
  21. import dex.compiler.model.statement.ForStatement;
  22. import dex.compiler.model.statement.IfStatement;
  23. import dex.compiler.model.statement.IncrementStatement;
  24. import dex.compiler.model.statement.ReturnStatement;
  25. import dex.compiler.model.statement.Statement;
  26. import dex.compiler.model.statement.SwitchCase;
  27. import dex.compiler.model.statement.SwitchStatement;
  28. import dex.compiler.model.statement.ThrowStatement;
  29. import dex.compiler.model.statement.TryStatement;
  30. import dex.compiler.model.statement.WhileStatement;
  31. import dex.compiler.model.type.BasicTypeNode;
  32. import dex.compiler.model.type.TypeNode;
  33.  
  34.  
  35. /**
  36.  * A parser for statement objects.
  37.  */
  38. class StatementParser extends ExpressionParser {
  39.  
  40.  
  41. /**
  42. * Character sequences that indicate a type tail.
  43. */
  44. final private static List<String> TYPE_TAILS = Collections.unmodifiableList(Arrays.asList(
  45. new String[] { "[]", "<(" }
  46. ));
  47.  
  48.  
  49. /**
  50. * The keywords that begin blocks in a switch statement.
  51. */
  52. final private static List<String> SWITCHES = Collections.unmodifiableList(Arrays.asList(
  53. new String[] { "case", "default" }
  54. ));
  55.  
  56.  
  57.  
  58. /**
  59. * Constructs a new StatementParser.
  60. *
  61. * @param config the configuration for the new parser
  62. */
  63. public StatementParser(ParserConfig config) {
  64. super(config);
  65. }
  66.  
  67.  
  68. /**
  69. * Parses a declaration. Expects a type followed by an identifier
  70. * and optional initializer.
  71. *
  72. * @return the parsed declaration
  73. */
  74. public Statement parseDeclaration() {
  75. TypeNode t = parseType();
  76. return parseDeclaration(t);
  77. }
  78.  
  79.  
  80. /**
  81. * Parses a declaration whose type was already parsed.
  82. *
  83. * @param t the previously parsed type
  84. * @return the newly parsed declaration statement
  85. */
  86. public Statement parseDeclaration(TypeNode t) {
  87. String id = parseIdentifier();
  88. Expression initializer = null;
  89. char ch = input.skipAndPeek();
  90. if (ch == '=') {
  91. input.expect('=');
  92. initializer = parseExpression();
  93. }
  94. return new Declaration(input.getPlace(), t, id, initializer);
  95. }
  96.  
  97.  
  98. /**
  99. * Parses a statement.
  100. *
  101. * @return the parsed statement
  102. */
  103. public Statement parseStatement() {
  104.  
  105. // New local scope?
  106. if (input.skipAndPeek() == '{') {
  107. return parseBlock();
  108. }
  109.  
  110. if (input.skipAndPeek() == '/') {
  111. return parseComment();
  112. }
  113.  
  114. // Check all the control flow reserved words...
  115.  
  116. if (testKeyword("while")) {
  117. return parseWhile();
  118. }
  119.  
  120. if (testKeyword("for")) {
  121. return parseFor();
  122. }
  123.  
  124. if (testKeyword("if")) {
  125. return parseIf();
  126. }
  127.  
  128. if (testKeyword("switch")) {
  129. return parseSwitch();
  130. }
  131.  
  132. if (testKeyword("try")) {
  133. return parseTry();
  134. }
  135.  
  136. if (testKeyword("return")) {
  137. return parseReturn();
  138. }
  139.  
  140. if (testKeyword("void")) {
  141. return parseVoid();
  142. }
  143.  
  144. if (testKeyword("throw")) {
  145. return parseThrow();
  146. }
  147.  
  148. // Okay, at this point we know it's not a reserved word.
  149.  
  150. // The next thing in the stream might be a type.
  151. // We can quickly tell whether it's a local user type.
  152. char ch = input.skipAndPeek();
  153. if (ch == '#') {
  154. return parseDeclaration();
  155. }
  156.  
  157. // Okay, not an obvious type, anyway.
  158. // But the next thing in the stream MUST be an identifier.
  159. // The identifier can be a type (signifying a declaration),
  160. // or a function call, or a variable.
  161. String id = parseIdentifier();
  162.  
  163. // If [] follows the id, that indicates a dynamic array type.
  164. // If <( follows the id, that indicates a function type.
  165. // If a type is indicated, then this statement must be a declaration.
  166. if (input.test(TYPE_TAILS, AnyBreaks.INSTANCE) != null) {
  167. TypeNode t = new BasicTypeNode(input.getPlace(), false, getName(id));
  168. return parseDeclaration(parseTypeTail(t));
  169. }
  170.  
  171. // If ( follows the id, that indicates a function call.
  172. if (input.skipAndPeek() == '(') {
  173. DirectCall dc = new DirectCall(input.getPlace(), id, parseExpressionList());
  174. return new CallStatement(input.getPlace(), dc);
  175. }
  176.  
  177. ch = input.skipAndPeek();
  178.  
  179. // An identifier followed by another identifier is a declaration
  180. // (identifier 1 is the type, followed by the local variable name)
  181. if (Character.isJavaIdentifierStart(ch)) {
  182. BasicTypeNode t = new BasicTypeNode(input.getPlace(), false, getName(id));
  183. return parseDeclaration(t);
  184. }
  185.  
  186. Expression lvalue;
  187.  
  188. // Not a block.
  189. // Not a control statement.
  190. // Not a declaration.
  191. // It must be an assignment.
  192. lvalue = new IdentifierExpression(input.getPlace(), id);
  193. lvalue = parseOperandTail(lvalue);
  194.  
  195. // ...except I lied, the above parseOperandTail may have returned
  196. // an expression chain that ends in a Call, eg:
  197. //
  198. // f.x.y[2].foo()
  199. //
  200. // Since you can't assign to the result of a function call,
  201. // we want to convert that call expression to a statement
  202. // and return that.
  203. if (lvalue instanceof Call) {
  204. Call call = (Call)lvalue;
  205. return new CallStatement(input.getPlace(), call);
  206. }
  207.  
  208. // Aren't you glad I did this by hand instead of using bison?
  209.  
  210. // It must be an assignment. We've got the lvalue; now we
  211. // need the assignment operator.
  212.  
  213. // Test for special case of ++
  214. if (input.test("++", SymbolBreaks.INSTANCE)) {
  215. input.expect("++", SymbolBreaks.INSTANCE);
  216. return new IncrementStatement(input.getPlace(), lvalue);
  217. }
  218.  
  219. // Also --
  220. if (input.test("--", SymbolBreaks.INSTANCE)) {
  221. input.expect("--", SymbolBreaks.INSTANCE);
  222. return new DecrementStatement(input.getPlace(), lvalue);
  223. }
  224.  
  225. Infix assignmentOp;
  226.  
  227. ch = input.skipAndPeek();
  228. if (ch == '=') {
  229. assignmentOp = null; // null value represents straight assignment
  230. } else {
  231. String symbol = pickOperator();
  232. if (symbol == null) {
  233. raise("Expected assignment operator.");
  234. }
  235. assignmentOp = Infix.get(symbol);
  236. }
  237.  
  238.  
  239. // Otherwise, whatever operator we just parsed must be follwed
  240. // by an = sign since it's an assignment.
  241. // Eg, if we parsed + then we expect +=
  242. input.expect('=');
  243.  
  244. Expression rvalue = parseExpression();
  245.  
  246. return new Assignment(input.getPlace(), assignmentOp, lvalue, rvalue);
  247. }
  248.  
  249.  
  250. /**
  251. * Parses a return statement.
  252. *
  253. * @return the parsed return statement
  254. */
  255. public ReturnStatement parseReturn() {
  256. input.skipAndExpect("return", WordBreaks.INSTANCE);
  257.  
  258. return new ReturnStatement(input.getPlace(), parseExpression());
  259. }
  260.  
  261.  
  262. /**
  263. * Parses a return statement that does not return a value.
  264. *
  265. * @return a return statement that does not return a value
  266. */
  267. public ReturnStatement parseVoid() {
  268. Place place = input.getPlace();
  269. input.skipAndExpect("void", WordBreaks.INSTANCE);
  270. return new ReturnStatement(place, null);
  271. }
  272.  
  273.  
  274. /**
  275. * Parses a throw statement
  276. *
  277. * @return the parsed throw statement
  278. */
  279. public ThrowStatement parseThrow() {
  280. input.skipAndExpect("throw", WordBreaks.INSTANCE);
  281. return new ThrowStatement(input.getPlace(), parseExpression());
  282. }
  283.  
  284.  
  285. /**
  286. * Parses a block of statements.
  287. *
  288. * @return the parsed block
  289. */
  290. public Block parseBlock() {
  291. input.skipAndExpect('{');
  292. List<Statement> statements = new ArrayList<Statement>();
  293. char ch = input.skipAndPeek();
  294. while ((ch != 0) && (ch != '}')) {
  295. statements.add(parseStatement());
  296. ch = input.skipAndPeek();
  297. }
  298. input.expect('}');
  299. return new Block(input.getPlace(), statements);
  300. }
  301.  
  302.  
  303. /**
  304. * Parses a while statement.
  305. *
  306. * @return the parsed while statement
  307. */
  308. public WhileStatement parseWhile() {
  309. input.skipAndExpect("while", WordBreaks.INSTANCE);
  310. Expression test = parseExpression();
  311. Statement body = parseStatement();
  312. return new WhileStatement(input.getPlace(), test, body);
  313. }
  314.  
  315.  
  316. /**
  317. * Parses a for statement.
  318. *
  319. * @return the parsed for statement
  320. */
  321. public ForStatement parseFor() {
  322. input.skipAndExpect("for", WordBreaks.INSTANCE);
  323. Statement initializer = parseStatement();
  324. input.skipAndExpect(';');
  325. Expression test = parseExpression();
  326. input.skipAndExpect(';');
  327. Statement modifier = parseStatement();
  328. Statement body = parseStatement();
  329. return new ForStatement(input.getPlace(), initializer, test, modifier, body);
  330. }
  331.  
  332.  
  333. /**
  334. * Parses an if statement.
  335. *
  336. * @return the parsed if statement
  337. */
  338. public IfStatement parseIf() {
  339. input.skipAndExpect("if", WordBreaks.INSTANCE);
  340. Expression test = parseExpression();
  341. Statement positive = parseStatement();
  342. Statement negative;
  343. if (testKeyword("else")) {
  344. input.expect("else", WordBreaks.INSTANCE);
  345. negative = parseStatement();
  346. } else {
  347. negative = null;
  348. }
  349. return new IfStatement(input.getPlace(), test, positive, negative);
  350. }
  351.  
  352.  
  353. /**
  354. * Parses a switch statement.
  355. *
  356. * @return the parsed switch statement
  357. */
  358. public SwitchStatement parseSwitch() {
  359. input.skipAndExpect("switch", WordBreaks.INSTANCE);
  360. Expression test = parseExpression();
  361. List<SwitchCase> cases = new ArrayList<SwitchCase>();
  362. Statement defaultStatement = null;
  363. input.skipAndExpect('{');
  364. while (input.skipAndTest(SWITCHES, WordBreaks.INSTANCE) != null) {
  365. if (testKeyword("case")) {
  366. cases.add(parseCase());
  367. } else {
  368. // must be default keyword
  369. input.expect("default", WordBreaks.INSTANCE);
  370. if (defaultStatement != null) {
  371. raise("More than one default for switch.");
  372. }
  373. defaultStatement = parseStatement();
  374. }
  375. }
  376. input.skipAndExpect('}');
  377. return new SwitchStatement(input.getPlace(), test, cases, defaultStatement);
  378. }
  379.  
  380.  
  381. /**
  382. * Parses a case of a switch statement.
  383. *
  384. * @return the parsed case
  385. */
  386. public SwitchCase parseCase() {
  387. input.skipAndExpect("case", WordBreaks.INSTANCE);
  388. List<Expression> tests = new ArrayList<Expression>();
  389. tests.add(parseExpression());
  390. char ch = input.skipAndPeek();
  391. while (ch == ',') {
  392. input.expect(',');
  393. tests.add(parseExpression());
  394. ch = input.skipAndPeek();
  395. }
  396. Statement body = parseStatement();
  397. return new SwitchCase(input.getPlace(), tests, body);
  398. }
  399.  
  400.  
  401. /**
  402. * Parses a try/catch/finally statement.
  403. *
  404. * @return the parsed try/catch/finally statement
  405. */
  406. public TryStatement parseTry() {
  407. input.skipAndExpect("try", WordBreaks.INSTANCE);
  408. Block trySection = parseBlock();
  409. List<CatchBlock> catchSection = new ArrayList<CatchBlock>();
  410. Block finallySection = null;
  411. while (testKeyword("catch")) {
  412. input.expect("catch", WordBreaks.INSTANCE);
  413. TypeNode type = parseType();
  414. String id = parseIdentifier();
  415. Block block = parseBlock();
  416. catchSection.add(new CatchBlock(input.getPlace(), type, id, block));
  417. }
  418. if (testKeyword("finally")) {
  419. input.expect("finally", WordBreaks.INSTANCE);
  420. finallySection = parseBlock();
  421. }
  422. return new TryStatement(input.getPlace(), trySection, catchSection, finallySection);
  423. }
  424.  
  425.  
  426. /**
  427. * Parses a comment.
  428. *
  429. * @return the parsed comment
  430. */
  431. public Comment parseComment() {
  432. char ch = input.skipAndPeek();
  433. if (ch != '/') {
  434. raise("Expected comment.");
  435. }
  436. input.expect('/');
  437. ch = input.peek();
  438. String message;
  439. if (ch == '*') {
  440. input.expect('*');
  441. message = input.readUntil("*/");
  442. } else if (ch == '/') {
  443. input.expect('/');
  444. message = input.readUntil("\n");
  445. } else {
  446. raise("Expected comment.");
  447. throw new AssertionError();
  448. }
  449. return new Comment(input.getPlace(), message);
  450. }
  451.  
  452. }
Last edited by ~s.o.s~; Jan 2nd, 2008 at 11:45 am. Reason: Added code tags, learn to use them.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 109
Reputation: rajatC is an unknown quantity at this point 
Solved Threads: 7
rajatC's Avatar
rajatC rajatC is offline Offline
Junior Poster

Re: i need java code convert if to switch and for to while

 
0
  #6
Dec 30th, 2007
oopss....such a long code....

is evey if statement convertible to switch...isn't it possible only in nested if's not mutually exclusive
Life is about being happy...
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: i need java code convert if to switch and for to while

 
0
  #7
Dec 31st, 2007
Code tags, please . Masijade told you to mention the problems you are getting. You've not done it yet.

is evey if statement convertible to switch...isn't it possible only in nested if's not mutually exclusive
True. switch-case statements can check only integer or character values.
Last edited by Jishnu; Dec 31st, 2007 at 5:41 am.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,615
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: i need java code convert if to switch and for to while

 
1
  #8
Jan 2nd, 2008
> switch-case statements can check only integer or character values.
To be more precise, switch statements can handle all Integral types except long. Character values and Integral values are not separate entities as explained below.
  1. <div class="bbquote"> <div class="tlc"><div class="tli">&bull;</div></div> <div class="blc"><div class="bli">&bull;</div></div> <div class="trc"><div class="tri">&bull;</div></div> <div class="brc"><div class="bri">&bull;</div></div> <div class="rule"> Originally Posted by <strong>Java Specification 4.2</strong> </div> <blockquote style="font-size:11px">A primitive type is predefined by the Java programming language and named by
  2. its reserved keyword (§3.9):
  3.  
  4. PrimitiveType:
  5. NumericType
  6. boolean
  7.  
  8. NumericType:
  9. IntegralType
  10. FloatingPointType
  11.  
  12. IntegralType: one of
  13. byte short int long char
  14.  
  15. FloatingPointType: one of
  16. float double</blockquote> </div>
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 25
Reputation: sara_84 is an unknown quantity at this point 
Solved Threads: 0
sara_84 sara_84 is offline Offline
Light Poster

java compilerhttp://www.daniweb.com/forums/dani-images/icons/help.gif

 
0
  #9
Jan 2nd, 2008
Hello Friends,

am doing Comp. Science project

I need a Project description mosty in JAVA(comiler )


Something which conver if statement to switch and for to while

thanks for all
Last edited by Ancient Dragon; Jan 3rd, 2008 at 8:41 am. Reason: removed color code
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 793
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: java compilerhttp://www.daniweb.com/forums/dani-images/icons/help.gif

 
0
  #10
Jan 2nd, 2008
so what have you done so far?
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC