Motoko grammar explanations?

The motoko sdk grammar doc was autogenerated with no explanation of the symbol meanings, so it’s not very helpful. Is there a guide that explains the grammar/syntax in more detail?

Maybe you are looking for this section? Language quick reference :: Internet Computer

2 Likes

See the section that @nomeata points to for detailed explanations.

As for the notation used in the auto-generated grammar, it is pretty much standard BNF, with the only addition of optional phrases (...)?.

1 Like

Hi Folks

I am trying to build a parser for mo files for an analysis project.

However, what I don’t get is the first two rules, they don’t seem to be part of the standard BNF syntax
e.g. <list(X,SEP)> this looks like a template for a rule, I can’t seem to locate this in the official BNF syntax. I am trying to parse the grammar in ANTLR4 and it seems to not recognize the first two rules which have this syntax. Typically an BNF id is meant to just be a string of chars with ‘-’ or numbers (according to the antlr grammar for BNF this is what an ID is supposed to be made of

id_
: LT ruleid GT
;

ruleid
: ID
;

ID
: (‘a’…‘z’|‘A’…‘Z’) (‘a’…‘z’|‘A’…‘Z’|‘0’…‘9’|‘-’|’ ')+
;

and a rule is

rule_
: lhs ASSIGN rhs
;

where the ASSIGN is ::==

ASSIGN
: ‘::=’
;

So a little lost here, what syntax do the first two rules follow, they almost look like meta-rules. Is the tool that generates them something internal or open source?

Any help will be appreciated, a little lost here…

Regards

Mesbah