Compiling ANTLRWorks generated class files - java

I am using ANTLRWorks to create ANTLR grammars. I have a valid grammar and the parser and lexer source files are generated as well. I have also tried debugging the generated code and the output is as expected in the debugger output.
But when I try to invoke the __Test__ class generated by the debugger nothing is coming up in the console. I have properly set up the classpath as I can successfully compile the __Test__.java with the same classpath.
What would be the problem? Is there any clear tutorial for writing and compiling a sample parser with antlr and antlrworks?

What do you expect on the console to come up?
Have a look at this project. The ANTLRWorks generated parser is here. As you can see from the dependencies in the POM you need to make sure antlr is in the classpath. Then you use the parser as shown in this class.
final DriftLexer lexer = new DriftLexer(new ANTLRInputStream(inputStream));
final CommonTokenStream tokens = new CommonTokenStream(lexer);
final DriftParser parser = new DriftParser(tokens);
parser.file();
That should be enough to get your stuff working as well.

ANTLRWorks generates test classes that create a socket connection back to ANTLRWorks, so they aren't usable from the console. You can edit the generated test class to not use the debug port (socket connection) option.
The line to edit is:
FormalSpecParser g = new FormalSpecParser(tokens, 49100, null);
You can change it to:
FormalSpecParser g = new FormalSpecParser(tokens, null);
which uses a debug listener object instead of a port, and the "null" means you're not giving it a debug listener, so debug output is ignored. You could write your own debug listener to print out messages to the console.
See the ANTLR documentation for more information: http://www.antlr.org/api/Java/namespaces.html

Related

Gradle task error : NullPointerException thrown while using ServerEvaluationCall to invoke xquery module

I am new to gradle and need to write a task for scheduling MarkLogic backup.
So, I want to invoke an XQuery module that uses a config XML for getting details for backup.
So I tried this :
task mlBackupTask(type: com.marklogic.gradle.task.ServerEvalTask) {
def client = hubConfig.newStagingClient()
println client
//DatabaseClient client = DatabaseClientFactory.newClient(host,portno,new DatabaseClientFactory.DigestAuthContext(username, password))
ServerEvaluationCall invoker = client.newServerEval();
String result = invoker.modulePath("/admin/create-backup.xqy").addVariable("config-name", "dev").evalAs(String.class);
}
I tried both :
hubConfig.newStagingClient()
DatabaseClientFactory.newClient(host,portno,new DatabaseClientFactory.DigestAuthContext(username, password))
This doesn't work and just give this error :
Execution failed for task ':mlBackupTask'.
java.lang.NullPointerException (no error message)
Could someone please help out on this?
Start with the docs at https://github.com/marklogic-community/ml-gradle/wiki/Writing-your-own-task . "hubConfig.newStagingClient()" will only work if you're using DHF, as hubConfig is specific to DHF.
Also, I think based on your code, what you really want is to use MarkLogicTask. The purpose of ServerEvalTask is to allow you to write a single line of JS or XQuery code. It looks like you want to write multiple lines of code, given a DatabaseClient. If so, use MarkLogicTask, and also put your code in a "doLast" block as shown in the docs.

Debugging ActiveJDBC with IntelliJ IDEA

