Package: rebnf
ReBNFReBNF(Regexes for Extended Backus-Naur Form) is a notation used to define the
syntax of a language using regular expressions.It is an extension of the EBNF (Extended Backus-Naur Form) notation, allowing
for more flexibility and ease of use.ooooooooo.             oooooooooo.  ooooo      ooo oooooooooooo 
`888   `Y88.           `888'   `Y8b `888b.     `8' `888'     `8 
 888   .d88'  .ooooo.   888     888  8 `88b.    8   888         
 888ooo88P'  d88' `88b  888oooo888'  8   `88b.  8   888oooo8    
 888`88b.    888ooo888  888    `88b  8     `88b.8   888    "    
 888  `88b.  888    .o  888    .88P  8       `888   888         
o888o  o888o `Y8bod8P' o888bood8P'  o8o        `8  o888oTable of ContentsSyntaxExampleUsageContributingLicenseSyntaxTheReBNFnotation uses regular expressions to define the structure of a
language. Eachruleconsists of aleft-hand side(non-terminal) and aright-hand sideseparated by anassignment operator(either::=,:=or=).The general syntax of aReBNFrule is as follows:<alnum> ::= r"[a-zA-Z0-9]" ; # any alphanumeric characterThe alphanumeric set is composed of all letters and all digits, which sums up
36 characters.TheEBNFsyntax requiresquotesand|operators in between characters to
define thealnumidentifier as matching any alphanumeric character, which sums up to 143 characters.UsingReBNF, a single regex is required such asr"[a-zA-Z0-9]", which sums up to 14 characters.IdentifiersThe enclosures<and>are optional, such as:alnum = r"[a-zA-Z0-9]"       # shorter definitionTo improve readability and consistency, spaces are removed fromidentifiers,
and thesnake_casenaming convention is used instead.Snake case identifiers consist oflowercase letters,digits,
andunderscores.The naming convention also dictates that each word within an identifier is
separated by an underscore.This convention makes a clear distinction between individual words and
ensures that identifiers are easily recognizable.For example, an identifiernon-terminal symbolwould have to be written asnon_terminal_symbol.By adhering to the snake case convention, ReBNF identifiers maintain a
standardized and consistent style throughout the notation, enabling easier
comprehension and usage.ModularityInReBNF,importstatements are used to bring ingrammar rulesdefined
in separate specification files. This enables the reuse of existing rules and
promotes modular design in grammar specifications.As a result, we can organize grammar rules into separate.rebnfspecification files, making it easier to manage and maintain complex
grammars. This allows for better code organization, reuse of common rules,
and separation of concerns.To import rules from another specification file, we can use theimportstatement followed by the dotted path to a specification file or thefromstatement to import only specific items. This enables us to selectively use
and reference rules defined in other files.Given a folder hierarchy such as:grammar/
├── common.rebnf
└── spec.rebnfHere's an example:from common import *Using modularity inReBNFfiles can lead to more maintainable and scalable
grammar specifications.Optional groupsSquare brackets[ ]are used to define optional groups rather than
repetition. InEBNF,3 * [aa]would indicate the generation of multiple
occurrences ofaa(e.g., A, AA, AAA), whereas inReBNF, it denotes an
optional group that can occurzero or onetimes.InEBNF:aa = "A";
bb = 3 * aa, "B";
cc = 3 * [aa], "C";Which means:aa: Abb: AAABcc: C, AC, AAC, AAACInReBNF:aa = "A";
bb = 3 * aa "B";
cc = 3 * [aa] "C";Which means:aa: Abb: AAABcc: AAACConcatenationReBNFalso introduces a change in concatenation.InEBNF, explicit concatenation is required using a comma,between two
identifiers.However, inReBNF, since snake cased identifiers are enforced,
concatenation is implicit. Adjacent terminals or identifiers are
concatenated.That's why we are able to drop the comma in3 * aa, "C"if we wantccto be"AAAC".ExampleHere's a short example of aReBNFdefinition for a simple arithmetic
expression language:expression = term { ('+' | '-') term }
term = factor { ('*' | '/') factor }
factor = number | expression
number = r'\d+'UsageReBNFnotation is used to define the syntax of programming languages,
configuration file formats, or any other formal language.It provides a concise and powerful way to express language structures with a
addition of regular expressions.Note that the functions in this module are only designed to parse
syntactically validReBNFcode (code that does not raise when parsed
usingparse()). The behavior of the functions in this module is undefined
when providing invalidReBNFcode and it can change at any point.ContributingContributions are welcome! If you have suggestions, improvements, or new ideas
related to theReBNFnotation, please feel free to open an issue or
submit a pull request.LicenseThis project is licensed under theGPLv3license - seeLICENSE.mdfor details.