coding utf-8 import sys reload sys sys setdefaultencoding utf-8 import

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# -*- coding: utf-8 -*- import sys reload(sys) sys.setdefaultencoding('utf-8')
import copy
from grammar import Symbol
from grammar import Rule
class Parser():
def __init__(self, lexer):
self.lexer = lexer
def parse(self):
'''
Ident = ([A-Z]) ([A-Z]p)
Oper = (+)(*)
ω = ("*" Ident θ) (Ident θ)
θ = ("(" Ident Ident ")" ω) ("(" Oper Ident Ident ")" ω) ("(" \"(\" Ident \")\" ")" ω) ("(" ")"ω)
'''
def w_parse():
rul = Rule()
def start_parse(ax):
if lexer.cur_token_is_ident():
rul = Rule()
head = Symbol(sym=lexer.cur_token()[1], type='noterm')
rul.head = head
rul.is_axiom = ax
lexer.next_token()
o_parse(rul, f=1)
else:
raise Exception("parse error on token %s" % lexer.cur_token()[1])
if lexer.cur_token_is_star():
lexer.next_token()
start_parse(True)
else:
start_parse(False)
def o_parse(rul, f = 0):
if lexer.cur_token_is_oper_left():
lexer.next_token()
if lexer.cur_token_is_ident():
st.append(lexer.cur_token())
lexer.next_token()
if lexer.cur_token_is_ident():
st.append(lexer.cur_token())
lexer.next_token()
if lexer.cur_token_is_oper_right():
lexer.next_token()
if rul.body != None:
rul = copy.copy(rul)
rul.body = st[-2:]
rules.append(rul)
o_parse(rul)
else:
raise Exception("parse error on token %s" % lexer.cur_token()[1])
else:
raise Exception("parse error on token %s" % lexer.cur_token()[1])
elif lexer.cur_token_is_text():
st.append(lexer.cur_token())
lexer.next_token()
if lexer.cur_token_is_ident():
st.append(lexer.cur_token())
lexer.next_token()
if lexer.cur_token_is_text():
st.append(lexer.cur_token())
lexer.next_token()
if lexer.cur_token_is_oper_right():
lexer.next_token()
if rul.body != None:
rul = copy.copy(rul)
rul.body = st[-3:]
rules.append(rul)
o_parse(rul)
elif lexer.cur_token_is_ident():
st.append(lexer.cur_token())
lexer.next_token()
if lexer.cur_token_is_oper_right():
lexer.next_token()
if rul.body != None:
rul = copy.copy(rul)
rul.body = st[-3:]
rules.append(rul)
o_parse(rul)
else:
raise Exception("parse error on token %s" % lexer.cur_token()[1])
elif lexer.cur_token_is_oper_right():
lexer.next_token()
if rul.body != None:
rul = copy.copy(rul)
rul.body = st[-3:]
rules.append(rul)
o_parse(rul)
elif lexer.cur_token_is_oper_right():
lexer.next_token()
if rul.body != None:
rul = copy.copy(rul)
rul.body=st[-1:]
rules.append(rul)
o_parse(rul)
else:
raise Exception("parse error on token %s" % lexer.cur_token()[1])
elif lexer.cur_token_is_oper_right():
lexer.next_token()
if rul.body != None:
rul = copy.copy(rul)
rul.body = []
rules.append(rul)
o_parse(rul)
elif f:
raise Exception("parse error on token %s" % lexer.cur_token()[1])
rules, st = [], []
lexer = self.lexer
while lexer.cur_token():
w_parse()
return rules