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

NULLABLE()[source]

Determines which nonterminals X ->* []

makenonterminals()[source]

Extracts C{nonterminals} from grammar rules.

maketerminals()[source]

Extracts C{terminals} from the rules. Nonterminals must already exist

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

chomsky()[source]

Transform to CNF

elim_unitary()[source]

Elimination of unitary rules

cfgGenerator

class cfgGenerator(cfgr, size)[source]

CFG uniform genetaror

Object initialization :param cfgr: grammar for the random objects :type cfgr: CNF :param size: size of objects :type size: integer

generate()[source]

Generates a new random object generated from the start symbol

Returns

object

Return type

string

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
  • Sigma (list|set) – re alphabet (that will be the set of grammar terminals)

  • size (int) – word size

  • cfgr – base grammar

  • epsilon – if not None is added to a grammar terminals

  • empty – if not None is added to a grammar terminals

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

smallAlphabet(k, sigma_base='a')[source]

Easy way to have small alphabets

Parameters
  • k – alphabet size (must be less than 52)

  • sigma_base – initial symbol

Returns

alphabet

Return type

list

Constants

const

reGrammar