diff python/yacc.py @ 334:6f4753202b9a

Added more recipes
author Windel Bouwman
date Thu, 13 Feb 2014 22:02:08 +0100
parents e9fe6988497c
children c7cc54c0dfdf
line wrap: on
line diff
--- a/python/yacc.py	Sun Feb 09 15:27:57 2014 +0100
+++ b/python/yacc.py	Thu Feb 13 22:02:08 2014 +0100
@@ -1,7 +1,7 @@
 #!/usr/bin/python
 
 """
-Parser generator utility. This script can generate a python script from a 
+Parser generator utility. This script can generate a python script from a
 grammar description.
 
 Invoke the script on a grammar specification file:
@@ -42,7 +42,7 @@
 import types
 import io
 import logging
-from pyyacc import Grammar, print_grammar
+from pyyacc import Grammar
 
 
 class XaccLexer:
@@ -244,15 +244,15 @@
 
         # Fill goto table:
         self.print('        self.goto_table = {}')
-        for gt in self.goto_table:
-            to = self.goto_table[gt]
-            self.print('        self.goto_table[{}] = {}'.format(gt, to))
+        for state_number in self.goto_table:
+            to = self.goto_table[state_number]
+            self.print('        self.goto_table[{}] = {}'.format(state_number, to))
         self.print('')
 
         # Generate a function for each action:
         for rule in self.grammar.productions:
-            M = len(rule.symbols)
-            args = ', '.join('arg{}'.format(n + 1) for n in range(M))
+            num_symbols = len(rule.symbols)
+            args = ', '.join('arg{}'.format(n + 1) for n in range(num_symbols))
             self.print('    def {}(self, {}):'.format(rule.f_name, args))
             if rule.f == None:
                 semantics = 'pass'
@@ -260,7 +260,7 @@
                 semantics = str(rule.f)
                 if semantics.strip() == '':
                     semantics = 'pass'
-            for n in range(M):
+            for n in range(num_symbols):
                 semantics = semantics.replace('${}'.format(n + 1), 'arg{}'.format(n + 1))
             self.print('        {}'.format(semantics))
             self.print('')
@@ -298,7 +298,6 @@
     # Sequence source through the generator parts:
     lexer.feed(src)
     grammar = parser.parse_grammar()
-    # TODO: optionize this: print_grammar(grammar)
     generator.generate(grammar, parser.headers, args.output)