|
@ -2,6 +2,9 @@ |
|
|
|
|
|
|
|
|
from grammarLexer import GrammarLexer |
|
|
from grammarLexer import GrammarLexer |
|
|
from grammarLexer import T_TOKEN |
|
|
from grammarLexer import T_TOKEN |
|
|
|
|
|
from grammar import Grammar |
|
|
|
|
|
from terminal import Terminal |
|
|
|
|
|
from nonTerminal import NonTerminal |
|
|
|
|
|
|
|
|
class GrammarParser: |
|
|
class GrammarParser: |
|
|
__lxr = GrammarLexer() |
|
|
__lxr = GrammarLexer() |
|
@ -10,13 +13,14 @@ class GrammarParser: |
|
|
|
|
|
|
|
|
def parse(self, grammarFile): |
|
|
def parse(self, grammarFile): |
|
|
lines = grammarFile.readlines() |
|
|
lines = grammarFile.readlines() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for line in lines: |
|
|
for line in lines: |
|
|
|
|
|
|
|
|
self.__lxr.processLine(line) |
|
|
self.__lxr.processLine(line) |
|
|
|
|
|
|
|
|
tokenLine = self.__lxr.getParsedLine() |
|
|
tokenLine = self.__lxr.getParsedLine() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if self.__lineIsValid(tokenLine): |
|
|
if self.__lineIsValid(tokenLine): |
|
|
leftSide = tokenLine.popleft().getLexeme() |
|
|
leftSide = tokenLine.popleft().getLexeme() |
|
|
tokenLine.popleft() |
|
|
tokenLine.popleft() |
|
@ -30,8 +34,38 @@ class GrammarParser: |
|
|
|
|
|
|
|
|
self.__grammar[leftSide].append(rightSide) |
|
|
self.__grammar[leftSide].append(rightSide) |
|
|
|
|
|
|
|
|
print(self.__grammar) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parseGrm(self, grammarFile): |
|
|
|
|
|
grm = Grammar() |
|
|
|
|
|
lines = grammarFile.readlines() |
|
|
|
|
|
|
|
|
|
|
|
for line in lines: |
|
|
|
|
|
self.__lxr.processLine(line) |
|
|
|
|
|
tokenLine = self.__lxr.getParsedLine() |
|
|
|
|
|
|
|
|
|
|
|
if self.__lineIsValid(tokenLine): |
|
|
|
|
|
leftSide = NonTerminal(tokenLine.popleft().getLexeme()) |
|
|
|
|
|
tokenLine.popleft() |
|
|
|
|
|
|
|
|
|
|
|
rightSide = [] |
|
|
|
|
|
tmpToken = None |
|
|
|
|
|
|
|
|
|
|
|
while len(tokenLine) > 0: |
|
|
|
|
|
tmpToken = tokenLine.popleft() |
|
|
|
|
|
|
|
|
|
|
|
if tmpToken.getClass() == T_TOKEN.NON_TERMINAL: |
|
|
|
|
|
rightSide.append(NonTerminal(tmpToken.getLexeme())) |
|
|
|
|
|
elif tmpToken.getClass() == T_TOKEN.TERMINAL: |
|
|
|
|
|
lexeme = tmpToken.getLexeme(); |
|
|
|
|
|
|
|
|
|
|
|
if lexeme == "~": |
|
|
|
|
|
lexeme = None; |
|
|
|
|
|
|
|
|
|
|
|
rightSide.append(Terminal(lexeme)) |
|
|
|
|
|
|
|
|
|
|
|
grm.addProduction(leftSide, rightSide) |
|
|
|
|
|
|
|
|
|
|
|
return grm |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __lineIsValid(self, tokenLine): |
|
|
def __lineIsValid(self, tokenLine): |
|
|
if len(tokenLine) < 3: |
|
|
if len(tokenLine) < 3: |
|
@ -42,7 +76,7 @@ class GrammarParser: |
|
|
|
|
|
|
|
|
if tokenLine[1].getClass() != T_TOKEN.ARROW: |
|
|
if tokenLine[1].getClass() != T_TOKEN.ARROW: |
|
|
return False; |
|
|
return False; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return True; |
|
|
return True; |
|
|
|
|
|
|
|
|
def __isNewNonTerm(self, token): |
|
|
def __isNewNonTerm(self, token): |
|
@ -54,7 +88,8 @@ class GrammarParser: |
|
|
def printGrammar(self): |
|
|
def printGrammar(self): |
|
|
for left in self.__grammar: |
|
|
for left in self.__grammar: |
|
|
self.__printRow(left, self.__grammar[left]); |
|
|
self.__printRow(left, self.__grammar[left]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __printRow(self, left, right): |
|
|
def __printRow(self, left, right): |
|
|
line = left + " -> " |
|
|
line = left + " -> " |
|
|
|
|
|
|
|
@ -67,4 +102,3 @@ class GrammarParser: |
|
|
line += str(token.getClass()) + "(" + token.getLexeme() + ")" |
|
|
line += str(token.getClass()) + "(" + token.getLexeme() + ")" |
|
|
|
|
|
|
|
|
print(line) |
|
|
print(line) |
|
|
|
|
|
|