Package: peg.py
peg.pyPeg.py is a Parsing Expression Grammar (PEG) parser written in pure Python.InstallYou can install peg.py by running:$pipinstallpeg.pyExample UsageYou can build aGrammarand call itsparsemethod to get the parse tree.frompegimportGrammargreeter=Grammar("""greet   <- "Hello, " someonesomeone <- [a-zA-Z0-9]""")input="Hello, world"tree=greeter.parse(input)Peg.py provides a tree visitorNodeVisitor. You an inherit this class and define your ownvisit_methods to handle your parse tree. Eachvisit_method handles a rule, such asvisit_greet,visit_someone.classGreeterVisitor(NodeVisitor):defvisit_greet(self,node,children):return('Hello',children[0])defvisit_someone(self,node,_):returnnode.textvisitor=GreeterVisitor(greeter)result=visitor.visit(tree)print(result)The program should produce such a result:('Hello', 'world')SyntaxAgrammarconsists of a set of rules.Aruleconsists of a name, a left arrow, and a pattern.name <- patternAnamestarts with alphabets or underscore, followed by alphabets or digits or underscore.valid
Valid
_valid
0nay 	// invalidApatterncontains one or more of the elements mentioned below.Aliteralis a string enclosed in double quotes or single quotes. For example,"hello",'hello'. Peg.py matches the input as-is.Asetis a set of characters enclosed in square brackets. Any pairs of characters having dash (-) in-between represents all characters from the first to the second (inclusive). For example,[a-z],[a-zA-Z0-9],[a-zA-Z$_].Adot(.) is for any character, except end-of-text.( pattern )groups pattern with parentheses.< pattern >captures input text associating with an unnamed rule.pattern?meanspatternis optional.pattern+meanspatternoccuring one or more times.pattern*meanspatternoccuring zero or more times.&patternchecks if pattern matches the input. If so, consume no input. Otherwise, the parse is failed.!patternchecks if pattern does not match the input. If so, consume no input. Otherwise, the parse is failed.Several patterns can be written one after another, likepattern1 pattern2 pattern3. The sequence matches only when each underlying pattern matches.Several patterns joined by slash (/), likepattern1 / pattern2 / pattern3, is ordered choices of patterns. The choices matches when any one of the underlying pattern matches.# ignored.is a comment.PEG Grammar For PEG GrammarsGrammar         <- Spacing Definition+ EndOfFile

Definition      <- Identifier LEFTARROW Expression
Expression      <- Sequence ( SLASH Sequence )*
Sequence        <- Prefix*
Prefix          <- ( AND / NOT )? Suffix
Suffix          <- Primary ( QUERY / STAR / PLUS )?
Primary         <- Identifier !LEFTARROW
                 / OPEN Expression CLOSE
                 / Literal
                 / Class
                 / DOT
                 / Action
                 / BEGIN
                 / END

Identifier      <- < IdentStart IdentCont* > Spacing
IdentStart      <- [a-zA-Z_]
IdentCont       <- IdentStart / [0-9]
Literal         <- ['] < ( !['] Char  )* > ['] Spacing
                 / ["] < ( !["] Char  )* > ["] Spacing
Class           <- '[' < ( !']' Range )* > ']' Spacing
Range           <- Char '-' Char / Char
Char            <- '\\' [abefnrtv'"\[\]\\]
                 / '\\' [0-3][0-7][0-7]
                 / '\\' [0-7][0-7]?
                 / '\\' '-'
                 / !'\\' .
LEFTARROW       <- '<-' Spacing
SLASH           <- '/' Spacing
AND             <- '&' Spacing
NOT             <- '!' Spacing
QUERY           <- '?' Spacing
STAR            <- '*' Spacing
PLUS            <- '+' Spacing
OPEN            <- '(' Spacing
CLOSE           <- ')' Spacing
DOT             <- '.' Spacing
Spacing         <- ( Space / Comment )*
Comment         <- '#' ( !EndOfLine . )* EndOfLine
Space           <- ' ' / '\t' / EndOfLine
EndOfLine       <- '\r\n' / '\n' / '\r'
EndOfFile       <- !.
BEGIN           <- '<' Spacing
END             <- '>' SpacingReferencesPeg.py provides identical PEG syntax as described inBryan Ford's PEG Paper.Peg.py implements a simplied VM similar tolpeg.Peg.py provides a similar API (Grammar,Grammar.parse(),NodeVisitor, etc) withparsimonious.