Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I have a Java source file. I want to parse it to find where a method call occurs. For example, it could find the call:
obj.callingMethod1();
I also want to be able to replace with a different call. For example, I might replace it with:
obj2.callingMethod2();
but every things are dynamic it means that some where maybe we have a method call with parameter and so on. I found lots of parser for java , but i want to know if there is any parser that has wrote for java grammar ? I mean does it has ability to find java method call , java method definition ,java variable definition and ... thanks However, I would like to know a way to do this that takes into account the dynamic possibilities for different method calls, i.e. there may be a method call with parameter, etc. I found many Java parsers, but I want to know if there is any parser specifically for Java grammar. I am looking for something that has the ability to find java method calls, java method definitions, java variable definitions, and so on.
Your question doesn't explain if the java code occurs within the text or if the text is java code.
Based on what you said it could be
1) There's text and java code and there's a demarcation between text and java code
2) There's text and java code and there is not a demarcation.
3) The text is java code.
If what you need is (1) or (3) then I would use JavaCC: http://www.engr.mun.ca/~theo/JavaCC-Tutorial/javacc-tutorial.pdf
If what you need is (2) then there isn't much you can do beyond String.replaceAll(...)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I’m learning Java and have recently started my first project. The idea of this project is to pass one input argument - path to file/folder, which would be analyzed in order to find all files with predefined extension, parse them and create objects based on the results of parsing to store for future.
So far I’ve written all the code and my project structure (simplified) looks like that:
Class defining resulting object
Class that analyzes the input parameter (exists, is file, is folder) and processes it, returning list of all suitable files
Class that parses suitable files and creates objects
The question is - am I following OOP with that structure?
From what I’ve read on the web the last two classes seem to look like polterheists. But I don’t think that it is a good idea to move the logic of the third class to the object class because it consists of lots of methods (define current section of the file, strategy to parse each separate section).
I am learning on my own and don’t want to start my journey by cultivating bad habits.
I am learning on my own and don’t want to start my journey by cultivating bad habits.
You're saying this like you have a choice :)
From what you described it seems reasonable, of course w/o seeing the code we can't say. And even if you show the code - 100 people will have 100 opinions, there's a lot of debates around OOP.
What's important is not to look at your design as something static. Once your app starts to be more complicated you'll have to re-work some of it.
PS: stackoverflow doesn't like this kind of questions since everyone will have an opinion. You'll have to find other resources if you keep having such questions.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I need to identify java source code all type of statements and store them in a tree structure to make a control flow graph! what i cannot understand is how should i read a java source code so that my program may distinguish all different types of statements in java( if,for, classes, methods etc.)
Do i need to add the whole grammar of java language?
what i cannot understand is how should i read a java source code so that my program may distinguish all different types of statements in java( if,for, classes, methods etc.)
Read java source code (uncompiled)
file extension is .java and it's just a regular text so this should be a trivial task.
Now depends what you wanted to parse and to store.
The best way is to have all the grammar and check the file.
There are tools that are doing lexical analysis, also known as language recognition, and also generate for you AST (abstract syntax tree). Eg. JavaCC or ANTLR.
But maybe you want just a custom parse(partial).
So you can store the keywords in a data-structure (if, for) and parse the file accordingly.
(and with some patterns for instruction eg:if. More could made simple automates_DFA for each instruction or maybe regular expression)
Even here is a little bit work. Eg. Want if from instruction not if from a text. String s="if". Or/And are you sure that every time will parse a valid java file? )
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 9 months ago.
Improve this question
I have a random text content in a String variable. I want to look for all word inflections of a specific word user specifies.
Example: If the user is looking for the word "assist" then it should grab all "assist, assists, assisted, assisting" occurrences in the String.
Is there a Java library available to detect such inflections automatically in the specified String?
Note: I have seen a Java library called WolframAlpha that claims it does this and here is its web interface, but i don't see this library working, and no guide is available for using it.
First of all it is not Java library, it is Wolfram language previously known as Mathematica. It does have JLink and can be called from Java, but you must have Wolfram Kernel running that executes the code.
This is called Natural Language Processing and it's a huge, complex field. I have fiddled about with few problems, but all I can say this is harder then complex if you want to get reliable solution.
Something you might want to take a look at would be : The Stanford NLP
It is called word stemming. First you need (for a specific language) derive the stem:
assisting -> assist using -ance, -ing, -ly, -s, -ed etcetera.
sought -> search using an exception list
Then do a search, maybe with a regular expression (Matcher.find). Pattern:
"\\bassist\\p{L}*"
"\\b(search|sought)\\p{L}"
For prefixes un- dis- inter- the case would still be more complicated, but in general flections are word endings in English. Then there is synonym searching.
Dictionaries out there are often called corpora. A search for "free English corpus" will yield results.
\\b = word boundary
p{L}* = 0 or more (*) letters
Check this out..
I don't know how big your requirement is, but you can always use wiktionary and parse your data??
Check this question.. Can be of help
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I have thousands of pieces of address data and I want to parse them so I can separate street from country from postal code and so on.
Is there any way to do that in Java ?
I know that google open sourced their international address and phone number parsing library. I'd suggest you check their presentation here and javadoc.
If you simply have addresses from all over the world in the form they are on the letters, and you later want to send letters there, you better leave them in this format (maybe after splitting of the country, which comes usually last).
The internal formats very differ between the individual countries (even if only comparing Germany, Great Britain, Russia), and having a database with the individual components afterwards requires individual (country-specific) logic to put them together again.
(I once had an application which took input of the individual fields and later created an address list from then (by the "german way to do this"), and always received complains from the British users that I formatted their addresses in wrong order. So in a later version I simply created a multi-line "address" input field, which I then outputted without any change.)
You could probably use regular expressions if you don't want to add 3rd party dependencies.
See: http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html
and http://download.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html
Usage is basically:
private static final Pattern PAT_NAME = Pattern.compile("my\\sregex");
...
Matcher matcher = PAT_NAME.matcher("my address");
There is an older library here: http://jgeocoder.sourceforge.net/parser.html, but it works for most cases.
If you want to use an API, I've used SmartyStreets in the past and they work decently well (https://smartystreets.com/).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
In Java, I have set of expressions like cond1 AND (cond2 OR cond3) AND ( cond 4 OR cond5). I would like to convert it into tree and then evaluate the final boolean answer. I tried searching a lot around java BDD but not able to get any. Any suggestion with sample code ?
A 5-second Google search returned some reasonable-looking results:
JavaBDD
Java Decision Diagram Libraries
What is the best Binary Decision Diagram library for Java?
Is this not what you're looking for?
He means Binary Decision Diagrams.
I've been tinkering with JavaBDD and JBDD/JDD. Both are based on BuDDY (a C library) -- JBDD actually uses the C DLLs for a marginal performance boost.
It looks to me like JavaBDD is more fully-featured (ex. it supports composing BDDs, which is what I need). But there is also no tutorial for it, and while the class docs aren't terrible, frankly I can't figure out how to use it for the most basic of boolean operations (like the problem you pose).
JBDD/JDD requires you to use manual garbage collection, and does weird things like store BDD objects in Java integers -- clearly carry-overs from C. But it has a set of tutorials.
If you want to run your own parser, check out JavaCC.
Here is a nice tutorial to get you started. A bit older, but still valid:
http://www.javaworld.com/jw-12-2000/jw-1229-cooltools.html