Advertisement
Guest User

Untitled

a guest
May 28th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.98 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import sys
  4.  
  5. symbols = []
  6. rules = []
  7. data = []
  8. isOriginal = 1
  9.  
  10.  
  11. def s_validation():
  12.     #9 represents new start, if S is used on the right side
  13.     global rules, symbols, isOriginal
  14.  
  15.     for i in rules:
  16.         if(i[3:].find("S") != -1):
  17.             print("WARNING: Starting terminal used on the right side, fixing ...")
  18.             rules.insert(0,"X->S")
  19.             symbols.insert(0,"X")
  20.             isOriginal = 0
  21.             break;
  22.  
  23.  
  24. def read_input():
  25.     terminalCount = 0;
  26.     nonTerminalCount = 0;
  27.     ruleCount = 0;
  28.     global symbols
  29.     global rules
  30.  
  31.     if(len(sys.argv) < 2):
  32.         print("ERROR: Too few arguments. Please, specify path to gramatika.txt")
  33.         sys.exit()
  34.  
  35.     if sys.argv[1:]:
  36.         grammarpath = sys.argv[1]
  37.  
  38.  
  39.     try:
  40.         inFile = open(grammarpath, 'r')
  41.     except IOError:
  42.         print("ERROR : Input file is missing or corrupted")
  43.         sys.exit()
  44.     else:
  45.         terminalCount = int(inFile.readline())
  46.         nonTerminalCount = int(inFile.readline())
  47.         ruleCount = int(inFile.readline())
  48.         for i in range(terminalCount + nonTerminalCount):
  49.             symbols.append(inFile.readline().rstrip('\n\r'))
  50.         for i in range(ruleCount):
  51.             rules.append(inFile.readline().rstrip('\n\r0'))
  52.  
  53.  
  54. def unfold(n): #unclod n-th state
  55.     global data
  56.     modified = 0
  57.     stateUnfold = ""
  58.  
  59.     while True:
  60.         modified = 0
  61.         for i in data[n]: #for each rule recognized so far
  62.             if(len(i) <4 ): #if the rule is already finished
  63.                 continue;
  64.             if(str.istitle(i[3]) and stateUnfold.find(i[3])==-1 ): #rule starts with nonTerminal
  65.                 stateUnfold += i[3] #set array of nonTerminals unfolded already
  66.                 modified = 1; #flag deciding whether the state is unchanged, so we can stop
  67.                 for originRule in rules: #for origin rules
  68.                     if(originRule[0] == i[3]): #add rule starting with recognized nonTerminal
  69.                         data[n].append(originRule)
  70.  
  71.  
  72.         if (modified == 0):
  73.             return
  74.  
  75. def lr_0():
  76.     global rules,data
  77.     tempRule = []
  78.  
  79.     firstTerminal = "S" if isOriginal else "X"
  80.     #------------------------------------initialize first state-----------------------------------------------
  81.     for rule in rules:
  82.         if(rule[0] == firstTerminal): #set first state with firstTerminal rule
  83.             tempRule.append(rule)
  84.  
  85.     data.append(tempRule)
  86.  
  87.     #check whether the rule starts with nonTerminal, if so, unfold
  88.     unfold(0)
  89.  
  90.  
  91.     #---------------------------------------other states----------------------------------------------
  92.     stateCnt = 0
  93.     isTransfer = 0
  94.     isReduction = 0
  95.     testCnt = 0
  96.     tempState = []
  97.  
  98.     while True:
  99.         for checkRulesIfNoReduction in data[stateCnt]:
  100.             if(len(checkRulesIfNoReduction) == 3):
  101.                 isReduction += 1     #TODO
  102.  
  103.         for symbol in symbols: #for every symbol
  104.             for rule in data[stateCnt]: #find whether we can move forward on that symbol
  105.                 if(len(rule) < 4): #if the rule is already finished
  106.                     continue;
  107.                 if(symbol == rule[3]): #we found symbol what we are going to unfold
  108.                     isTransfer = 1        #TODO
  109.                     tempState = [] #create new state
  110.                     for subRule in data[stateCnt]: #iterate through current rules in our state
  111.                         if (len(subRule) < 4):
  112.                             continue;
  113.                         if(subRule[3] == symbol): #if rule starting with letter is found, cut it
  114.                             tempState.append(subRule[:3] + subRule[4:]) #cut the rule
  115.  
  116.                     data.append(tempState);  #add the state to global state
  117.                     unfold(len(data) -1) #unfold the state
  118.  
  119.                     testCnt = len(data) #len of data
  120.                     for t in range (testCnt-1): #delete last state if it is duplicate
  121.                         if(data[t] == data[testCnt-1]):
  122.                             data.pop(testCnt-1)
  123.                             break;
  124.  
  125.  
  126.         if(isTransfer == 1 and isReduction > 0):
  127.             print "ERROR: KOLIZIA TRANSFER REDUKCIA"
  128.             print "Daná gramatika nie je LR0 gramatikou"
  129.             return
  130.         elif(isReduction > 1):
  131.             print "ERROR: KOLIZIA REDUKCIA REDUKCIA"
  132.             print "Daná gramatika nie je LR0 gramatikou"
  133.             return
  134.         isTransfer = isReduction = 0;
  135.         stateCnt+=1
  136.         if(stateCnt >= len(data)):
  137.             break
  138.  
  139.     print "Daná gramatika je LR0 gramatikou"
  140.  
  141.  
  142. def main():
  143.     read_input()
  144.     s_validation()
  145.     lr_0()
  146.  
  147.  
  148. if __name__ == "__main__":
  149.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement