• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is this possible with regex?

 
Ravi Shankarappa
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is a line of code in ALGOL 60



In Java I could write it as


Is there a way to do this programmatically using regex or something else? If so could you tell me how?

-Thanks
Ravi
 
fred rosenberger
lowercase baba
Bartender
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course it can be done - but the answer is always "it depends". You need to define better what you need.

for example, it could be done in a java program quite simply. Assuming the String s holds

"FOR" K:=N-1 "STEP" -1 "UNTIL" 1 "DO"


you could write this:


but I'm guessing that won't actually solve your problem.

My point is that you have to decide what situations you will and won't handle. Only once you have ALL those conditions clearly thought out and defined should you consider writing a single line of java/regex/perl/whatever to solve your problem.
 
Darryl Burke
Bartender
Posts: 5136
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that regex doesn't help in converting between uppercase and lowercase.
 
Ravi Shankarappa
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking for a more general solution via substitution. I only need to take care of 174 routines in ALGOL 60, so its not a big effort. I understand regex isn't much use in converting upper case to lower case, then again it is easily done via built in tolowercase functionality of String class.
There are sections of code that are not easily convertible via parser, anyway. Also I need to take into consideration the machine precision. So except for some obvious substitutions, I'll have to look at each line of code. Writing a parser might take a longer time than diving into converting by hand.

-Ravi
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason converting by hand is faster than a parser is that you already have a parser written, and you can find it in the back of your head which is a smart place to keep it. It is by no means difficult to write a parser, eg with lex and yacc (JFlex and CUP are more modern counterparts) or ANTLR or similar, if the grammar is context-free. You will find lex, flex, JFlex etc, use regular expressions frequently to identify numbers, keywords, etc.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic