Пример #1
0
 def create_root_symbols(self):
     """Insert magical symbols above the root of the grammar in
     order to match the beginning and end of the sample."""
     RootSymbol = Symbol(Token(None,None,'R00t.Symbol'))
     RootSymbol.GlobalSymbolDict=self.GlobalSymbolDict
     StartDocSymbol = Symbol(Token(None,None,'%^'))
     StartDocSymbol.regex = Set('%^')
     StartDocSymbol.is_lit = True
     StartDocSymbol.GlobalSymbolDict=self.GlobalSymbolDict
     EndDocSymbol = Symbol(Token(None,None,'%$'))
     EndDocSymbol.regex = Set('%$')
     EndDocSymbol.is_lit = True
     EndDocSymbol.GlobalSymbolDict=self.GlobalSymbolDict
     RootSymbol.productions = [Production(RootSymbol,[StartDocSymbol]+self.get_roots()+[EndDocSymbol])]
     self.GlobalSymbolDict['R00t.Symbol'] = RootSymbol #XXX this is a nasty hack
     self.GlobalSymbolDict['%^']=StartDocSymbol
     self.GlobalSymbolDict['%$']=EndDocSymbol
Пример #2
0
 def promote_productions(self):
     """Convert all the elements of products from tokens into
     symbols, meanwhile checking that all of the elements are
     existing symbols. This is name analysis in action: because
     symbol names have Algol scoping inside the concrete grammar
     portion of the input file, we wait until the whose shebang is
     parsed before attempting to promote tokens into symbols."""
     for sym in self.GlobalSymbolDict.values():
         for production in sym.productions:
             elements = production.elements
             if len(elements) > 0: # An empty production has no tokens to promote
                 firstToken = elements[0]
                 for i in range(0, len(elements)):
                     if re.compile("^'").match(elements[i].text): # If the element is a literal, no name analysis needs to be done
                         elements[i] = Symbol(elements[i])
                         elements[i].is_lit = True
                         elements[i].regex = Set(re.escape(elements[i].defining_token.text[1:-1]))
                         self.GlobalSymbolDict[elements[i].defining_token.text]=elements[i]
                     else: # Do name analysis: check if the symbol is used without being defined.
                         try:
                             elements[i] = self.GlobalSymbolDict[elements[i].text]
                         except KeyError, e:
                             raise Exception("Production for %s beginning at %d,%d: %s is not a symbol." % \
                                             (sym.defining_token.text, firstToken.line, firstToken.col, elements[i].text))