]> git.donarmstrong.com Git - lilypond.git/blob - scripts/auxiliar/yyout2grammar.py
Merge commit 'origin'
[lilypond.git] / scripts / auxiliar / yyout2grammar.py
1 #! /usr/bin/env python
2
3 #  Convert from bison output file parser.output to
4 #  Grammar and index.
5 #  Drops all of the state information.
6 #  Converts \\ to \
7 #  Eliminates the @ variables created when {} is placed in the middle of a rule##  all of the c-code stuff
8 #
9 #
10 #  Copyright 2005 by Carl D. Sorensen
11 #
12
13 # to create input file, run
14 #   bison -v parser.yy
15 # this will create a file parser.output
16 # then run
17 #   yyout2grammar.py parser.output your_output_file
18 #
19
20 import sys
21 import re
22
23 atre = re.compile('(@\d+):')
24
25 write_me = True
26
27 if len(sys.argv)!=3:
28     print "Usage: yyout2grammar.py parser_output_file grammar_file."
29 else:
30     in_name = sys.argv[1] 
31     out_name = sys.argv[2]
32
33     print "input file name",in_name
34     print "output file name",out_name
35     in_file = open(in_name,'r')
36     out_file= open(out_name, 'w')
37
38     at_items=[]
39     inline = in_file.readline()
40     while inline != '' and not(inline.startswith("Grammar")):
41         inline = in_file.readline()
42     if inline != '':
43         out_file.write(inline)
44         inline = in_file.readline()
45         while inline != '' and not(inline.startswith("Terminals")):
46             i = inline.find("$accept:")
47             if i>-1:
48                 write_me = False
49                 inline = in_file.readline()
50             atfound = re.findall(atre,inline)
51             if len(atfound) > 0:
52                 at_items.extend(atfound)
53                 print at_items
54                 write_me = False
55                 inline=in_file.readline()
56             else:
57                 for at_item in at_items:
58                     i=inline.find(at_item)
59                     if i >= 0:
60                         inline=inline[:i] + inline[i+len(at_item):]
61                     i=inline.find('"\\\\')
62                     while i > -1 :
63                         inline = inline[:i+1]+inline[i+2:]
64                         i = inline.find('"\\\\')
65             if write_me:
66                 out_file.write(inline)
67             inline = in_file.readline()
68             write_me = True
69     index_items = []
70     #  Write the Terminals header line and the following blank line
71     out_file.write(inline)
72     inline = in_file.readline()
73     out_file.write(inline)
74     inline = in_file.readline()
75     while inline != '' and not(inline.startswith("Nonterminals")):
76         i=inline.find('"\\\\')
77         while i > -1 :
78             inline = inline[:i+1]+inline[i+2:]
79             i = inline.find('"\\\\')
80         index_items.append(inline)
81         inline = in_file.readline()
82     index_items.sort(lambda x,y:cmp(x.lower(),y.lower()))
83     for index_item in index_items:
84         out_file.write(index_item)
85     out_file.write('\n')
86     # Write the Nonterminals header and the blank line
87     out_file.write(inline)
88     inline = in_file.readline()
89     out_file.write(inline)
90     index_items = []
91     index_item=in_file.readline()
92     inline=in_file.readline()
93     while inline != '' and not(inline.startswith("state")):
94         while inline.startswith("    "):
95             index_item = index_item + inline
96             inline = in_file.readline()
97         if not(index_item.startswith("@")) and \
98                not(index_item.startswith("$accept")):
99             index_items.append(index_item)
100         index_item = inline
101         inline=in_file.readline()
102     index_items.sort(lambda x,y:cmp(x.lower(),y.lower()))
103     for index_item in index_items:
104         out_file.write(index_item)