ANTLR for IntelliJ 15 - parsing

I am trying to use the ANTLR4 plug-in for IntelliJ to create a simple expression analyzer.
I have seen a few websites and questions but I don't seem to be able to get it running.
I have watched this video but I still get an error
Can't load hello as lexer or parser
Does anyone have a way of using ANTLR to create a grammar and then using standard input or a text file input to test the grammar and print out.
I am trying to take an infix expression and convert it to a postfix expression.
Also is there a way to use ONLY intellij to write, compile and run the program rather than swapping to the command line?
Thank you.

I ran into the same problem. I installed the ANTLR plugin for IntelliJ 15. I created an Java project called Antlr, and created the example Hello.g4 text file by right-clicking on the src directory node and selecting New->File. Once the grammar was typed into Hello.g4, I compiled it by right-clicking on the Hello.g4 tab and selecting Compile "Hello.g4", which created the template files in directory "gen".
I wasn't able to figure out how to run the grun example in the Antlr4 reference, where "hello parrt" was parsed and analyzed. Instead, it turns out that if you right click on the rule ('r') in Hello.g4, there is an option called "Test Rule r". Select that, and you get a couple of small windows at the bottom of the IDE. You can either type in "hello parrt" and it will parse it; or create a new text file by right clicking on the src directory node and selecting New->File, add "hello parrt" to the file, compile that file which will put it in the production/Antlr directory, and then right click on the rule ('r') again to select "Test Rule r'. Then you'll see the parse tree to the right.

Related

Multiline command-line editing in Gforth console

I have just started learning the Forth programming language.
I'm using Gforth on Ubuntu. In Gforth interactive console, I want to do indentation but it requires changing line. Enter key didn't work, it executed code. For comparison, for example, when one tests JavaScript code in web browser console, shift+enter change line without executing code. I want something like that. What key should I press? Is there a way other than using text editors like vim?
Best.
Gforth doesn't support multiline editing (see the manual).
A workaround is to edit a file in your favorite editor in another window and reload this file in Gforth console as:
include /tmp/scratch.fs
An external file can be also edited in Gforth console via a command like:
"vim /tmp/scratch.fs" system
So a one-liner for that is:
"vim /tmp/scratch.fs" system "/tmp/scratch.fs" included
That can be wrapped into a definition as:
: scratch "vim /tmp/scratch.fs" system "/tmp/scratch.fs" included ;
So the word scratch will open an editor and than load the edited file.
NB: if you use a quite old build of Gforth, you have to use s" ccc" instead of "ccc" for string literals.
To conditionally include/exclude some parts in a file the words [defined] and [if] can be used; to erase the previous instance of the loaded definitions the word marker can be used as:
[defined] _clear [if] _clear [then]
marker _clear
\ some definitions
\ ...
Take into account that usual control-flow words can be used in definitions only.

Atom keybindings and the LaTeX package

I want to change the keybinding for building in LaTeX from the default ctrl-alt-b to cmd-b. Settings suggests I copy paste
'atom-text-editor[data-grammar~='latex']':
'cmd-b': 'latex:build'
into the keymap.cson file but upon saving it I get the error message
[stdin]:20:34: error: unexpected latex
'atom-text-editor[data-grammar~='latex']':
^^^^^
Since the whole expression is inside a pair of single quotes, you need to use different quotes around latex:
"atom-text-editor[data-grammar~='latex']":
or
'atom-text-editor[data-grammar~="latex"]':
Also note, that you will likely run into a conflict with existing keybindings. The keybinding-resolver package is a great helper to find conflicting keybindings.

How can a parser and a lexer work together if they're in separate packages?

This question concerns Antlr, the parser/lexer generator (Which is pretty awesome IMO). Specifically, the version in question is Antlr4. Currently I'm playing around trying to create a parser/lexer combo in separate files, which worked well at first.
However, when I tried to modularize the different components, for organization's sake, I discovered an issue. The two tools I'm using to modularize, package declarations in headers and setting the parser's token vocab, work perfectly separately, but I cannot seem to get them to play nice together.
I've put together a very short example that illustrates my issue.
First, I've defined my lexer:
lexer grammar UsefulLexer;
#header{
package org.useful.lexer;
}
USEFUL_TOKEN:'I\'m useful, I promise!';
Second I've defined my parser.
parser grammar UsefulParser;
#header{
package org.useful.parser;
}
options{
tokenVocab=UsefulLexer;
}
usefulRule:USEFUL_TOKEN*;
But when I build, I get the useful error:
cannot find tokens file /Users/me/Desktop/Workspace/Project_Name/src-gen/org/useful/parser/UsefulLexer.tokens
All the rules together work perfectly together in a combined grammar, or even separately, provided they are in the same package. However, for how I'm using Antlr, with multiple parsers sharing the same lexer, having all the components in the same package defeats the purpose of using packages in the first place.
I've consulted the docs, especially the section on grammar structure, and I can't find an official source for how to fix this. I've also tried the obvious solution, changing tokenVocab=UsefulLexer to tokenVocab=org.useful.lexer.UsefulLexer, but that doesn't even parse. (Which I find somewhat ironic.)
What is the syntax I am missing? Or is this just something that there isn't syntax for?
Have to build both the lexer and parser. Here is a simple test rig builder:
#echo off
rem Execute the Antlr compiler/generator tool
rem put grammar files in "D:/DevFiles/Java/src/test/parser"
SETLOCAL
set files=../UsefulLexer.g4 ../UsefulParser.g4
set CLASSPATH=D:/DevFiles/Java/lib/antlr-4.5-complete.jar
set tool=org.antlr.v4.Tool
set cmd="C:/Program Files/Java/jre7/bin/java.exe"
set opts=-visitor
cd /d D:/DevFiles/Java/src/test/parser/gen
%cmd% %tool% %opts% %files%
ENDLOCAL
pause
rem timeout 5
To solve this, I had to modify my ANTLR build command for both the lexer and the parser, adding the -lib & -package options. Once I pointed -lib at the package of my lexer in my parser buildscript, and moved my package declarations to the build commands in both, it was smooth sailing.
Hope this helps someone else!