I am new to ActiveJDBC. I am trying to debug the sample project.
The code I want to debug is:
public static void main(String[] args) {
Base.open();
Person director = new Person("Stephen Spielberg");
director.saveIt();
//[break point here]
director.add(new Movie("Saving private Ryan", 1998));
director.add(new Movie("Jaws", 1982));
director.getAll(Movie.class).forEach(System.out::println);
Base.close();
}
The code compiles correctly and the instrumentation is properly executed (I believe) (have a look here).
The debugger is launched and paused at the defined break-point.
I am trying to evaluate the expression "Person.count()" and I am expecting the result to be 1.
But I have the following error in the 'Evaluate expression' window:
Method threw 'org.javalite.activejdbc.InitException' exception.
failed to determine Model class name, are you sure models have been instrumented?
Have a look: https://unsee.cc/nipareto/
It is possible that you recompiled models after instrumentation unintentionally. If you instrument, then make any change to a model, and then try to run your code, and IDE will detect the change and recompile your model, thus blowing away instrumentation.
Ensure you instrument before you run your code.
Additionally, the link you provided: https://github.com/javalite/activeweb-simple is not corresponding to code. I think you are using this one: https://github.com/javalite/simple-example. If so, try running on command line according to README.
Debugging models in ActiveJDBC in IDEA is what I do daily:)
Also, I recommend you watch the video on this page: http://javalite.io/instrumentation because it walk you step by step using IDEA.
UPDATE April 10 2017:
I recorded this video to show you how to instrument and debug an ActiveJDBC project: https://www.youtube.com/watch?v=2OeufCH-S4M

Compiling ISO SQL-2003 ANTLR Grammar

I am trying to compile the ISO-SQL 2003 grammar from here
http://www.antlr3.org/grammar/1304304798093/SQL2003_Grammar.zip. All three versions of it can be found here http://www.antlr3.org/grammar/list.html.
These are the steps I followed,
java -jar antlr-3.3-complete.jar -Xmx8G -Xwatchconversion sql2003Lexer.g
java -jar antlr-3.3-complete.jar -Xmx8G -Xwatchconversion sql2003Parser.g
javac ANTLRDemo.java
ANTLRDemo.java file:
import org.antlr.runtime.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class ANTLRDemo {
static String readFile(String path) throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, "UTF-8");
}
public static void main(String[] args) throws Exception {
ANTLRStringStream in = new ANTLRStringStream( readFile(args[0]) );
sql2003Lexer lexer = new sql2003Lexer(in);
CommonTokenStream tokens = new CommonTokenStream(lexer);
sql2003Parser parser = new sql2003Parser(tokens);
parser.eval();
}
}
First two steps work fine, then while compiling my main class I get a lot of errors related to Java syntax like these:
./sql2003Parser.java:96985: error: not a statement
$UnsignedInteger.text == '1'
./sql2003Parser.java:96985: error: ';' expected
$UnsignedInteger.text == '1'
./sql2003Parser.java:102659: error: unclosed character literal
if ( !(((Unsigned_Integer3887!=null?Unsigned_Integer3887.getText():null) == '01')) ) {
Please let me know if I am doing something wrong in setting up the parser. It would be helpful if someone can show me how exactly to setup this grammar using ANTLR.
Edit: After a little more fiddling, I think that these errors are caused by the actions present in lexer and parser rules. Is there a safe way to overcome this?
You are not doing anything wrong, ANTLR has never been able to generate a working Java parser from these grammar files.
According to a post by Douglas Godfrey to antlr-interest in Oct 2011:
I generated a C parser and lexer. they both generate and compile
successfully
on my machine with 8GB heap allocated to Antlr.
...
I don't believe that it will ever be possible to get a working parser in
Java. A C language parser on the other hand is quite possible.
Yes, basically you’re right. The grammar is broken. But also there is an error in your ANTLRDemo.java as there’s no eval() method in Parser class. You should call method with the name of any rule of the parser grammar e.g. query_specification(). In the grammar itself there were some errors looking as a typo, some undefined Java error() method calls, skip() calls in parser that are only suitable in lexer. You see all fixes in this commit. I’ve published my research in this GitHub repository.
I started to fix obvious errors of the grammar, which led to the compilation errors in generated java code. I had the same errors that you posted. Eventually I have fixed all Java syntax errors but faced another one which it impossible to fix directly because it originates from limitation of JVM, the compilation error: code too large. Reading ANTLR mailing list there was a hint to extract some static members of the huge classes into separate interfaces and “implement” them to have a sort of multiple inheritance. With trial and error I ended up with 6 interfaces ”imlemented” by parser in sql2003Parser.java.
But still there are 2 problems:
Wrong start rule. Douglas Godfrey wrote grammar that starts with sql2003Parser rule. Unfortunately if you call parser by this start rule, it won’t parse correctly even simplest select a from b. So I call parser by query_specification rule to parse SELECT clause only.
Some other errors in grammar. I didn’t dig too deep in the grammar but query_specification fails to parse some random complex SQLs.

how to use IConsoleLineTracker for reading console in eclipse

I have developed a plugin in which a java application is launched.In the console log of this application i want to be notified when new lines added.I have searched internet and found org.eclipse.debug.ui.consoleLineTrackers extention point.I have used it like below.
<extension point="org.eclipse.debug.ui.consoleLineTrackers">
<consoleLineTracker
id="com.plugin.util.MyConsoleTracker"
class="com.plugin.util.MyConsoleTracker"
processType="MyProcessType">
</consoleLineTracker>
</extension>
Then in my java code i have launched application like below.
config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH, classpath);
config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH,false);
config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME,MAIN_CLASS_NAME);
config.setAttribute(IProcess.ATTR_PROCESS_TYPE, "MyProcessType");
ILaunch launch = DebugUITools.buildAndLaunch(config, ILaunchManager.DEBUG_MODE,new NullProgressMonitor());
After these, i can launch my application successfully however my class com.plugin.util.MyConsoleTracker is never called.I cant see any error log either.
can anybody please tell me what i am missing here?
For an example you can see how it is made in m2e-core:
http://git.eclipse.org/c/m2e/m2e-core.git/tree/org.eclipse.m2e.launching/src/org/eclipse/m2e/internal/launch/MavenConsoleLineTracker.java
and
http://git.eclipse.org/c/m2e/m2e-core.git/tree/org.eclipse.m2e.launching/plugin.xml
Methed of YourConsoleLineTrackers should be evoked each time when a new line is showed up in console. Inside Line Tracer you have to distinguish to which process this line belongs to.
Marek

