Rev 200 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/usr/bin/env python# Copyright: 2006 Ira W. Snyder# Start Date: 2006-02-01# License: GNU General Public License v2 (or at your option, any later version)#parsing_table = (('', 'BEGIN', 'END', 'IF', 'ELSE', 'WHILE', 'ID', 'READ', 'WRITE', 'NUM', '+', '(', ')', ';', '=', '$'),('program', 1, '', '', '', '', '', '', '', '', '', '', '', '', '', ''),('stmtseq', '', '', 2, '', 2, 2, 2, 2, '', '', '', '', '', '', ''),('stmtseq1', '', 4, '', '', '', '', '', '', '', '', '', '', 3, '', ''),('stmt', '', '', 5, '', 6, 7, 8, 9, '', '', '', '', '', '', ''),('exp', '', '', '', '', '', 10, '', '', 10, '', 10, '', '', '', ''),('exp1', '', 12, '', 12, '', '', '', '', '', 11, '', 12, 12, '', ''),('term', '', '', '', '', '', 15, '', '', 14, '', 13, '', '', '', ''))def terminals():"""Return a list of the terminals in the table"""return [n for n in parsing_table[0] if n != '']def nonterminals():"""Return a list of non-terminals in the table"""return [n[0] for n in parsing_table if n[0] != '']def LL1_Parsing_Algo ():"""This implements the table-based LL(1) parsing algorithm from Figure 4.2(Page 155) in Compiler Construction Principles and Practices"""END_SYMBOL = '$'s = Stack()s.push(START_SYMBOL)#while s.peek() != END_SYMBOL and input.getNextToken() != END_SYMBOL:# passclass InputParser:"""Implements a simple tokenizer for the TOY language"""def __init__(self, inputstr):self.__inputstr = inputstrdef getNextToken(self):"""Gets the first token off of the front of the input string, butDOES NOT remove any input."""passdef match(self):"""A match has happened, so remove the front of the input string."""passclass Stack:"""Implements a simple stack using a list"""def __init__(self):self.__data = []def push(self, val):"""Push a value onto the top of the stack"""self.__data.append(val)def pop(self):"""Pop a value off the top of the stack.WARNING: throws IndexError if there is nothing left in the stack"""return self.__data.pop()def peek(self):"""Peek at the top of the stack. Basically a pop() without removing theobject from the top.WARNING: will throw an IndexError if there is nothing left in the stack."""temp = self.pop()self.push(temp)return temp################################################################################################################################################################################################################################################################################################################################################################################################################def main():"""The main function for this program"""print terminals()print nonterminals()if __name__ == '__main__':main()