|
@ -0,0 +1,209 @@ |
|
|
|
|
|
#!/usr/bin/env python |
|
|
|
|
|
|
|
|
|
|
|
from enum import Enum |
|
|
|
|
|
from stateMachine import StateMachine |
|
|
|
|
|
from collections import deque |
|
|
|
|
|
from token import Token |
|
|
|
|
|
|
|
|
|
|
|
T_TOKEN = Enum("T_TOKEN", "NON_TERMINAL " + |
|
|
|
|
|
"ARROW " + |
|
|
|
|
|
"TERMINAL " + |
|
|
|
|
|
"ERROR") |
|
|
|
|
|
|
|
|
|
|
|
class GrammarLexer: |
|
|
|
|
|
__STATE = Enum("STATE", "START " + |
|
|
|
|
|
"NON_TERMINAL " + |
|
|
|
|
|
"ARROW " + |
|
|
|
|
|
"TERMINAL " + |
|
|
|
|
|
"ERROR") |
|
|
|
|
|
|
|
|
|
|
|
__T_TOKEN_STATE_MAP = {} |
|
|
|
|
|
|
|
|
|
|
|
__stateMachine = StateMachine() |
|
|
|
|
|
__line = deque() |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self): |
|
|
|
|
|
self.__T_TOKEN_STATE_MAP[self.__STATE.NON_TERMINAL] = T_TOKEN.NON_TERMINAL |
|
|
|
|
|
self.__T_TOKEN_STATE_MAP[self.__STATE.TERMINAL] = T_TOKEN.TERMINAL |
|
|
|
|
|
self.__T_TOKEN_STATE_MAP[self.__STATE.ARROW] = T_TOKEN.ARROW |
|
|
|
|
|
self.__T_TOKEN_STATE_MAP[self.__STATE.ERROR] = T_TOKEN.ERROR |
|
|
|
|
|
|
|
|
|
|
|
self.__stateMachine.setStartState(self.__STATE.START) |
|
|
|
|
|
self.__stateMachine.setErrorState(self.__STATE.ERROR) |
|
|
|
|
|
|
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.START, "$", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.START, "/", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.START, "-", self.__STATE.ARROW) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.START, " ", self.__STATE.START) |
|
|
|
|
|
|
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "a", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "b", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "c", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "d", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "e", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "f", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "g", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "h", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "i", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "j", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "k", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "l", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "m", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "n", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "o", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "p", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "q", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "r", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "s", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "t", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "u", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "v", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "w", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "x", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "y", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "z", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "A", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "B", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "C", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "D", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "E", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "F", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "G", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "H", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "I", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "J", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "K", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "L", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "M", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "N", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "O", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "P", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "Q", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "R", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "S", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "T", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "U", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "V", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "W", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "X", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "Y", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "Z", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "0", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "1", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "2", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "3", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "4", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "5", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "6", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "7", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "8", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "9", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, "/", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.NON_TERMINAL, " ", self.__STATE.START) |
|
|
|
|
|
|
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "a", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "b", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "c", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "d", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "e", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "f", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "g", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "h", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "i", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "j", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "k", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "l", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "m", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "n", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "o", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "p", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "q", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "r", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "s", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "t", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "u", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "v", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "w", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "x", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "y", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "z", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "A", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "B", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "C", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "D", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "E", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "F", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "G", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "H", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "I", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "J", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "K", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "L", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "M", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "N", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "O", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "P", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "Q", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "R", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "S", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "T", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "U", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "V", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "W", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "X", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "Y", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "Z", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "0", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "1", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "2", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "3", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "4", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "5", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "6", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "7", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "8", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "9", self.__STATE.TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, "$", self.__STATE.NON_TERMINAL) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.TERMINAL, " ", self.__STATE.START) |
|
|
|
|
|
|
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.ARROW, ">", self.__STATE.ARROW) |
|
|
|
|
|
self.__stateMachine.addTransition(self.__STATE.ARROW, " ", self.__STATE.START) |
|
|
|
|
|
|
|
|
|
|
|
def processLine(self, line): |
|
|
|
|
|
self.__stateMachine.reset() |
|
|
|
|
|
prevState = self.__stateMachine.getCurrentState() |
|
|
|
|
|
currState = prevState |
|
|
|
|
|
lexemeBuffer = ""; |
|
|
|
|
|
|
|
|
|
|
|
self.__line.clear() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for char in line: |
|
|
|
|
|
self.__stateMachine.processChar(char) |
|
|
|
|
|
|
|
|
|
|
|
currState = self.__stateMachine.getCurrentState() |
|
|
|
|
|
|
|
|
|
|
|
if currState != prevState and prevState != self.__STATE.START: |
|
|
|
|
|
tToken = self.__T_TOKEN_STATE_MAP.get(prevState) |
|
|
|
|
|
|
|
|
|
|
|
if tToken == T_TOKEN.TERMINAL or tToken == T_TOKEN.NON_TERMINAL: |
|
|
|
|
|
lexemeBuffer = lexemeBuffer[1:] |
|
|
|
|
|
|
|
|
|
|
|
self.__line.append(Token(tToken, lexemeBuffer)) |
|
|
|
|
|
lexemeBuffer = "" |
|
|
|
|
|
|
|
|
|
|
|
if char != " ": |
|
|
|
|
|
lexemeBuffer += char; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
prevState = self.__stateMachine.getCurrentState() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def nextToken(self): |
|
|
|
|
|
return self.__line.popleft(); |
|
|
|
|
|
|
|
|
|
|
|
def hasTokensLeft(self): |
|
|
|
|
|
return True if len(self.__line) != 0 else False |
|
|
|
|
|
|
|
|
|
|
|
def getParsedLine(self): |
|
|
|
|
|
return self.__line |