ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
public static void main(String args[]) {
int x = 10;
while( x < 5)
{
}
while( x < 20 ) {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
}
}
Now as you can see there are two while loops , one while loop is empty and other while loop has got some logic.
Now I would like to print the while loop which has got no logic in it. In other words I would like to grep and print the while loop which does not have any logic. How can I grep Multiple lines. So that I can print the while loop which has no logic.
I don't understand what you want to print. I am not a Java programmer, but it appears that you initialize a variable (x) and set it equal to 10. In your first while loop, you state while X < 5, but X will never be less than 5 in this case, so what would you expect to print.
Hi, I would just like to grep (do a pattern matching) on the java file. This java file is just a sample. In real scenario I would like to do a pattern match that identifies all while loops (the loops which are empty i.e does not have logic between { and } ) from many java files in my project.
In this particular case you may use something like this:
Code:
$ perl -0777 -ne 'print "$&\n" if /while[^{]*{[ \n]*}/' infile
while( x < 5)
{
}
Here -0777 enables so called slurp mode -- perl will read entire file into memory before attempting pattern match (in this case the paragraph mode -00 could be used too). This allows to match multiline patterns.
In general you should consider using a parser for such kind of processing. It may be a complete java parser (e.g. this one) or a surface grammar (the one capturing only specific syntactic structures and ignoring everything else) parser. In the latter case Parsing Expression Grammars come in handy.
... I would like to print the while loop which has got no logic in it. ...
Can these files contain multiple instances of "empty" (i.e. no logic) while loops? If so, do you want to print all such code segments?
Example:
Code:
public class Test {
public static void main(String args[]) {
int x = 10;
while( x < 5)
{
}
while( x < 99)
{
}
while( x < 20 ) {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
}
}
public class Test {
public static void main(String args[]) {
int x = 10;
while( x < 5)
{
}
while( x < 99)
{
}
while( x < 20 ) {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
}
}
public class Test {
public static void main(String args[]) {
int x = 10;
while( x < 5 )
{
}
while( x < 6 )
{
}
while( x < 7 ) {}
while( x < 8 ) { }
while (x<9) {
// some comments here
}
while( x < 20 ) {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
}
}
... and produced this OutFile ...
Code:
while( x < 5 )
{
}
while( x < 6 )
{
}
while( x < 7 ) {}
while( x < 8 ) { }
while (x<9) {
// some comments here
}
import pyparsing as pp
import sys
emptyLoopBody = pp.ZeroOrMore( pp.javaStyleComment ^ pp.White() )
emptyLoop = pp.Combine( pp.Regex(r"while[^{]+{") + emptyLoopBody + "}" )
parser = pp.ZeroOrMore( pp.Suppress(pp.SkipTo(emptyLoop)) + emptyLoop )
def printLocation(s, loc, toks):
print "###### Match at line", pp.lineno(loc, s)
print toks[0]
emptyLoop.setParseAction(printLocation)
for fn in sys.argv[1:]:
parser.parseFile(fn)
Code:
$ python parser.py ./infile
###### Match at line 6
while( x < 5 )
{
}
###### Match at line 10
while( x < 6 )
{
}
###### Match at line 15
while( x < 7 ) {}
###### Match at line 18
while( x < 8 ) { }
###### Match at line 20
while (x<9) {
// some comments here
}
The following code searches for loops with one or more comments inside:
Code:
import pyparsing as pp
import sys
emptyLoopBody = pp.OneOrMore( pp.javaStyleComment )
emptyLoop = pp.Regex(r"while[^{]+{") + emptyLoopBody + "}"
parser = pp.ZeroOrMore( pp.Suppress(pp.SkipTo(emptyLoop)) + emptyLoop )
def printLocation(s, loc, toks):
print "###### Match at line", pp.lineno(loc, s)
end = pp.getTokensEndLoc()
print s[loc:end]
emptyLoop.setParseAction(printLocation)
for fn in sys.argv[1:]:
parser.parseFile(fn)
By default pyparsing ignores whitespaces (spaces, tabs and newlines). I removed pp.Combine() because it treats whitespaces specially and instead used pp.getTokensEndLoc() function to determine where is the end of the match. This allows to remove pp.White() and tell pp.OneOrMore(pp.javaStyleComment) since our grammar is whitespace agnostic now.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.