librdesc
Loading...
Searching...
No Matches
Macros
bnf_dsl.h File Reference

Macros for context-free grammar definitions. More...

#include <stddef.h>

Go to the source code of this file.

Macros

#define PREFIX_TK(tk)   tk
 Name mapping for token identifiers.
 
#define PREFIX_NT(nt)   nt
 
#define POSTFIX_NT_REST(nt)   nt ## _REST
 Name mapping for non-terminal list to rest types.
 
#define EOB   -1
 
#define EOC   -2
 
#define SEOB   { .ty = CFG_SENTINEL, .id = EOB }
 Sentinel struct for the end of a rule's body (EOB)
 
#define SEOC   { { .ty = CFG_SENTINEL, .id = EOC } }
 Sentinel struct for the end of a construct's variants (EOC)
 
#define TK(tk)   { .ty = CFG_TOKEN, .id = PREFIX_TK(tk) }
 Macro to create a terminal (token) production symbol.
 
#define NT(nt)   { .ty = CFG_NONTERMINAL, .id = PREFIX_NT(nt) }
 Macro to create a non-terminal production symbol.
 
#define EPSILON   SEOB
 Macro to create an epsilon production symbol.
 
#define r(...)   { { __VA_ARGS__ SEOB }, SEOC }
 Macro to define a grammar rule. Adds end-of-body and construct sentinels to grammar rules.
 
#define rrr(head, listelem, delim)
 Macro to define a pair of rules for a right-recursive list.
 
#define ropt(...)   { { __VA_ARGS__, SEOB }, { SEOB }, SEOC }
 Macro to define an optional grammar rule (epsilon production).
 
#define alt   SEOB, }, {
 Macro for syntactic sugar to separate non-terminal alternatives.
 

Detailed Description

Macros for context-free grammar definitions.

Provides macros (r, rrr, ropt) to define these production rules in a readable way.

You may define PREFIX_TK and PREFIX_NT macros before including this file to automatically add prefixes in grammar definitions.

Macro Definition Documentation

◆ alt

#define alt   SEOB, }, {

Macro for syntactic sugar to separate non-terminal alternatives.

Expands to the end-of-body sentinel and new variant syntax.

◆ EOB

#define EOB   -1

integer representing EOB (end-of-body)

◆ EOC

#define EOC   -2

integer representing EOC (end-of-construct)

◆ PREFIX_NT

#define PREFIX_NT (   nt)    nt
See also
PREFIX_TK

◆ PREFIX_TK

#define PREFIX_TK (   tk)    tk

Name mapping for token identifiers.

This macro determines how a raw token name passed to TK(...) is expanded into a C identifier (typically an enum member).

  • Default: Identity mapping. TK(IDENT) expands to IDENT.
  • Override: Define this macro before including the DSL to add namespaces (e.g., #define PREFIX_TK(x) MY_TK_ ## x).

◆ ropt

#define ropt (   ...)    { { __VA_ARGS__, SEOB }, { SEOB }, SEOC }

Macro to define an optional grammar rule (epsilon production).

This is a shortcut for a rule with two variants:

  1. The symbols provided in __VA_ARGS__
  2. An empty (epsilon) production

◆ rrr

#define rrr (   head,
  listelem,
  delim 
)
Value:
{ { listelem, NT(POSTFIX_NT_REST(head)), SEOB }, SEOC }, \
{ { delim, NT(head), SEOB }, { SEOB }, SEOC }
#define SEOB
Sentinel struct for the end of a rule's body (EOB)
Definition: bnf_dsl.h:52
#define NT(nt)
Macro to create a non-terminal production symbol.
Definition: bnf_dsl.h:63
#define POSTFIX_NT_REST(nt)
Name mapping for non-terminal list to rest types.
Definition: bnf_dsl.h:39
#define SEOC
Sentinel struct for the end of a construct's variants (EOC)
Definition: bnf_dsl.h:58

Macro to define a pair of rules for a right-recursive list.

This is the standard pattern for lists and operator precedence. It automatically defines the rule for 'head' and 'head_REST'.

For example, rrr(EXPR_LS, NT(EXPR), TK(COMMA)) defines:

  1. <expr_ls> ::= <expr> <expr_ls_rest>
  2. <expr_ls_rest> ::= "," <expr_ls> | E
Parameters
headThe base non-terminal.
listelemThe non-terminal for the list element.
delimThe token used as a separator.

◆ SEOB

#define SEOB   { .ty = CFG_SENTINEL, .id = EOB }

Sentinel struct for the end of a rule's body (EOB)

Usage with DSL is not recommended, use EPSILON instead.

◆ SEOC

#define SEOC   { { .ty = CFG_SENTINEL, .id = EOC } }

Sentinel struct for the end of a construct's variants (EOC)

Not recommended, use r and ropt instead.