How to count all if() instructions (Java) - search

Is there any way to count all if() (or else()) instructions without using BufferedReader to search for distinguish words ? If there isn't - how can I guess if the if() instruction inside the /* comment:
/*
if(*condition*){
*do something*
}
*/
is just a comment, not an instruction?

Have a look at this answer: https://stackoverflow.com/a/22760693/981828
What you are trying to achieve is far from trivial,
You would have to use a parser such as ANTLR
or JavaCC
edit:
alternativly you could run this perl one-liner from the command line, this should strip the file from all of its comments. If you are running windows, install cygwin.
perl -0pe 's#/\*(.|\n)*?\*/##g; s|//.*?\n|\n|g' test.java >> newfile.java
and then count if( and else( inside the newfile.java with a BufferedReader and Tokenizer

Related

What is the Lua "replacement" for the pre_exec command in Conky files?

I'm not great at programming, but I was trying to fiddle around with a conky_rc file I liked that I found that seemed pretty straight-forward.
As the title states, I have now learned that the previous command of pre_exec has been long removed and superseded by Lua.
Unfortunately, I cannot seem to find anything directly related to this other than https://github.com/brndnmtthws/conky/issues/62. The thread https://github.com/brndnmtthws/conky/issues/146 references it, and its "solution" states: Basically, there is no replacement and you should use Lua or use a very large interval and execi.
I have found a few more threads that all include the question as to why this function was discontinued, but with no actual answers. So, to reiterate mine, I have absolutely no knowledge of Lua (I've heard of it before, and I've now added a few websites to look at tomorrow since I have spent most of the evening trying to figure out this Conky thing), and I'll probably just give up and do the execi option (my computer can handle it but, I just think it's so horribly inefficient).
Is there an appropriate Lua option? If so, would someone please direct me to either the manual or wiki for it, or explain it? Or is the "proper" Lua solution this?
#Vincent-C It's not working for your script is because the function
ain't getting call. from the quick few tests I did, it seem
lua_startup_hook need the function to be in another file that is
loaded using lua_load, not really sure how the hook function thingy
all works cause I rather just directly use the config as lua since it
is lua.
Basically just call the io.popen stuff and concat it into conky.text
conky.text = [[ a lot of stuff... ${color green} ]];
o = io.popen('fortune -s | cowsay', 'r') conky.text = conky.text ..
o:read('*a')
The comment by asl97 on the first page you cited appears to provide an answer, but a bit of explanation would probably help.
asl97 provides the following general purpose Lua function to use as a substitute for $pre_exec, preceded by a require statement to make io available for use by the function:
require 'io'
function pre_exec(cmd)
local handle = io.popen(cmd)
local output = handle:read("*a")
handle:close()
return output
end
Adding this block of code to your conky configuration file will make the function available for use therein. For testing, I added it above the conky.config = { ... } section.
Calling the Lua pre_exec function will return a string containing the output of the command passed to it. The conky.text section from [[ to ]] is also a string, so it can then be conactenated to the string returned by pre_exec using the .. operator, as shown in the usage section provided by asl97.
In my test, I did the following silly bit, which worked as expected, to display "Hello World!" and the output of the date function with spacing above and below each at the top of my conky display:
conky.text = pre_exec("echo; echo Hello World!; echo; date; echo")..[[
-- lots of boring conky stuff --
]]
More serious commands can, of course, be used with pre_exec, as shown by asl97.
One thing that asl97 didn't explain was how to provide how to concatenate so that the pre_exec output is in the middle of the conky display rather than just the beginning. I tested and found that you can do it like the following:
conky.text = [[
-- some conky stuff --
]]..pre_exec("your_important_command")..[[
-- more conky stuff --
]]

How to add a sentence to the enter entry and before return instruction of all the functions of my project . Thanks

I want to print some useful message after entering or returning functions by adding the following sentence.
printf("enter %s\n", __FUNCTION__);
Add the following sentence before return instruction.
printf("exit %s\n", __FUNCTION__);
My OS is ubuntu. I think vim can do this work. Editing before return instruction is easy. However, to the first case, finding the first line of every function is not easy .
Can gcc do this work during the precompiled period ?
Can any one help me .
Thanks very much.
You are considering an editor-based approach.
You could also consider a compiler based approach. You could extend GCC using your MELT extension which would add a pass. This pass would just have to add some extra Gimple into the GCC internal representations.

Having command line arguments to flex as search strings

I use flex, the linux/unix not the Adobe type, to generate small scanners. In the past I have always used static search strings. I now want to provide a command line provided search string by providing a string via getopt and then being able to use it for searching with.
The old way of searching was:
.*"_"\n ECHO;
To find lines that ended with an underscore.
Now I want to search this way:
.*<arbitrary string>.*\n ECHO;
I don't know how to get flex to accept the <arbitrary string>. I can get it via getopt, but I haven't been able to get flex to accept my syntax.
What I am doing is a special purpose very limited grep for a special problem I am having.
Any help would be appreciated.
.*\n { if(strstr(yytext, "arbitrary string")) ECHO; else REJECT; }
The REJECT statement will skip to next rule if yytext doesn't contain "arbitrary string". This will of course not provide the same performance as if the search string was known at compile time. regcomp()/regexec() in glibc might be faster than flex if you are implementing your own grep program.

perl byte code generation with too many file.pl

How to make perl bytecode if sub is there in another file.pl so that I can get all perl script in to binary to give for usage but I am getting codedump warning.
Here is the example how I have done!
File: add.pl
require "util.pl";
$a = 1;
$b = 2;
$res = add($a,$b);
print $res;
File: util.pl
sub add()
{
my ($a,$b) = #_;
my $c = $a + $b;
return $c;
}
1; #to return true
Then when I run:
perlcc add.pl
./a.out
I get
Segmentation fault (core dumped)
I also tried
perlcc add.pl util.pl
but it says
/usr/bin/perlcc: using add.pl as input file, ignoring util.pl
Note:
If both are in single file
perlcc file.pl
and
./a.out
will work
I cannot answer for the actual compiler problem, but let me make a few notes.
<Edit> the more I look at this, the more I think that the problem is the namespacing of the add function. When they are in the same file, the function is declared in the main namespace. I think that would be true of the require-d file too, since there was not package declaration. Either way, these are still some good notes that I hope help. </Edit>
You really should use strict and warnings pragmas
You shouldn't use $a and $b, because they are semi-magical in Perl and should be avoided (yeah, thats a weird one)
Perl prototypes are not the same as most languages, and even then the empty prototype () on your add function is incorrect, best to leave it off
Those things said here is how I would format my files.
File: add.pl
use strict;
use warnings;
use MyUtils;
my $x = 1;
my $y = 2;
my $res = add($a,$b);
print $res;
File: MyUtils.pm
package MyUtils;
use strict;
use warnings
use parent 'Exporter';
our #EXPORT = ('add');
sub add
{
my ($x,$y) = #_;
my $c = $x + $y;
return $c;
}
1;
This uses the more modern module/package formalism for reusable libraries. The use directive contains a require directive, but does it at compile-time rather than run-time.
The Exporter module (and the #EXPORT variable) correctly import the function into the script's namespace (typically main).
Perhaps perlcc will like these changes better; but even if not, these are good practices to get used to.
perlcc was removed from Perl in version 5.10.0 (almost five years ago). The perldelta manual page has this to say:
perlcc, the byteloader and the supporting modules (B::C, B::CC,
B::Bytecode, etc.) are no longer distributed with the perl sources.
Those experimental tools have never worked reliably, and, due to the
lack of volunteers to keep them in line with the perl interpreter
developments, it was decided to remove them instead of shipping a
broken version of those.
Seeing that, I have to suggest that using perlcc with any version of Perl is probably a rather bad idea. It was an experimental feature that never really worked. You probably want to move away from using it.

How to read string from file using Groovy in QuickBuild

I am new to QuickBuild.
I have a lot of different versions stored in text files. To start the build process, I need to retrieve the versions from the text files and pass them to a shell script.
My question is: How do I read the contents of a file using the QuickBuild environment?I know that it supports Groovy, MVEL and OGNL languages but I'm not familiar with any of them.
Thanks in advance.
I found the solution :)
${groovy: str = new java.io.File("[PATH_TO]/file.txt").text}
or
${groovy: str = new java.io.File("[PATH_TO]/file.txt").text
str.split("[\\r\\n]")[0] }
to read the first line only.
Thanks to me :)
A slightly shorter version for reading only the first line (which doesn't make any assumption about the EOL character):
${groovy: str = new File("[PATH_TO]/file.txt").readLines()[0] }

Resources