SMIE Grammar (GNU Emacs Lisp Reference Manual)

Next: , Previous: , Up: Simple Minded Indentation Engine [Contents][Index]


24.8.1.3 Defining the Grammar of a Language

The usual way to define the SMIE grammar of a language is by defining a new global variable that holds the precedence table by giving a set of BNF rules. For example, the grammar definition for a small Pascal-like language could look like:

(require 'smie)
(defvar sample-smie-grammar
 (smie-prec2->grammar
 (smie-bnf->prec2
 '((id)
 (inst ("begin" insts "end")
 ("if" exp "then" inst "else" inst)
 (id ":=" exp)
 (exp))
 (insts (insts ";" insts) (inst))
 (exp (exp "+" exp)
 (exp "*" exp)
 ("(" exps ")"))
 (exps (exps "," exps) (exp)))
 '((assoc ";"))
 '((assoc ","))
 '((assoc "+") (assoc "*")))))

A few things to note:


Next: Defining Tokens, Previous: Operator Precedence Grammars, Up: Simple Minded Indentation Engine [Contents][Index]

AltStyle によって変換されたページ (->オリジナル) /