Compare commits

...

Author SHA1 Message Date
  Tom 4f790dd1dc sync 6 years ago
3 changed files with 52 additions and 9 deletions
Split View
  1. +12
    -7
      src/grammar.py
  2. +40
    -0
      src/nonTerminal.py
  3. +0
    -2
      src/testParser.py

+ 12
- 7
src/grammar.py View File

@ -2,15 +2,16 @@ from terminal import Terminal
from nonTerminal import NonTerminal
class Grammar:
__atomKeyMapping = []
__productions = []
__first = {}
def __init__(self):
self.__nonTerminals = {}
self.__terminals = {}
self.__startSymbol = None;
self.__endMarker = Terminal("$")
__nonTerminals = {}
__terminals = {}
def addProduction(self, nonTerminal, atoms):
if self.__startSymbol == None:
self.__startSymbol = nonTerminal;
self.__startSymbol.addToFollowSet(self.__endMarker);
self.__addAtom(nonTerminal);
@ -45,6 +46,7 @@ class Grammar:
def generateMetrics(self):
self.__generateFirstSets()
self.__generateFollowSets()
def __generateFirstSets(self):
@ -52,6 +54,9 @@ class Grammar:
nonTerminal = self.__nonTerminals[key]
nonTerminal.generateFirstSet();
def __generateFollowSets(self):
for key in self.__nonTerminals:
self.__nonTerminals[key].generateFollowSet()
def printNullableSet(self):
output = "Nullable = { "


+ 40
- 0
src/nonTerminal.py View File

@ -6,6 +6,7 @@ class NonTerminal:
self.__isNullable = False
self.__productions = []
self.__first = []
self.__follow = []
self.__firstSetIsGenerated = False;
self.__string = string
@ -49,6 +50,45 @@ class NonTerminal:
self.__firstSetIsGenrated = True;
def addToFollowSet(self, marker):
self.__follow.append(marker)
def generateFollowSet(self):
for production in self.__productions:
self.__extendFollowSetsBasedOnProduction(production)
def __extendFollowSetsBasedOnProduction(self, production):
for i in range(len(production)):
atom = production[i]
if isinstance(atom, NonTerminal):
if self.__isPossiblyLastInProduction(i, production):
atom.extendFollowWithFollowOf(self)
else:
atom.extendFollowWithFirstOf(production[i + 1])
def __isPossiblyLastInProduction(self, index, production):
for j in range(index, len(production)):
if isinstance(production[j], Terminal):
return False
if not production[j].isNullable():
return False
return True
def extendFollowWithFollowOf(self, atom):
for el in atom.getFollowSet():
self.addToFollowSet(el)
def extendFollowWithFirstOf(self, atom):
for el in atom.getFirstSet():
if not el.isEmptyString():
self.addToFollowSet(el)
def getFollowSet(self):
return self.__follow
def __addFirsSetForProduction(self, production):
stillNullable = True
lenProd = len(production)


+ 0
- 2
src/testParser.py View File

@ -18,5 +18,3 @@ grm.printNullableSet()
print("----------------")
grm.printFirstSets()
#prs.printGrammar();

Loading…
Cancel
Save