I am new in ANTLR. My requirement is to programaticaly parse a PL/SQL block and to be able to format it based on some conditions.For example:
1) I want to find all the commented code inside the SQL block.
2) I should be able to write back the parsed/edited object back in some sql file.
Currently I have compiled the plsql-parser available at porcelli/pl-sql.
I also went through this helpful link. So in a nutshell I have parsed the sql block.
PLSQLLexer lex = new PLSQLLexer(new ANTLRNoCaseFileStream(file));
CommonTokenStream tokens = new CommonTokenStream(lex);
PLSQLParser parser = new PLSQLParser(tokens);
/*start_rule_return AST =*/ parser.data_manipulation_language_statements();
System.err.println(file +": " + parser.getNumberOfSyntaxErrors());
//This is the place I want to build my tree
// parser.setBuildParseTree(true);
//ParseTree tree = parser.plsql_block();
I need some help, useful links in this direction. I am new in ANTLR; so any help in any direction will be appreciated.
Related
I want to convert $departureFromDate (format:yyyy-MM-dd) to date object so that I can perform increment operations on it. I have been trying to do it the following way:
#set($departureFromDate = "{{jsonPath request.body
'$.departureFromDate'}}")
#set($dateObj = $date.toDate('yyyy-MM-dd',"$departureFromDate"))
#set($calendar = $date.getCalendar())
$calendar.setTime($dateObj)
$calendar.add(6,5)
The above code works if give an actual date like:
#set($dateObj = $date.toDate('yyyy-MM-dd',"2018-09-22"))
But does not work when I try to use $departureFromDate
There are several problems in your code. First, as user7294900 noted, the right value of the first assignation seems quite weird. Then, you don't need to instanciate yourself a calendar (plus, you can write $date.calendar instead of $date.getCalendar(), and you don't need double quotes around string arguments).
#set($body = '{ "departureFromDate" : "2018-03-01" }')
$json.parse($body)
#set($departureFromDate = $json.departureFromDate)
#set($dateObj = $date.toDate('yyyy-MM-dd', $departureFromDate))
#set($calendar = $date.toCalendar($dateObj))
$calendar.add(6, 5)
The above code uses a JSON parsing tool, whose parse() method renders a json wrapper, that you shall provide in your context.
As a final advise, if you hadn't already thought of it, be sure to print $obj and $obj.class.name in your context as a trivial debugging technique if you don't understand what happens.
at start i want to apologise for my bad english.
I make webApp and my task what i need to do is to tokenize Java code. I found tool like ANTLR v4 and i tried to implements it.
public class Tokenizer {
public void tokenizer(String code) {
ANTLRInputStream in = new ANTLRInputStream(code);
Java8Lexer lexer = new Java8Lexer(in);
List<? extends Token> tokenList = new ArrayList<>();
tokenList = lexer.getAllTokens();
for(Token token : tokenList){
System.out.println("Next token :" + token.getType() + "\n");
}
}
}
And this code print on screen list of int with number of token Type.
I need something like this:
Code with something like "comments" to code.
How can i get this result?
I have this grammar : https://github.com/antlr/grammars-v4/tree/master/java8
The Token class contains several methods including
int getLine();
int getCharPositionInLine();
that associate the token with the corresponding source.
Using
token.getText()
you should get the parsed text the token represents.
In addition, you should get the token's name by
lexer.getVocabulary().getSymbolicName(token.getType())
The problem you are facing here is you want a mix of tokens and rules in the output. For instance VARIABLE_DECLARATION is actually a parser rule, while IDENTIFIER ASSIGN IDENTIFIER consists of 3 lexer rules. You can use the token stream to print the recognized lexems, but that won't give you any parser rule.
What you can try instead is to print the return parse tree, which you get when you do a real parse run on your input (see ParseTree.toString()). You can use a parser listener to walk a parse tree and transform that into a stream of rule descriptions along with the text that belongs to a rule (context).
I am absolutely new to Java development.
Can someone please elaborate on how to obtain "Grammatical Relations" using the Stanfords's Natural Language Processing Lexical Parser- open source Java code?
Thanks!
See line 88 of first file in my code to run the Stanford Parser programmatically
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
Collection tdl = gs.typedDependenciesCollapsed();
System.out.println("words: "+words);
System.out.println("POStags: "+tags);
System.out.println("stemmedWordsAndTags: "+stems);
System.out.println("typedDependencies: "+tdl);
The collection tdl is a list of these typed dependencies. If you look on the javadoc for TypedDependency you'll see that using the .reln() method gets you the grammatical relation.
Lines 311-318 of the third file in my code show how to use that list of typed dependencies. I happen to get the name of the relation, but you could get the relation itself, which would be of the class GrammaticalRelation.
for( Iterator<TypedDependency> iter = tdl.iterator(); iter.hasNext(); ) {
TypedDependency var = iter.next();
TreeGraphNode dep = var.dep();
TreeGraphNode gov = var.gov();
// All useful information for a node in the tree
String reln = var.reln().getShortName();
Don't feel bad, I spent a miserable day or two trying to figure out how to use the parser. I don't know if the docs have improved, but when I used it they were pretty damn awful.
I am currently trying to run a Python grammar template from the ANTLR website. I am studying by currently trying to run a test input from it, but it does not seem to be generating any error messages. What could be the cause of this? I am currently using the following syntax:
CharStream input = new ANTLRFileStream("t1.py");
System.out.println(input);
PythonLexer lexer = new MyLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
PythonTokenSource indentedSource = new PythonTokenSource(tokens);
tokens = new CommonTokenStream(indentedSource);
PythonParser parser = new PythonParser(tokens);
System.out.println(tokens);
System.out.println("tokens="+tokens.getTokens());
parser.file_input();
What basically happens is just my input just gets shown and it shows the string done if its done, but my input text is this:
asdasdasdasdguess = "a"
a = raw_input('Please enter a value:')
correct = "b"
if guess == correct:
print "That's it!\n"
and my output shows this:
asdasdasdasdguess = "a"
a = raw_input('Please enter a value:')
correct = "b"
if guess == correct:
print "That's it!\n"
done
Isn't this supposed to generate an error since after all the input is an error? Or is it because the grammar I used is wrong? I'm trying to understand how it's supposed to read inputs but all of the examples I've seen were using correct inputs so I couldn't determine what would happen if you used a wrong input.And if you think that my grammar is wrong, could you suggest a sample python grammar template that I could use to test with?
i haven't found the answer to my problem so I decided to write my question to get some help.
I use lucene to index the objects in computer memory(they exist only in my java code). While processing the code I index (using WhitespaceAnalyzer) the field with value objA/4.
My problem starts when I want to find it after the indexation (also using WhitespaceAnalyzer).
When i create a query obj* , I find all objects that start with obj - if i create a query objA/4 I also can find this object.
However i don't know how to find all objects starting with objA/ , when I create a query objA/* lucene is changing it to obja/* and finds nothing.
I've checked and "/" is not a special character so i dont need any "\" preceding it.
So my question is how to ask to get all objects that starts with objA/ (for example - objA/0, objA/1, objA/2, objA/3)?
Are you using QueryParser.escape(String) to escape everything correctly?
The code i'm using:
String node = "objA/*";
Query node_query = MultiFieldQueryParser.parse(node, "nodeName", new WhitespaceAnalyzer());
BooleanQuery bq = new BooleanQuery();
bq.add(node_query, BooleanClause.Occur.MUST);
System.out.println("We're asking for - " + bq);
IndexSearcher looker = new IndexSearcher(rep_index);
Hits hits = looker.search(bq);