Python Grammar Template in ANTLR Java - java

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?

Related

take json input from command line and parse the data in java

I want to take a (command + JsonData) pattern from command line as input and I don't know how to do it in java. I would be very grateful if anyone can help.
imagine that I have some command + JsonData like this:
addOffering {"code":"81013602","name":"course name","Instructor":"the teacher name",
"units":3,"classTime": {"days": ["saturday","Monday"], "time":"16-17:30"},
"examTime": {"start": "2021-9-01T08:00:00","end": "2021-9-01T08:00:00"},"capacity":60,
"prerequisites": ["Advanced Programming","Operating Systems"]}
in the program I have several commands (like addOffering) so I should take other commands as input.
for taking inputs I am using scanner (but I don't know whether it's a good idea) because i'm new to java.
while(scanner.hasNext()) {
String command = scanner.next();
if(command.equals("exit")) {
break;
}
}
in a while loop I wish to take my inputs and parse command from JsonData to call several functions for different commands and also to initialize my classes fields by the Json data using object mapper(jackson). any help would be greatly appreciated.

How tokenize code with ANTLR v4

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).

How to use porcelli plsql-parser (ANTLR)

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.

I can't get useDelimiter() to cooperate

I am working on a Java project and I am trying to get useDelimiter to remove everything except the text in between "=" and ",". For example, on the first line of the file I would like to keep "ThermostatNight".
This is what the text file looks like:
Event=ThermostatNight,time=0
Event=LightOn,time=2000
I've been able to do the exact opposite using this code:
s.useDelimiter("=(.*?),");
Is there any way I can tweak this to do the opposite?
Try this:
s.replaceAll("([a-zA-Z]*=)([a-zA-Z]*)(,.*)","$2")
This is following the pattern you show. If it changes you will have to change this regex.
See it here: http://regex101.com/r/jN8iR8
See the section Match groups, you will see that group 2 is your desired result.
The replace all method is replacing everything just for the group 2 which is what is in between = and ,
Edit
As the OP said that s is a Scanner
Scanner s = new Scanner(new File("YOURFILE"));
while (s.hasNext()) {
String text = s.next();
System.out.println( text.replaceAll("([a-zA-Z]*=)([a-zA-Z]*)(,.*)","$2") );
}
s.close();

Java - generating conditions from string

I'm trying to generate some conditions using string i get as input.
For example, i get as in put the string "length = 15" and i want to create from that the condition:
length == 15.
To be more specific, i have an int in my program called length and it is set to a specific value.
i want to get from the user a conditon as input ("length < 15" or "length = 15"....) and create an if statement that generates the condition and test it.
What is the best way of doing that?
Thanks a lot
Ben
Unless you're talking about code-generation (i.e. generating Java-code by input strings) you can't generate an if-statement based on a string.
You'll have to write a parser for your condition-language, and interpret the resulting parse trees.
In the end it would look something like this:
Condition cond = ConditionParser.parse("length = 15");
if (cond.eval()) {
// condition is true
}
Use a string tokenizer. The default method to distinguish between tokens (or the smallest parts of the input string) is white space, which is to your benefit.
check out javadocs for details:
http://docs.oracle.com/javase/1.3/docs/api/java/util/StringTokenizer.html
Depending on what restrictions you can place on your input format, you could consider using Rhino to embed Javascript. Your 'conditions' then just have to be valid JavaScript code. Something like this (disclaimer: haven't compiled it):
import javax.script.*;
public bool evalCondition (Object context, String javascript) {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");
Object result = engine.eval(javascript);
Boolean condTrue = (Boolean)result;
return condTrue;
}
See the Embedding Rhino Tutorial for more details.

Categories

Resources