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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
#!/usr/bin/env python3
#
# Copyright © 2019 Keith Packard <keithp@keithp.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#
grammar = {
"lines" : (("line", "lines"),
()
),
"line" : (("expr", "#PRINT", "NL"),
("NL",),
),
"expr" : (("term", "PLUS", "expr", "#ADD"),
("term", "MINUS", "expr", "#SUBTRACT"),
("term",)
),
"term" : (("fact", "term-p"),
),
"term-p" : (("TIMES", "fact", "#TIMES", "term-p"),
("DIVIDE", "fact", "#DIVIDE", "term-p"),
(),
),
"fact" : (("OP", "expr", "CP"),
("MINUS", "fact", "#NEGATE"),
("#PUSH", "NUMBER"),
)
}
def productions(non_terminal):
return grammar[non_terminal]
# flo
#
# factor left common expressions out of a grammar
#
def head(list):
return list[0]
def rest(list):
return list[1:]
def nthrest(list, n):
return list[n:]
def error(msg):
print(msg)
exit(1)
#
# remove the first element from each of a list
# of productions (unless the production is nil)
#
def clip_firsts(prods):
ret = ()
for prod in prods:
if prod:
ret += (head(prod),)
return ret
#
# return the first duplicated element from a list of elements
#
def find_common(firsts):
while firsts:
f = head(firsts)
firsts = rest(firsts)
if f in firsts:
return f
#
# return a list of productions which have the given
# element as their first member
#
def with_first(prods, first):
ret = ()
for prod in prods:
if first == head(prod):
ret = ret + (prod,)
return ret
#
# return a list of productions which *don't* have the given
# element as their first member
#
def without_first (prods, first):
ret = ()
for prod in prods:
if first != head(prod):
ret = ret + (prod,)
return ret
#
# strip the first 'count' elements off a list of productions
#
def remove_firsts (prods, count):
ret = ()
for prod in prods:
ret = ret + (nthrest(prod, count),)
return ret
#
# return True if each production in the list has the same first
# element
#
def all_common_first(prods):
while len(prods) >= 2:
if head(prods[0]) != head(prods[1]):
return False
prods = rest(prods)
return True
#
# return the maximal list of common first sub-lists
# from a set of productions
#
def max_common(prods):
ret = ()
while all_common_first(prods):
ret = ret + (head(prods[0]),)
prods = remove_firsts(prods, 1)
return ret
#
# factor out common left sub-lists from the list
# of productions
#
def eliminate_common(non_terminal):
prods = productions(non_terminal)
firsts = clip_firsts(prods)
common = find_common(firsts)
if common:
removed = with_first(prods, common)
remain = without_first(prods, common)
common_list = max_common(removed)
new_non_terminal = non_terminal
while True:
new_non_terminal = new_non_terminal + "-p"
if new_non_terminal not in grammar:
break
grammar[new_non_terminal] = remove_firsts(removed, len(common_list))
grammar[non_terminal] = ((common_list + (new_non_terminal,)),) + remain
#
# remove common left sub-expressions from each non-terminal
# in the grammar
#
def factor_left(non_terminals):
for non_terminal in non_terminals:
eliminate_common(non_terminal)
def get_non_terminals(grammar):
non_terminals = ()
for e in grammar:
non_terminals += (e,)
return non_terminals
def dump_grammar(grammar):
for nt,prods in grammar.items():
print("%s" % nt)
started = False
for prod in prods:
label = ":"
if started:
label = "|"
print("\t%s %s" % (label, prod))
started = True
print("\t;")
def flo():
factor_left(get_non_terminals(grammar))
flo()
dump_grammar(grammar)
|