Can I use the ANTLR Java API to generate the lexer/parser as streams and save them somewhere other than some files?
Also, is there a simple example of using the API to generate the required files from a given grammar?
thanks
I am not 100% sure if I understand your question correctly, but you might want to have a look at https://stackoverflow.com/a/38052798/5068458. This is an in-memory compiler for antlr grammars that generates lexer and parser for a given grammar in-memory. You do not have to do it manually. Code examples are provided there.
Best,
Julian
Related
In the tutorial of custom language support for Intellij Plugin development, it uses GrammarKit to generate the Parser and JFlex (patched) to generate the Lexer.
On this page the author says we can "use the GrammarKit plugin to generate lexer and parser".
Is there a particular reason that JetBrains suggest using JFlex to generate the lexer? How different the process would be if we use GrammarKit to generate both?
Well, I think I know the answer now.
The short answer is NO. It is a little bit complex than that.
Grammar-Kit can be used to generate .flex file and you still need JFlex generator to generate the Java lexer.
Both steps can be done via the context menu.
See here for more info.
I'm wondering if it is possible to use Antlr v4 with Xtext?
I know currently (and the near future) Xtext is relying on Antlr v3 because they say they'd have to rewrite all their functions to fit Antlr v4, but I thoughtit might be possible to either add the Antlr v4 library to the buildpath of the Xtext onstead of let it download the v3 itself or if that is not possible if it is possible to generate the Parser with Antlr v4 and then insert the generated java code into Xtext.
Or could you think of another way using v4 with Xtext?
If you are wondering why I want to use v4: As far as I know it automatically handles ambigious grammar (it rewrites the grammar so everything is solvable by the computer) and that is the thing I'mstruggling with the most
Thanks for your help
Krzmbrzl
No, there is no trivial way to plug ANTLR4 into Xtext.
Note that ANTLR4's parsing algorithms are more powerful than those of ANTLR3, but it isn't magic. If you have a highly ambiguous grammar, you might (likely?) still get issues with v4.
I recommend you create a new question and post your Xtext grammar and explain where you're having issues.
I have built an antlr v4 grammar for parsing Cobol files. It is tested and fully functional. Now I need to adapt it to be used within a XText project (with unfortunately uses antlrv3). How can I achieve it without backporting my grammar (and loosing all already built Listeners and Visitors)?
After a few thinking about the problem I am wondering if there is a way to generate antlr v3 interface adaptors to use antlr v4 Parser and Lexer. If so, i could "tweak" XText, so it would be using my already built antlrv4 classes through this adaptor interface.
Anyone already had done something like this?
We are currently working on this problem as well. Is there a possibility to look at your grammar file?
I am considering writing a markdown parser in Objective C.
Can anyone explain the basic algorithm of doing this?
Is there any library I can look at? I'm not familiar with C. Python or Obj-C implementation would be nice.
The parser should not focus on HTML or any other output. Instead it focuses on detecting the markdown syntax.
Thanks
Markdown is essentially specified by the original Perl script that parses it.
It doesn't fit neatly into a standard parser architecture unfortunately (there isn't a formal grammar as such), it is based on a multi-pass set of string manipulations and regular expression pattern matching/replacement. If you read through the Perl script you will see what I mean.
The best way to achieve your goal is to hand transcode the Perl script into Obj-C, as has been done for other languages.
Has somebody used both for Language implementation and
is able to compare them, pointing out strengths and
weaknesses? I seek a RAD tool with support for
AST Walker Code generation. SableCC is LALR and thus
supports ´Left recursion´, whereas ANTLR is LL(*).
Is this important for typical grammars or DSLs? I need
to perform some domain-specific analysis as well. (The target
language of my compiler will be OpenCL C). As this will be
for a student project it is important that I do not lose
that much time on the tedious side, that is implementing
the Front-End of the language.
I cannot say much about ANTLR, but maybe some information about SableCC.
Design
It generate a parser, which generated code and hand-written code are clean separated using Visitor pattern, and integrates the transform from Concrete Syntax Tree to Abstract Syntax Tree. As a result the designer can get a AST after the parser parses successful the input, and he can walk through the tree and make action on corresponding nodes.
The designer can first write and debug his grammar, try to optimize the transform from Concrete Syntax Tree to Abstract Syntax Tree. After he has a solid AST he can write action code in separated class. So the designer write grammar only once and can write more type of action for the grammar, for example once for Syntax Highlight, once for Semantic analysis and code generator. I have done it in a productive system. It works very well.
With ANTLR the designer can construct the AST tree by adding action code in grammar t generate the AST, then reuses it for different manner. But there a not a clean separation between generated code and hand-written code.
An other aspect maybe support of IDE. Since with SableCC you have separated code, you can easy use auto-complete function of IDE.
grammar
SableCC is a LR(1) parser generator, so it is IMO easier to write grammar for ANTLR, which is a LL(k) parser generator, (without trick). I think (aber not sure) SableCC is the only one LR(1) java parser generator, which is so popular.
output parser
ANTLR can generate parser in many languages, while SableCC can only generate parser in Java (mainstream). There some plugin / adapter to generate parser in other language, however according to the author (http://www.mare.ee/indrek/sablecc/) they are too old. SableCC 4 can generate more, but it is in beta, which is not recommend for serious project.
Development Support
ANTLR hat a IDE to write grammar. It is ANTLRWorks, which can visual grammar, navigate in source (like jump to definition of token or production). SableCC hat no such tools. There are primitive Syntax Highlight script for VIM and a poor feature plugin for Netbeans.
Conclusion
IMO I think for big project, required long term maintenance SableCC is more suitable than ANTLR.
Martin Fowler has a informative about SableCC, you can find it here.
http://martinfowler.com/bliki/HelloSablecc.html