1*ca1c9b0cSelric# 2*ca1c9b0cSelric# Copyright (c) 2008 Kungliga Tekniska Högskolan 3*ca1c9b0cSelric# (Royal Institute of Technology, Stockholm, Sweden). 4*ca1c9b0cSelric# All rights reserved. 5*ca1c9b0cSelric# 6*ca1c9b0cSelric# Redistribution and use in source and binary forms, with or without 7*ca1c9b0cSelric# modification, are permitted provided that the following conditions 8*ca1c9b0cSelric# are met: 9*ca1c9b0cSelric# 10*ca1c9b0cSelric# 1. Redistributions of source code must retain the above copyright 11*ca1c9b0cSelric# notice, this list of conditions and the following disclaimer. 12*ca1c9b0cSelric# 13*ca1c9b0cSelric# 2. Redistributions in binary form must reproduce the above copyright 14*ca1c9b0cSelric# notice, this list of conditions and the following disclaimer in the 15*ca1c9b0cSelric# documentation and/or other materials provided with the distribution. 16*ca1c9b0cSelric# 17*ca1c9b0cSelric# 3. Neither the name of the Institute nor the names of its contributors 18*ca1c9b0cSelric# may be used to endorse or promote products derived from this software 19*ca1c9b0cSelric# without specific prior written permission. 20*ca1c9b0cSelric# 21*ca1c9b0cSelric# THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22*ca1c9b0cSelric# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23*ca1c9b0cSelric# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24*ca1c9b0cSelric# ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25*ca1c9b0cSelric# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26*ca1c9b0cSelric# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27*ca1c9b0cSelric# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28*ca1c9b0cSelric# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29*ca1c9b0cSelric# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30*ca1c9b0cSelric# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31*ca1c9b0cSelric# SUCH DAMAGE. 32*ca1c9b0cSelric 33*ca1c9b0cSelric 34*ca1c9b0cSelricimport sys 35*ca1c9b0cSelric 36*ca1c9b0cSelrictokens = [ 'SYMBOL' ] 37*ca1c9b0cSelricliterals = ['{','}',';', ':'] 38*ca1c9b0cSelric 39*ca1c9b0cSelrict_SYMBOL = r'[a-zA-Z_][a-zA-Z0-9_\.]*' 40*ca1c9b0cSelrict_ignore = " \t\n" 41*ca1c9b0cSelric 42*ca1c9b0cSelricdef t_error(t): 43*ca1c9b0cSelric print "Illegal character '%s'" % t.value[0] 44*ca1c9b0cSelric t.lexer.skip(1) 45*ca1c9b0cSelric 46*ca1c9b0cSelricimport ply.lex as lex 47*ca1c9b0cSelriclex.lex() 48*ca1c9b0cSelric 49*ca1c9b0cSelricnamespace = "global" 50*ca1c9b0cSelricsymbols = [] 51*ca1c9b0cSelric 52*ca1c9b0cSelricdef p_syms(p): 53*ca1c9b0cSelric 'syms : SYMBOL "{" elements "}"' 54*ca1c9b0cSelric print "# %s" % p[1] 55*ca1c9b0cSelric 56*ca1c9b0cSelricdef p_elements(p): 57*ca1c9b0cSelric '''elements : element 58*ca1c9b0cSelric | element elements''' 59*ca1c9b0cSelric 60*ca1c9b0cSelricdef p_element(p): 61*ca1c9b0cSelric '''element : SYMBOL ":" 62*ca1c9b0cSelric | SYMBOL ";"''' 63*ca1c9b0cSelric global namespace 64*ca1c9b0cSelric if p[2] == ':': 65*ca1c9b0cSelric namespace = p[1] 66*ca1c9b0cSelric else: 67*ca1c9b0cSelric symbols.append([namespace, p[1]]) 68*ca1c9b0cSelric 69*ca1c9b0cSelricdef p_error(p): 70*ca1c9b0cSelric if p: 71*ca1c9b0cSelric print "Syntax error at '%s'" % p.value 72*ca1c9b0cSelric else: 73*ca1c9b0cSelric print "Syntax error at EOF" 74*ca1c9b0cSelric 75*ca1c9b0cSelricimport ply.yacc as yacc 76*ca1c9b0cSelricyacc.yacc() 77*ca1c9b0cSelric 78*ca1c9b0cSelriclines = sys.stdin.readlines() 79*ca1c9b0cSelric 80*ca1c9b0cSelricfor line in lines: 81*ca1c9b0cSelric yacc.parse(line) 82*ca1c9b0cSelric 83*ca1c9b0cSelricfor symbol in symbols: 84*ca1c9b0cSelric if symbol[0] == "global": 85*ca1c9b0cSelric print "%s" % symbol[1] 86