What's the relationshiop between Xtext and ANTLR? - parsing

I heard that Xtext ultimately uses ANTLR but their grammar specification files have somewhat different formats. So what's the relationship between the two?

Xtext relies on the Antlr parser generator for the parsing of input files. On top of that the framework provides lot's of added value such as strongly typed ASTs, abstractions for linking and static analysis as well as IDE integration for Eclipse.
For that purpose, Xtext generates two Antlr grammars. One for the production parsing where the actual AST is produced, and a second grammar that is used to consume events to compute the content proposals for the Eclipse editor.

ANTLR grammar is generated from Xtext. You may find it in src-gen/org/example/dsl/parser/antlr/internal/InternalDsl.g.

Related

Get clang/llvm parser from yacc parser

I'm trying to build a parser for Promela in llvm. I have the parser SPIN uses, which is built using yacc, including the input that goes to yacc. Is there a way to use the yacc parser to quickly and painlessly generate a clang/llvm parser? I will be using it to generate call graphs and perform static analysis.
What I need to know now is whether I can use the existing Promela compiler, which was built with yacc, to quickly build a parser (and later, IR generator) using the llvm framework.
Yes, you can re-use the existing YACC-grammar (and if you want even the existing AST) for your project. "Building a parser using the llvm framework" is a bit misleading though because LLVM won't have anything to do with parsing and the AST. LLVM won't enter into it until you generate the LLVM IR and then work with it.
So you either take the existing YACC grammar and the existing AST or you only take the grammar and replace the actions with ones that create your own AST that you've defined yourself. Either way that part won't involve LLVM.
Then you'd write a separate phase that walks the AST and generates LLVM IR using the LLVM API, on which you can then run all the transformations and analyses supported by LLVM.

Autocomplete punctuation using grammar

I'm working on a tool that is able to autocomplete the necessary literals defined in a grammar. For example: in C# if a programmer enters: for with a space after it, then it's entirely possible to parse the code, determine that the programmer has started a for statement and autocomplete the necessary punctuation: ( ; ; ).
The more I think about the problem, the more I think there must already be a solution for it, because it's such a common use case, but I can't find anything.
Is there a tool that can do this using a given grammar?
If you don't mind using ANTLR v3 instead of v4 you can use Xtext in order to generate an editor that features auto-complete and error-highlighting. This will happen in form of a plugin for eclipse and apparently also for IntelliJ IDEA.
If you want to use a different IDE or simply want to make use of ANTLR v4's powerfull features you could still have a look at the Xtext Sources as they have to do what you are searching for in order to provide the above mentioned features properly...
This package looks very promising at first glance... You might find the respective code in there.
Be aware though that Xtext is mainly written with Xtend so you either have to do so as well or you have to rewrite it a bit

Bison C++ GLR parser using variants

I am currently creating a parser using bison, which uses the variant feature heavily, since my grammar is not LALR(1) I want to use the GLR option.
When I try to do so I get the following error:
error: '"glr.cc"' does not support '%define api.value.type variant'
%define api.value.type variant
^^^^^^^^^^^^^^
What am I doing wrong?
Note: The answer below was valid when written and for the subsequent four and a half years. However, in Bison v3.8 (released September 7, 2021), a new experimental C++ GLR implementation was included which does support variant semantic types. You can test this skeleton, if you have updated your bison installation to version 3.8, by adding the directive %skeleton "glr2.cc". The Changelog indicates:
It will eventually replace "glr.cc". However we need user feedback on this skeleton. Please report your results and comments about it.
To me, this suggests that it should not yet be used in production code, but undoubtedly this warning will become invalid sometime in the next four years. In the meantime, use your own judgement or read the answer below from 2017.
You are trying to build a GLR parser using the C++ API with a semantic type which is not POD, and that is not supported by the current C++ Bison GLR implementation.
In particular, the variant type used by Bison's C++ API is not POD, and so it cannot be used in a GLR parser, as the error message states.
The only workaround I know of is to use C-style discriminated unions with a tag field and a union.
If you look at the "examples/c++/glr/c++types.yy", you will know this can be solved by using latest version with skeleton "glr2.cc" by
%require "3.8"
%skeleton "glr2.cc"
Languages are measured by LR(k), for some k. Languages are not measured by the term GLR. GLR is a parsing technique.

Multiple language parser generator

Is there a parser generator that can take a single grammar and create a parser in both c# and javascript?
I've tried using ANTLR, but I have yet to get it into Visual Studio (lackluster/outdated documentation and packages).
The end goal is that I can manage a single grammar (for a subset of SQL; specifically select statements and a few new keywords specific to my problem domain) but generate two parsers (c#/javascript).
Any help is much appreciated.
Is there a parser generator that can take a single grammar and create a parser in both c# and javascript?
The only one I am aware of is ANTLR. Note that ANTLR will not generate both a JavaScript- and C# based parser in one go though. You will have to change (at least) one option in the grammar and invoke org.antlr.Tool to generate a parser for your other target language.
I've tried using ANTLR, but I have yet to get it into Visual Studio
Then don't use Visual Studio, but use your favorite text editor (and use org.antlr.Tool from the console), or ANTLRWorks.
There's canopy, which targets javascript, ruby, java, and python from PEG
My AGL parser builder is written in Kotlin common, so it can be used on any Kotlin target (JVM, JavaScript, native code, etc).
https://medium.com/#dr.david.h.akehurst/a-kotlin-multi-platform-parser-usable-from-a-jvm-or-javascript-59e870832a79
Unfortunately, Kotlin does not yet target .net....but maybe it will come in the future.
docopt let you describe your help message in a string respecting some common convention and that's how all commands, options and arguments will be defined.
Docopt has many official implementations: python, bash, C#, rust, ruby, C++, Go, R, Julia, Nim, Hashkell, PHP, C, F#, CoffeeScript, Swift, Scala, D, Java, Clojure, Tcl, Lua

Parser generator for Delphi?

Can anyone recommend a parser generator that will produce win32 Delphi code? What I'm trying to do is create a simple Domain-Specific Language.
How complex is your DSL?
I created a parser (in Delphi) for the new Delphi RIDL language to support some in-house COM generation tools we use.
My approach was to use ANTLR to play around with the syntax rules until I had something that parsed the various test files I had. I then hand-coded a recursive descent parser (based on the Java generated by ANTLR). I was also using the Castalia Delphi Parser in the project, so I based my lexical analyser on that.
Recursive descent parsers are actually really simple (but tedious :-) ) to write manually.
The Delphi versions of Coco/R are quite ok:
http://www.ssw.uni-linz.ac.at/Research/Projects/Coco/
most notably Pat Terry's one:
http://www.scifac.ru.ac.za/coco/
Try this:
http://www.grendelproject.nl/dyacclex/
I have not tried this at all and I just noticed the information while I was reading through the documentation, but FastScript, in their documentation which you can get here, says that you can use their parser to create your own language which you specify in an XML file. This might give you something you can use.
I found Antlr For Delphi 3.1.1.
SharpPlus Antlr For Delphi Target
3.1.1, a language tool that provides a framework for constructing
recognizers, interpreters, compilers
with Delphi!
It is a commercial product. I have no idea what it is like.

Resources