Subversion Repositories programming

Rev

Rev 199 | Rev 201 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 199 Rev 200
Line 1... Line 1...
1
#!/usr/bin/env python
1
#!/usr/bin/env python
2
 
2
 
-
 
3
#
3
# Copyright: 2006 Ira W. Snyder
4
# Copyright: 2006 Ira W. Snyder
4
# Start Date: 2006-02-01
5
# Start Date: 2006-02-01
5
# License: GNU General Public License v2 (or at your option, any later version)
6
# License: GNU General Public License v2 (or at your option, any later version)
6
#
7
#
7
 
8
 
Line 12... Line 13...
12
                 ('stmt', '', '', 5, '', 6, 7, 8, 9, '', '', '', '', '', '', ''),
13
                 ('stmt', '', '', 5, '', 6, 7, 8, 9, '', '', '', '', '', '', ''),
13
                 ('exp', '', '', '', '', '', 10, '', '', 10, '', 10, '', '', '', ''),
14
                 ('exp', '', '', '', '', '', 10, '', '', 10, '', 10, '', '', '', ''),
14
                 ('exp1', '', 12, '', 12, '', '', '', '', '', 11, '', 12, 12, '', ''),
15
                 ('exp1', '', 12, '', 12, '', '', '', '', '', 11, '', 12, 12, '', ''),
15
                 ('term', '', '', '', '', '', 15, '', '', 14, '', 13, '', '', '', ''))
16
                 ('term', '', '', '', '', '', 15, '', '', 14, '', 13, '', '', '', ''))
16
 
17
 
-
 
18
grammar_spec = (('program', 'BEGIN', 'stmtseq', 'END'),
-
 
19
                ('stmtseq', 'stmt', 'stmtseq1'),
-
 
20
                ('stmtseq1', ';', 'stmt', 'stmtseq1'),
-
 
21
                ('stmtseq1', ),
-
 
22
                ('stmt', 'IF', '(', 'exp', ')', 'stmt', 'ELSE', 'stmt'),
-
 
23
                ('stmt', 'WHILE', '(', 'exp', ')', 'stmt'),
-
 
24
                ('stmt', 'ID', '=', 'exp'),
-
 
25
                ('stmt', 'READ', 'ID'),
-
 
26
                ('stmt', 'WRITE', 'exp'),
-
 
27
                ('exp', 'term', 'exp1'),
-
 
28
                ('exp1', '+', 'term', 'exp1'),
-
 
29
                ('exp1', ),
-
 
30
                ('term', '(', 'exp', ')'),
-
 
31
                ('term', 'NUM'),
-
 
32
                ('term', 'ID'))
-
 
33
 
-
 
34
 
17
def terminals():
35
def terminals():
18
    """Return a list of the terminals in the table"""
36
    """Return a list of the terminals in the table"""
19
 
37
 
20
    return [n for n in parsing_table[0] if n != '']
38
    return [n for n in parsing_table[0] if n != '']
21
 
39
 
Line 32... Line 50...
32
    s = Stack()
50
    s = Stack()
33
    s.push(START_SYMBOL)
51
    s.push(START_SYMBOL)
34
 
52
 
35
    #while s.peek() != END_SYMBOL and input.getNextToken() != END_SYMBOL:
53
    #while s.peek() != END_SYMBOL and input.getNextToken() != END_SYMBOL:
36
    #    pass
54
    #    pass
37
    
55
 
38
class InputParser:
56
class InputParser:
39
    """Implements a simple tokenizer for the TOY language"""
57
    """Implements a simple tokenizer for the TOY language"""
40
 
58
 
41
    def __init__(self, inputstr):
59
    def __init__(self, inputstr):
42
        self.__inputstr = inputstr
60
        self.__inputstr = inputstr.split()
43
 
61
 
44
    def getNextToken(self):
62
    def getNextToken(self):
45
        """Gets the first token off of the front of the input string, but
63
        """Gets the first token off of the front of the input string, but
46
        DOES NOT remove any input."""
64
        DOES NOT remove any input."""
-
 
65
        
47
        pass
66
        try:
-
 
67
            if self.__inputstr[0] in terminals():
-
 
68
                return self.__inputstr[0]
-
 
69
            else:
-
 
70
                return 'BAD INPUT'
-
 
71
        except IndexError:
-
 
72
            print 'CAUGHT IndexError in getNextToken()'
-
 
73
            return ''
48
 
74
 
49
    def match(self):
75
    def match(self):
50
        """A match has happened, so remove the front of the input string."""
76
        """A match has happened, so remove the front of the input string."""
-
 
77
        
51
        pass
78
        try:
-
 
79
            del self.__inputstr[0]
-
 
80
        except IndexError:
-
 
81
            print 'CAUGHT IndexError in match()'
52
 
82
 
53
class Stack:
83
class Stack:
54
    """Implements a simple stack using a list"""
84
    """Implements a simple stack using a list"""
55
 
85
 
56
    def __init__(self):
86
    def __init__(self):
Line 81... Line 111...
81
################################################################################
111
################################################################################
82
 
112
 
83
def main():
113
def main():
84
    """The main function for this program"""
114
    """The main function for this program"""
85
 
115
 
-
 
116
    i = InputParser('BEGIN  READ ID ; WHILE ( ID ) ID  = ID + NUM ;  WRITE ID END')
-
 
117
    
-
 
118
    while i.getNextToken() != '':
86
    print terminals()
119
        print i.getNextToken()
87
    print nonterminals()
120
        i.match()
-
 
121
 
88
   
122
 
-
 
123
 
89
if __name__ == '__main__':
124
if __name__ == '__main__':
90
    main()
125
    main()
91
 
126