Is there an ANTLR4 grammar for YAML?
Asked Answered
G

1

17

Since YAML has a rather complicated syntax, is it possible to write a parser for YAML mainly using ANTLR4 ? I was looking for examples, that implement the YAML like indentation parsing and the detection of data types.

Glutenous answered 30/8, 2014 at 6:56 Comment(5)
Indentation handling can by found in the Python grammar (github.com/antlr/grammars-v4/tree/master/python3).Dacy
Yeah, but i think YAML's indentation handling similar but still quite a bit different than pythons. yaml.org/spec/1.2/spec.html#id2777534 vs docs.python.org/3/reference/lexical_analysis.html#indentationGlutenous
Yaml identation seems to be more complex that in python. At first glance it looks like it can be achieved with several lexer modes (to cope with flow style) and lexer actions that convert whitespace to Indent|Dedent tokens, so you have not to deal with whitespace in the parser.Dacy
Worth mentioning the github repo enyaml an ANTLR + .net yaml grammar. I have not used it but have been debating forking it and porting it to java, and then changing the grammar to embed some of the domain rules about our yaml documents inside the parser. I'll update this question when that's done.Cotinga
As far as I can tell, you can handle the block syntax for YAML collections (indentation rules) inside a handwritten lexer. I used this approach myself to create a very basic YAML parser based on ANTLR here. Apart from the custom lexer (YAMLLexer.cpp), all other parts of the parser use the standard facilities provided by ANTLR (input handling, parser grammar, listener interface).Intellectualism
A
2

The YAML specification contains a BNF grammar. Bear in mind that according to this document, fully correct YAML is context-sensitive and not parseable by parser-generators, so your grammar will have to describe a context-free superset.

Alisealisen answered 9/9, 2020 at 16:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.