Module: Context Free Grammars Manipulation (cfg)¶
Context Free Grammars Manipulation.
Basic context-free grammars manipulation for building uniform random generetors
Classes¶
CFGrammar¶
- class CFGrammar(gram)[source]¶
Class for context-free grammars
- Variables
Rules – grammar rules
Terminals – terminals symbols
Nonterminals – nonterminals symbols
Start (str) – start symbol
ntr – dictionary of rules for each nonterminal
Initialization
- Parameters
gram – is a list for productions; each production is a tuple (LeftHandside, RightHandside) with LeftHandside nonterminal, RightHandside list of symbols, First production is for start symbol
CNF¶
- class CNF(gram, mark='A@')[source]¶
Chomsky Normal Form. No useless nonterminals or eepsipsilon rules are ALLOWED… Given a CFG grammar description generates one in CNF Then its possible to random generate words of a given size. Before some pre-calculations are needed.
Initialization
- Parameters
gram – is a list for productions; each production is a tuple (LeftHandside, RightHandside) with LeftHandside nonterminal, RightHandside list of symbols, First production is for start symbol
cfgGenerator¶
reStringRGenerator¶
- class reStringRGenerator(Sigma=None, size=10, cfgr=None, epsilon=None, empty=None, ident='Ti')[source]¶
Uniform random Generator for reStrings
- Uniform random generator for regular expressions. Used without arguments generates an uncollapsible re
over {a,b} with size 10. For generate an arbitary re over an alphabet of 10 symbols of size 100: reStringRGenerator (smallAlphabet(10),100,reGrammar[“g_regular_base”])
- Parameters
Note
the grammar can have already this symbols
Functions¶
gRules¶
- gRules(rules_list, rulesym='->', rhssep=None, rulesep='|')[source]¶
Transforms a list of rules into a grammar description.
- Parameters
rules_list – is a list of rule where rule is a string of the form: Word rulesym Word1 … Word2 or Word rulesym []
rulesym – LHS and RHS rule separator
rhssep – RHS values separator (None for white chars)
- Returns
a grammar description
smallAlphabet¶
Constants¶
- const
reGrammar