Skip to content

Instantly share code, notes, and snippets.

@Jacoby6000
Last active May 7, 2018 01:58
Show Gist options
  • Save Jacoby6000/a18a705f1475980a769ff537ba702bea to your computer and use it in GitHub Desktop.
Save Jacoby6000/a18a705f1475980a769ff537ba702bea to your computer and use it in GitHub Desktop.

BNF Grammar of Regular Expressions

Following the precedence rules given previously, a BNF grammar for Perl-style regular expressions can be constructed as follows.

  • <RE> ::= <union> | <simple-RE>
  • <union> ::= <RE> "|" <simple-RE>
  • <simple-RE> ::= <concatenation> | <basic-RE>
  • <concatenation> ::= <simple-RE> <basic-RE>
  • <basic-RE> ::= <star> | <plus> | <elementary-RE>
  • <star> ::= <elementary-RE> "*"
  • <plus> ::= <elementary-RE> "+"
  • <elementary-RE> ::= <group> | <any> | <eos> | <char> | <set>
  • <group> ::= "(" <RE> ")"
  • <any> ::= "."
  • <eos> ::= "$"
  • <char> ::= any non metacharacter | "\" metacharacter
  • <set> ::= <positive-set> | <negative-set>
  • <positive-set> ::= "[" <set-items> "]"
  • <negative-set> ::= "[^" <set-items> "]"
  • <set-items> ::= <set-item> | <set-item> <set-items>
  • <set-items> ::= <range> | <char>
  • <range> ::= <char> "-" <char>
data RegexTop
data Regex
= Term RegexTerm
| StartAnchor Regex
| EndAnchor Regex
| Repeat Int (Maybe Int) Bool Regex
| Or Regex Regex
| FollowedBy Regex Regex
deriving(Show, Eq)
data RegexTerm
= OneOf [Choose]
| NotOneOf [Choose]
| Literal RegexLiteral
data RegexLiteral
= RegexChar Char
| RegexSpecialChar SpecialChar
| Wildcard
data Choose
= ChooseLiteral RegexLiteral
| ChooseCharRange Char Char
deriving(Show, Eq)
data SpecialChar
= Whitespace
| Number
| AlphaNumeric
| OctalEscape Char
| HexadecimalEscape Char
deriving(Show, Eq)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment