[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
SMIE grammar for C-style function expressions
From: |
Nikolay Kudryavtsev |
Subject: |
SMIE grammar for C-style function expressions |
Date: |
Mon, 27 Sep 2021 17:39:47 +0300 |
User-agent: |
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 |
Hello.
Recently, I've been working on a major mode with an indentation
implementation based on SMIE. I've been also collecting some notes with
the plan of writing a post on reddit, describing the experience gained.
Since the language I'm working on is kind of a nightmare to indent, some
part of my post is going to be SMIE-related.
With this in mind, I still have a very basic case that I don't think I
100% grok, so I've decided to ask here. The case is this: what's the
proper way to describe SMIE grammar for C-style functions? I remember
Stefan talking that he has a prototype SMIE C implementation, but I
don't think he ever published it.
To explain, a C-style function definition grammar looks something like this:
f n ( a ) { s }
'f' here is a keyword, which in practice would be "function" or say
"def" like in Python so lets mark it like this:
"f" n ( a ) { s }
Both '( a )' and '{ s }' are lists and SMIE grammars generally prefer to
abstract those, to keep parens considered openers and closers. So now
our grammar looks like this:
args = ( a )
stmts = { s }
"f" n args stmts
Now the grammar we ended up with is invalid in SMIE because non
terminals appear consequently in it. The simplest solution would
probably be using virtual keywords, to separate our non terminals:
"f" n " : " args " : " stmts
This of course puts a bit of a strain on the lexer, since it has to
recognize when to give out those tokens.
So, is this the recommended solution for such cases, or maybe there's
some other preferred way?
- SMIE grammar for C-style function expressions,
Nikolay Kudryavtsev <=