Example #1
0
 def parse_tree_walker(tree):
     if tree.symbol == NonTerminalSymbol("S"):
         return parse_tree_walker(tree.childlist[0])
     if tree.symbol == NonTerminalSymbol("E"):
         return to_number(tree.childlist[0].symbol.gd) + to_number(
             tree.childlist[2].symbol.gd)
     raise Exception
Example #2
0
 def testFirstLookup(self):
     from pydsl.Grammar.Symbol import NonTerminalSymbol, TerminalSymbol
     from pydsl.Grammar.PEG import Choice
     self.grammardef.first_lookup(NonTerminalSymbol("exp"))[0]
     self.assertEqual(
         self.grammardef.first_lookup(NonTerminalSymbol("exp")),
         Choice([String("S")]))
Example #3
0
 def parse_tree_walker(tree):
     from pydsl.Grammar.Symbol import NonTerminalSymbol
     if tree.symbol == NonTerminalSymbol("S"):
         return parse_tree_walker(tree.childlist[0])
     if tree.symbol == NonTerminalSymbol("E"):
         return int(str(tree.childlist[0].content)) + int(
             str(tree.childlist[2].content))
     else:
         raise Exception
Example #4
0
def tree_translator(tree):
    from pydsl.Grammar.Symbol import NonTerminalSymbol
    if tree.symbol == NonTerminalSymbol("E"):
        return int(str(tree.childlist[0].content)) + int(
            str(tree.childlist[2].content))
    elif len(tree.childlist) == 1:
        return tree_translator(tree.childlist[0])
    else:
        raise Exception
Example #5
0
def read_nonterminal_production(line, symboldict):
    sidesarray = line.split("::=")
    if len(sidesarray) < 2 or len(sidesarray) > 3:
        raise ValueError("Error reading nonterminal production rule")
    leftside = sidesarray[0].strip()
    #leftside contains at least one NonTerminalSymbol
    #FIXME supports only one symbol
    symboldict[leftside] = NonTerminalSymbol(leftside)
    rightside = sidesarray[1]
    alternatives = [alt.rstrip() for alt in rightside.split("|")]
    result = []
    n = 0
    for alternative in alternatives:
        symbollist = alternative.split()
        symbolinstancelist = []
        for symbol in symbollist:
            symbolinstancelist.append(symboldict[symbol])
        result.append(Production([symboldict[leftside]], symbolinstancelist))
        n += 1
    return result
Example #6
0
from pydsl.Grammar.Symbol import TerminalSymbol, NonTerminalSymbol, NullSymbol
from pydsl.Grammar.BNF import Production, BNFGrammar
from pydsl.File.BNF import strlist_to_production_set
from pydsl.File.Python import load_python_file
from pydsl.Grammar.Definition import String, RegularExpression

br = "max"
leftrecursive=["S ::= E","E ::= E dot | dot","dot := String,."]
rightrecursive=["S ::= E","E ::= dot E | dot","dot := String,."]
centerrecursive=["S ::= E","E ::= dot E dot | dot","dot := String,."]

#productionset0 definition

symbol1 = TerminalSymbol(String("S"))
symbol2 = TerminalSymbol(String("R"))
final1 = NonTerminalSymbol("exp")
rule1 = Production([final1], (symbol1, symbol2))
productionset0 = BNFGrammar(final1, (rule1,symbol1,symbol2))
p0good = "SR"
p0bad = "SRL"


#productionset1 definition
symbol1 = TerminalSymbol(String("S"))
symbol2 = TerminalSymbol(String("R"))
symbol3 = TerminalSymbol(String(":"))
symbol4 = TerminalSymbol(RegularExpression("^[0123456789]*$"), None, br)
symbol5 = TerminalSymbol(load_python_file('pydsl/contrib/grammar/cstring.py'), None, br)
final1 = NonTerminalSymbol("storeexp") 
final2 = NonTerminalSymbol("retrieveexp") 
final3 = NonTerminalSymbol("exp")
Example #7
0
#You should have received a copy of the GNU General Public License
#along with pydsl.  If not, see <http://www.gnu.org/licenses/>.

"""SLR0 unfinished implementation"""

__author__ = "Nestor Arocha"
__copyright__ = "Copyright 2008-2013, Nestor Arocha"
__email__ = "*****@*****.**"

import logging
LOG = logging.getLogger(__name__)
from pydsl.Parser.Parser import BottomUpParser
from pydsl.Grammar.Symbol import NonTerminalSymbol, TerminalSymbol, EndSymbol
from pydsl.Grammar.BNF import Production

Extended_S = NonTerminalSymbol("EI")

def _build_item_closure(itemset, productionset):
    """Build input itemset closure """
    #For every item inside current itemset, if we have the following rule:
    #  xxx <cursor><nonterminalSymbol> xxx  append every rule from self._productionruleset that begins with that NonTerminalSymbol
    LOG.debug("_build_item_closure: Begin")
    if not isinstance(itemset, LR0ItemSet):
        raise TypeError
    import copy
    resultset = copy.copy(itemset)
    changed = True
    while changed:
        changed = False
        for currentitem in resultset.itemlist:
            nextsymbol = currentitem.next_symbol()
Example #8
0
 def testNextLookup(self):
     from pydsl.Grammar.Symbol import NonTerminalSymbol, EndSymbol
     self.grammardef.next_lookup(NonTerminalSymbol("exp"))[0]
     self.assertListEqual(
         self.grammardef.next_lookup(NonTerminalSymbol("exp")),
         [EndSymbol()])