# Grammar of rust

**URL:** <https://internals.rust-lang.org/t/grammar-of-rust/4094>\
**Category:** compiler\
**Created:** [September 17, 2016, 9:38am UTC](https://internals.rust-lang.org/t/grammar-of-rust/4094 "2016-09-17T09:38:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dns2utf8](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dns2utf8/32/1713_2.png) [@dns2utf8](https://internals.rust-lang.org/u/dns2utf8)\
**Post date:** [September 17, 2016, 9:38am UTC](https://internals.rust-lang.org/t/grammar-of-rust/4094/1 "2016-09-17T09:38:39Z")

</div>

Hi all

I started in june with trying to verify the grammar inside the compiler. So I started to compile the `make check-grammar` target. [https://github.com/rust-lang/rust/pull/34994](https://github.com/rust-lang/rust/pull/34994)

Now I am a little stuck with understanding the current grammar infrastructure. Can anyone give me a little intro?

I am currently at rustfest.eu if you are here as well, please find me.

Regards Stefan

---

<div class="post-metadata">

**Author:** ![matklad](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/matklad/32/12266_2.png) [@matklad](https://internals.rust-lang.org/u/matklad)\
**Post date:** [September 17, 2016, 10:06am UTC](https://internals.rust-lang.org/t/grammar-of-rust/4094/2 "2016-09-17T10:06:58Z")

</div>

The grammar situation in general is quite poor at the moment, the language is basically defined by the parser. There is a plan to make things better ([GitHub issue](https://github.com/rust-lang/rust/issues/30942)) and provide a reference grammar, but it is far from completion.

I don’t know about grammar checking infra in the repo, but I guess it should start with a proper grammar definition, and there isn’t one at the moment.

Currently available options are:

- [https://doc.rust-lang.org/grammar.html](https://doc.rust-lang.org/grammar.html) — very incomplete
- [https://github.com/rust-lang/rust/blob/master/src/grammar/parser-lalr.y](https://github.com/rust-lang/rust/blob/master/src/grammar/parser-lalr.y) — not maintained (has some removed features, don’t have some recent features), and a bit difficult to read imo.
- [https://github.com/jorendorff/rust-grammar](https://github.com/jorendorff/rust-grammar) — don’t know much about this one.
- [https://github.com/rust-lang/rust/blob/master/src/libsyntax/parse/parser.rs](https://github.com/rust-lang/rust/blob/master/src/libsyntax/parse/parser.rs) — the only real definition of syntax. It is quite readable though (better than parser-lalr.y for me). I use it as a reference for
- [https://github.com/intellij-rust/intellij-rust/blob/master/src/main/kotlin/org/rust/lang/core/grammar/rust.bnf](https://github.com/intellij-rust/intellij-rust/blob/master/src/main/kotlin/org/rust/lang/core/grammar/rust.bnf) — I think this is the only grammar like thing which is more or less complete, but its very unreadable because of the IntelliJ specific stuff.

---

<div class="post-metadata">

**Author:** ![matklad](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/matklad/32/12266_2.png) [@matklad](https://internals.rust-lang.org/u/matklad)\
**Post date:** [September 17, 2016, 10:10am UTC](https://internals.rust-lang.org/t/grammar-of-rust/4094/3 "2016-09-17T10:10:41Z")

</div>

Ah, and the README at [https://github.com/rust-lang/rust/tree/master/src/grammar](https://github.com/rust-lang/rust/tree/master/src/grammar) is completely misleading

In that folder, only the lexer is implemented in ANTLR4 (a Java tool). I think there was some code to compare it with rustc native lexer, but it isn’t run as part of the build.

Also that folder contains a lexer and parser definition for lex/bison (C tools). There is no code that compares syntax trees though.

---

<div class="post-metadata">

**Author:** ![dns2utf8](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dns2utf8/32/1713_2.png) [@dns2utf8](https://internals.rust-lang.org/u/dns2utf8)\
**Post date:** [September 17, 2016, 10:20am UTC](https://internals.rust-lang.org/t/grammar-of-rust/4094/4 "2016-09-17T10:20:40Z")

</div>

My first goal was to finish the antlr4 grammer to create a lexer. This would enable checks like if the lexer does not accept a source file, the compiler must not accept it.

---

<div class="post-metadata">

**Author:** ![graydon](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/graydon/32/643_2.png) [@graydon](https://internals.rust-lang.org/u/graydon)\
**Post date:** [September 17, 2016, 9:25pm UTC](https://internals.rust-lang.org/t/grammar-of-rust/4094/5 "2016-09-17T21:25:48Z")

</div>

parser-lalr.y, parser-lalr-main.c, tokens.h and lexer.l are extracts from the more-complete rust-grammar repo: [https://github.com/bleibig/rust-grammar](https://github.com/bleibig/rust-grammar)

It is the most up-to-date grammar I’m aware of; everything pre-2015 – when the language underwent its final rush-to-stability – is probably junk due to constant grammar churn (all the ANTLR work is hopelessly bitrotted, and it’s not an appropriate tool anyways).

IMO Rust isn’t ideally thought of as LL(k) anymore; I tried to keep it that way for a long time, but it’s grown a lot of bits that work better in LR-family. I highly recommend just deleting anything ANTLR-related and focusing on LR(k) or LALR(k) grammars.

The [verify.rs](http://verify.rs) and [testparser.py](http://testparser.py) tools are components of the rust-grammar repo, but only parts; they’re intended to be used with the rlex / rparse stubs in that repo, that generate a comparable syntax tree dump from the production compiler. This is a reasonable approach to bringing a grammar-derived parser up to parity, but it’s bitrotted some. Also, of course, true lex-and-yacc are not likely the targets you want to stick with long term; they’re a stopgap.

Niko is working on [https://github.com/nikomatsakis/lalrpop](https://github.com/nikomatsakis/lalrpop) which should, eventually, be a good target for building an LR-family frontend for Rust, based on a grammar similar-to the one in the yacc file.

Hth.

---

<div class="post-metadata">

**Author:** ![graydon](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/graydon/32/643_2.png) [@graydon](https://internals.rust-lang.org/u/graydon)\
**Post date:** [September 17, 2016, 9:33pm UTC](https://internals.rust-lang.org/t/grammar-of-rust/4094/6 "2016-09-17T21:33:48Z")

</div>

Hm, further digging. These are the 3 best (live, post-2015) resources:

[https://github.com/bleibig/rust-grammar](https://github.com/bleibig/rust-grammar) – Source of the lex/yacc (LALR) grammar in the Rust repo [https://github.com/nikomatsakis/rustypop](https://github.com/nikomatsakis/rustypop) – Niko’s partial port of that from yacc to LALRPOP [https://github.com/jorendorff/rust-grammar](https://github.com/jorendorff/rust-grammar) – Jason’s ANTLR4 grammar

I would recommend focusing on Niko’s (rustypop), though it might be worth emailing Jason to ask what the story is on his. I suspect it was made as part of the Programming Rust book [http://shop.oreilly.com/product/0636920040385.do](http://shop.oreilly.com/product/0636920040385.do)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:27am UTC](https://internals.rust-lang.org/t/grammar-of-rust/4094/7 "2019-03-25T08:27:00Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
