help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: SMIE grammar for C-style function expressions


From: Stefan Monnier
Subject: Re: SMIE grammar for C-style function expressions
Date: Tue, 28 Sep 2021 09:07:33 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

> I understand that SMIE does not concern itself with the AST, but I think
> it's reasonable to start with describing the language grammar for it in the
> most naive way, until we have a good reason not to.   "f n ( a ) { s }" is
> a single expression in the language, so someone implementing a grammar for
> such language may first try to declare it in SMIE as a single expression.

No, the better way to work with SMIE is to fix the problems that occur
rather than to try and first reproduce all the details of the grammar:
the SMIE grammars are almost always an approximation of the real grammar
that accepts meaningless programs as well, but we don't care.

> To try and keep the problem stated in practical terms, lets say the language
> grammar looks like this:
>
> args = ( a )
>
> stmts = { s }
>
> "f" n args stmts "f-end"

Any sequence of "atomic" elements is automatically handled by SMIE
without having to specify it in the grammar.  If SMIE knows that "("
matches ")" then "( a )" is such an "atomic" element (similarly, if
SMIE is told that "f" matches "f-end", then "f ... f-end" is also an
atomic element).
So for the above example, the grammar just needs something like:

    (defvar foo-grammar
        '((exp ("(" exp ")")
               ("{" exp "}")
               ("f" exp "f-end"))))

And you may even skip the first two entries because they're probably
already described in the syntax-table.

Now the above will understand

   f n (a) {s} f-end

like you want it to.  It will also "understand"

   f {a} n f (haha) f-end f-end

even though it's non-sensical, but we usually don't care about what SMIE
does with such nonsensical code.


        Stefan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]