annotate python/pyyacc.py @ 179:0f3b1adfd416

LR(0) parser
author Windel Bouwman
date Sat, 04 May 2013 18:50:36 +0200
parents c694ec551f34
children 25a0753da4cf
rev   line source
178
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
1 class Grammar:
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
2 def __init__(self, terminals):
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
3 self.terminals = terminals
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
4 self.nonterminals = []
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
5 self.productions = []
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
6 def add_production(self, name, symbols):
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
7 p = Production(name, symbols)
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
8 self.productions.append(p)
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
9 if not name in self.nonterminals:
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
10 self.nonterminals.append(name)
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
11 def productionsForName(self, name):
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
12 return [p for p in self.productions if p.name == name]
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
13 @property
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
14 def Symbols(self):
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
15 return self.nonterminals + self.terminals
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
16
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
17 class Production:
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
18 def __init__(self, name, symbols):
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
19 self.name = name
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
20 self.symbols = symbols
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
21 def __repr__(self):
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
22 return '{0} -> {1}'.format(self.name, self.symbols)
179
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
23
178
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
24 class Item:
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
25 def __init__(self, production, dotpos):
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
26 self.production = production
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
27 self.dotpos = dotpos
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
28 def __eq__(self, other):
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
29 if type(other) is type(self):
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
30 return (self.production, self.dotpos) == (other.production, other.dotpos)
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
31 return False
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
32 def __hash__(self):
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
33 return (self.production, self.dotpos).__hash__()
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
34 @property
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
35 def IsReduce(self):
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
36 return self.dotpos == len(self.production.symbols)
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
37 @property
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
38 def IsShift(self):
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
39 return not self.IsReduce
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
40 @property
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
41 def Next(self):
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
42 return self.production.symbols[self.dotpos]
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
43 def matches(self, symbol):
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
44 return self.Next == symbol
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
45 def __repr__(self):
179
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
46 return '{0} -> {1} _dot_ {2} {{}}'.format(self.production.name, ' '.join(self.production.symbols[0:self.dotpos]), ' '.join(self.production.symbols[self.dotpos:]))
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
47
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
48 class LRgenerator:
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
49 def __init__(self, g):
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
50 self.grammar = g
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
51 def e_closure(self, S):
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
52 """ Expand itemset by using epsilon moves """
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
53 something_changed = True
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
54 while something_changed:
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
55 something_changed = False
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
56 worklist = list(S)
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
57 for item in worklist:
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
58 if item.IsShift and item.Next in self.grammar.nonterminals:
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
59 n = item.Next
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
60 for add_p in self.grammar.productionsForName(n):
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
61 i = Item(add_p, 0)
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
62 if not i in S:
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
63 S.add(i)
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
64 something_changed = True
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
65 return frozenset(S)
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
66 def initialItemSet(self):
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
67 nis = set()
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
68 for p in self.grammar.productionsForName(self.grammar.start_symbol):
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
69 nis.add(Item(p, 0))
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
70 return self.e_closure(nis)
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
71 def nextItemSet(self, itemset, symbol):
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
72 nis = set()
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
73 for item in itemset:
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
74 if item.IsShift and item.Next == symbol:
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
75 nis.add(Item(item.production, item.dotpos + 1))
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
76 return self.e_closure(nis)
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
77 def genParser(self):
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
78 states = set()
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
79 goto_table = {}
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
80 action_table = {}
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
81 iis = self.initialItemSet()
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
82 worklist = [iis]
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
83 states.add(iis)
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
84 while len(worklist) > 0:
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
85 itemset = worklist.pop(0)
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
86 has_goto = False
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
87 for symbol in self.grammar.Symbols:
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
88 nis = self.nextItemSet(itemset, symbol)
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
89 if nis:
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
90 goto_table[(itemset, symbol)] = nis
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
91 has_goto = True
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
92 if not nis in states:
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
93 worklist.append(nis)
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
94 states.add(nis)
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
95 if has_goto:
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
96 action_table[itemset] = 'shift', 's'
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
97 for item in itemset:
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
98 assert item.IsShift, item
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
99 else:
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
100 # Check for reduce-reduce conflicts:
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
101 assert len(itemset) == 1
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
102 item = list(itemset)[0]
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
103 assert item.IsReduce
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
104 action_table[itemset] = 'reduce', item.production
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
105 for s, i in zip(states, range(len(states))):
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
106 print(i, ' || '.join(str(x) for x in s))
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
107 p = LRParser()
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
108 p.goto_table = goto_table
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
109 p.action_table = action_table
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
110 p.s0 = iis
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
111 return p
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
112
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
113 class LRParser:
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
114 def parse(self, toks):
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
115 stack = [self.s0]
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
116 while stack[-1] != 'input':
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
117 action, p = self.action_table[stack[-1]]
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
118 print(action, p)
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
119 if action == 'shift':
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
120 stack.append(toks.pop(0))
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
121 s, i = stack[-2:]
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
122 stack.append(self.goto_table[(s, i)])
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
123 elif action == 'reduce':
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
124 for s in p.symbols:
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
125 stack.pop()
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
126 stack.pop()
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
127 stack.append(p.name)
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
128 s, i = stack[-2:]
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
129 if stack[-1] == 'input':
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
130 break
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
131 stack.append(self.goto_table[(s, i)])
178
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
132
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
133 # Test it:
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
134 # 1. define a simple grammar:
179
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
135 g = Grammar(['EOF', 'identifier', '(', ')', '+', '*'])
178
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
136 g.add_production('input', ['expression', 'EOF'])
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
137 g.add_production('expression', ['term'])
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
138 g.add_production('expression', ['expression', '+', 'term'])
179
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
139 g.add_production('term', ['factor'])
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
140 #g.add_production('term', ['term', '*', 'factor'])
178
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
141 g.add_production('term', ['(', 'expression', ')'])
179
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
142 g.add_production('factor', ['identifier'])
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
143 g.start_symbol = 'input'
178
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
144 # 2. define input:
179
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
145 tokens = ['identifier', '+', 'identifier', '+', 'identifier', 'EOF']
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
146 # 3. build parser:
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
147 p = LRgenerator(g).genParser()
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
148 # 4. feed input:
0f3b1adfd416 LR(0) parser
Windel Bouwman
parents: 178
diff changeset
149 p.parse(tokens)
178
c694ec551f34 Added lex yacc test scripts
Windel Bouwman
parents:
diff changeset
150