Loading external source code and using them internally (by re-compiling or something)

Is there a way in using externally stored sourcecode and loading it into a Java programm, so that it can be used by it?
I would like to have a program that can be altered without editing the complete source code and that this is even possible without compiling this every time. Another advantage is, that I can change parts of the code like I want.
Of course I have to have interfaces so that it is possible to send data into this and getting it back into the fixed source program again.
And of course it should be faster than a pure interpreting system.
So is there a way in doing this like an additional compiling of these external source code parts and a start of the programm after this is done?
Thank you in advance, Andreas :)
You need the javax.tools API for this. Thus, you need to have at least the JDK installed to get it to work (and let your IDE point to it instead of the JRE). Here's a basic kickoff example (without proper exception and encoding handling just to make the basic example less opaque, cough):
public static void main(String... args) throws Exception {
String source = "public class Test { static { System.out.println(\"test\"); } }";
File root = new File("/test");
File sourceFile = new File(root, "Test.java");
Writer writer = new FileWriter(sourceFile);
writer.write(source);
writer.close();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
compiler.run(null, null, null, sourceFile.getPath());
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
Class<?> cls = Class.forName("Test", true, classLoader);
}
This should print test in stdout, as done by the static initializer in the test source code. Further use would be more easy if those classes implements a certain interface which is already in the classpath. Otherwise you need to involve the Reflection API to access and invoke the methods/fields.
In Java 6 or later, you can get access to the compiler through the javax.tools package. ToolProvider.getSystemJavaCompiler() will get you a javax.tools.JavaCompiler, which you can configure to compile your source. If you are using earlier versions of Java, you can still get at it through the internal com.sun.tools.javac.Main interface, although it's a lot less flexible.
Java6 has a scripting API. I've used it with Javascript, but I believe you can have it compile external Java code as well.
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/scripting/
Edit: Here is a more relevant link:
"Dynamic source" code in Java applications

Categories

Resources