Sublime Text selection using Lua/love2d

say i have this line of code :
Object.Property.field;
Object.Property:FunctionName();
in all my sublime languages if i was to double click "Property" on either line, it would select just that word.
For some reason my lua/lua love2d syntax highlighting selects the whole line up to the ":"
How can I change this behavior, so it will only select the single word?
The reason this is occurring is because of a somewhat strange addition to the Lua Love plugin, which I assume you're using. You're using Sublime 2, so select Preferences -> Browse Packages... to open up your Packages folder, then open the Lua Love subfolder. There is a file called completions.py, which has this content:
#completions.py
import sublime
import sublime_plugin
import re
class LoveCompletions(sublime_plugin.EventListener):
ST = 3000 if sublime.version() == '' else int(sublime.version())
def on_query_completions(self, view, prefix, locations):
if self.ST < 3000 and ("lua" in view.scope_name(locations[0])):
seps = view.settings().get("word_separators")
seps = seps.replace('.', '')
view.settings().set("word_separators", seps)
Even if you don't know Python, the logic is pretty easy to follow. It sets the variable ST to Sublime's version, which is 3000+ if you're using ST3 (current build is 3061), and is 2221 (I think) for ST2. It then sets up an event listener (the process is always running in the background) checking to see if the Sublime version is less than 3000 (you're using ST2) and you have lua in your current scope (basically, your file is source.lua or source.lua.love, if you're using the plugin's language definition). If both of those are true, it removes the . character from your "word_separators" setting, which is defined in Preferences -> Settings-Default and can be overridden in Preferences -> Settings-User.
The word_separators setting controls what characters are considered to be word separators when double-clicking to select a word. Its default value is ./\\()\"'-:,.;<>~!##$%^&*|+=[]{}`~? so, for example, if you double-click on the foo part of foo-bar Sublime will only select foo, but if you double-click on the foo part of foo_bar Sublime will select the whole thing (since - is in word_separators). . is in word_separators by default, so double-clicking on foo in foo.bar will only select foo, which is expected behavior for most people, I would assume. However, this cute little plugin removes . from word_separators in Sublime Text 2, so in your case clicking on Property selects everything from the beginning of the "word" (the whitespace before Object) to the next word separator - the :, in the case of your second example.
OK, so we know what the problem is, how do we fix it? First, while you're in Packages/Lua Love, just delete completions.py altogether. There's no harm in doing so, and in fact it's actually causing harm by being there. Make sure you restart Sublime after deleting the file. Next, open Preferences -> Settings-User and add . back into the word_separators list, anywhere between the beginning and ending double-quotes. Save that file, go back to your source code, and double-clicking should once again behave normally.
Good luck!
EDIT
I submitted this pull request to delete the completions.py file from the plugin's Github repo, and it was just merged, so hopefully users in the future won't have to deal with this :)

Running spss syntax file on multiple data files automatically

I have a spss syntax file that I need to run on multiple files each in a different directory with the same name as the file, and I am trying to too do this automatically. So far I have tried doing it with syntax code and am trying to avoid doing python is spss, but all I have been able to get is the code bellow which does not work.
VECTOR v = key.
LOOP #i = 1 to 41.
GET
FILE=CONCAT('C:\Users\myDir\otherDir\anotherDir\output\',v(#i),'\',v(#i),'.sav').
DATASET NAME Data#i WINDOW=FRONT.
*Do stuff to the opened file
END LOOP.
EXE.
key is the only column in a file that contains all the names of the files.
I am having trouble debugging since I don't know how to print to the screen if it is possible. So my question is: is there a way to get the code above to work, or another option that accomplishes the same thing?
You can't use an expression like that on a GET command. There are two choices. Use the macro language to put this together (see DEFINE in the Command Syntax Reference via the Help menu) or use the SPSSINC PROCESS FILES extension command or your own Python code to select the files with a wildcard.
The extension command or a Python program require the free Python Essentials available from the SPSS Community website or available with your Statistics version.

Resources