Chinaunix首页 | 论坛 | 博客
  • 博客访问: 272606
  • 博文数量: 53
  • 博客积分: 1910
  • 博客等级: 中尉
  • 技术积分: 1130
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-10 14:56
文章分类

全部博文(53)

文章存档

2013年(1)

2012年(17)

2011年(33)

2010年(2)

分类: Python/Ruby

2011-05-11 18:51:05

promote,python 代码,最后test.py 是测试代码
 
MyTreeFilter.py (3.12 对python不直接支持tree filter ,参考了antlr 论坛上的一个帖子)
  1. from antlr3.tree import *
  2. from antlr3 import TokenStream
  3. from antlr3 import RecognizerSharedState, RecognitionException
  4. from antlr3 import NoViableAltException, BacktrackingFailed



  5. class MyTreeFilter(TreeParser):
  6.     '''
  7.     public TreeFilter(TreeNodeStream input) {
  8.         super(input);
  9.     }
  10.     '''
  11.     def __init__(self,input, state=None):
  12.         if state == None:
  13.             super(MyTreeFilter,self).__init__(input)
  14.         else:
  15.                 super(MyTreeFilter,self).__init__(input, state)
  16.             
  17.         self.originalAdaptor = input.getTreeAdaptor();
  18.         #print 'liutest original adaptor'
  19.         #print self.originalAdaptor
  20.         self.originalTokenStream = input.getTokenStream();

  21.     def applyOnce(self, t, rule):
  22.         if t == None: return None
  23.         
  24.         try:
  25.             self._state = RecognizerSharedState()
  26.             self.input = CommonTreeNodeStream(self.originalAdaptor, t)
  27.             self.input.setTokenStream(self.originalTokenStream)
  28.             self.setBacktrackingLevel(1)
  29.             r = rule()
  30.             self.setBacktrackingLevel(0)
  31.             
  32.             # there is no failed state so skipping this check; failure should cause an exception to be thrown?
  33.             #if (self.failed() ): return t
  34.             
  35.             #if ( self.showTransformations and r != None and t != r.tree and r.tree != None):
  36.             # self.reportTransformation(t, r.tree)
  37.             
  38.             if ( r != None and r.tree != None ):
  39.                 return r.tree
  40.             else:
  41.                 return t
  42.         #except RecognitionException,re:
  43.         except RecognitionException:
  44.             pass
  45.             #print 'liutest applyOnce RecognitionException'
  46.             #self.reportError(re)
  47.             #self.recover(self.input, re)
  48.         #except BacktrackingFailed:
  49.         # pass


  50.         return t

  51.     #def applyRepeatedly(self, t, rule):
  52.     # treeChanged = True
  53.     # while treeChanged:
  54.     # u = self.applyOnce(t, rule)
  55.     # treeChanged = (t != u)
  56.     # t = u
  57.     # return t
  58.         
  59.     #def downup(self, t, showTransformations=False):
  60.     def downup(self, t):
  61.         #self.showTransformations = showTransformations
  62.         v = TreeVisitor(self.originalAdaptor) #TreeVisitor(CommonTreeAdaptor())
  63.         #t = v.visit(t, lambda t: self.applyOnce(t, self.topdown), lambda t: self.applyRepeatedly(t, self.bottomup))
  64.         t = v.visit(t, lambda t: self.applyOnce(t, self.topdown), lambda t: self.applyOnce(t, self.bottomup))
  65.         return t

  66.     
  67.     #def reportTransformation(self, oldTree, newTree):
  68.     # print oldTree.toStringTree() + " -> " + newTree.toStringTree()
  69.     
  70.     # methods the downup strategy uses to do the up and down rules.
  71.     # to override, just define tree grammar rule topdown and turn on
  72.     # filter=true.
  73.         
  74.     def topdown(self):
  75.         return None
  76.         #raise NotImplementedError("topdown abstract")
  77.     
  78.     def bottomup(self):
  79.         return None
  80.         #raise NotImplementedError("bottomup abstract")
 
MethodSymbol.py
  1. from ScopedSymbol import ScopedSymbol

  2. class MethodSymbol(ScopedSymbol):

  3.     #Map<String, Symbol> orderedArgs = new LinkedHashMap<String, Symbol>();

  4.     def __init__(self,name,retType,parent):
  5.         #self.orderedArgs = {} # liunote dict is not ordered
  6.         self.orderedArgs = []
  7.         #super(MethodSymbol,self).__init__(name, retType, parent) # liunote error see MethodSymbol init func param
  8.         super(MethodSymbol,self).__init__(name, parent,retType)

  9.     def getMembers(self):
  10.         return self.orderedArgs
  11.         
  12.     def get(self,name):
  13.         for item in self.orderedArgs:
  14.             if type(item) != tuple:
  15.                 raise RuntimeError('orderedArgs item is not tuple')
  16.             else:
  17.              if item[0] == name:
  18.                  return item[1]
  19.             
  20.         return None

  21.     def getName(self):
  22.         #return name+"("+ stripBrackets(orderedArgs.keySet().toString())+")";
  23.         #return name+"("+ stripBrackets(str(self.symbols.items())+")"
  24.         #return self.name + "("+ stripBrackets(str(self.orderedArgs.keys())+")"
  25.         return self.name + "(" + ScopedSymbol.stripBrackets(str([item[0] for item in self.orderedArgs])) + ")"
 
LocalScope.py
  1. from BaseScope import BaseScope

  2. class LocalScope(BaseScope):
  3.     
  4.     #public LocalScope(Scope parent) { super(parent); }
  5.     def __init__(self, parent):
  6.         super(LocalScope,self).__init__(parent)
  7.     
  8.     #public String getScopeName() { return "local"; }
  9.     def getScopeName(self):
  10.         return "local"

GlobalScope.py

  1. from BaseScope import *

  2. class GlobalScope( BaseScope ):
  3.     #public GlobalScope() { super(null); }
  4.     def __init__(self):
  5.         super(GlobalScope,self).__init__(None)
  6.     
  7.     #public String getScopeName() { return "global"; }
  8.     def getScopeName(self):
  9.          return "global"

 

CymbolListener.py

 

  1. from Scope import abstract

  2. class CymbolListener(object):
  3.     
  4.     #def __init__(self):
  5.     # if self.__class__ is Scope:
  6.     # abstract()

  7.     def info(self,msg):
  8.         #abstract()
  9.         print msg
  10.         
  11.     def error(self, msg):
  12.         #abstract()
  13.         print msg

CymbolErrorNode.py

 

  1. from antlr3 import RecognitionException
  2. from antlr3 import Token
  3. from antlr3 import TokenStream
  4. from antlr3.tree import CommonErrorNode


  5. class CymbolErrorNode (CymbolAST) :
  6.     
  7.     #CommonErrorNode delegate

  8.     def __init__(self,input, start, stop, e):
  9.         self.delegate = CommonErrorNode(input,start,stop,e)
  10.     

  11.     def isNil(self) :
  12.         return self.delegate.isNil() # Indicates the node is a nil node but may still have children, meaning the tree is a flat list

  13.     def getType(self) :
  14.         return self.delegate.getType()

  15.     def getText(self) :
  16.         return self.delegate.getText()
  17.         
  18.     def toString(self) :
  19.         return self.delegate.toString()

 

CymbolAST.py

 

  1. from antlr3 import RecognitionException
  2. from antlr3 import Token
  3. from antlr3 import TokenStream
  4. from antlr3.tree import CommonErrorNode


  5. class CymbolErrorNode (CymbolAST) :
  6.     
  7.     #CommonErrorNode delegate

  8.     def __init__(self,input, start, stop, e):
  9.         self.delegate = CommonErrorNode(input,start,stop,e)
  10.     

  11.     def isNil(self) :
  12.         return self.delegate.isNil() # Indicates the node is a nil node but may still have children, meaning the tree is a flat list

  13.     def getType(self) :
  14.         return self.delegate.getType()

  15.     def getText(self) :
  16.         return self.delegate.getText()
  17.         
  18.     def toString(self) :
  19.         return self.delegate.toString()

 

ClassSymbol.py
  1. from ScopedSymbol import ScopedSymbol
  2. from Scope import Scope
  3. from Type import Type


  4. class ClassSymbol ( ScopedSymbol, Scope, Type ):
  5.     #/** This is the superclass not enclosingScope field. We still record
  6.     # * the enclosing scope so we can push in and pop out of class defs.
  7.     # */
  8.     
  9.     #ClassSymbol superClass;
  10.     
  11.     # /** List of all fields and methods */
  12.     # public Map<String,Symbol> members =new LinkedHashMap<String,Symbol>();
  13.     
  14.     
  15.     #public ClassSymbol(String name, Scope enclosingScope, ClassSymbol superClass) {
  16.     # super(name, enclosingScope);
  17.     # this.superClass = superClass;
  18.     #}
  19.     def __init__(self, name, enclosingScope, superClass) {
  20.         self.members = []
  21.         super(ClassSymbol,self).__init__(name, enclosingScope)
  22.         self.superClass = superClass;
  23.     
  24.     
  25.     def getParentScope(self) :
  26.         if self.name == "Object" : return enclosingScope # globals ,liunote self.name at Symbol.py
  27.         return superClass # if not Object, return super
  28.     }

  29.     # /** For a.b, only look in a's class hierarchy to resolve b, not globals */
  30.     #public Symbol resolveMember(String name) {
  31.     # Symbol s = members.get(name);
  32.     # if ( s!=null ) return s;
  33.     # // if not here, check any enclosing scope
  34.     # if ( superClass != null ) {
  35.     # return superClass.resolveMember(name);
  36.     # }
  37.     # return null; // not found
  38.     #}
  39.     def resolveMember(self, name) :
  40.         s = get(name)
  41.         if s != None : return s
  42.         # if not here, check any enclosing scope
  43.         if self.superClass != None :
  44.             return self.superClass.resolveMember(name)
  45.         
  46.         return None # not found
  47.     
  48.     
  49.     def get(self,name):
  50.         for item in self.members:
  51.             if type(item) != tuple:
  52.                 raise RuntimeError('orderedArgs item is not tuple

 

BuiltInTypeSymbol.py

 

  1. from Symbol import Symbol
  2. from Type import Type


  3. #public class BuiltInTypeSymbol extends Symbol implements Type {
  4. class BuiltInTypeSymbol (Symbol,Type): # liunote if def Interface type , but not implement it method ,then raise no implement error
  5.     
  6.     #int typeIndex;
  7.     
  8.     def __init__(self,name,typeIndex):
  9.         print 'liutest super class Symbol init'
  10.         Symbol.__init__(self,name)
  11.         self.typeIndex = typeIndex
  12.     
  13.     def getTypeIndex(self):
  14.         return self.typeIndex
  15.         
  16.     def toString(self):
  17.         return self.getName() # liunote will call Symbol

BaseScope.py

 

  1. from Scope import Scope

  2. class BaseScope(Scope): # BaseScope is abstract class

  3.     def __init__(self,parent):
  4.         #self.symbols = {} # liunote dict dnot use as linked hash map
  5.         self.symbols = [] # [(sym.name),(sym)] => ([x][0],[x],[1])
  6.         self.enclosingScope = parent # Scope enclosingScope; null if global (outermost) scope
  7.         

  8.     def resolve(self,name):
  9.         # s = self.symbols.get(name) # liunote
  10.         s = self.get(name) # liunote :get(self,name) call instance method,not use self param
  11.         if s != None: return s

  12.     # if not here, check any enclosing scope
  13.     if self.enclosingScope != None: return self.enclosingScope.resolve(name)
  14.     return None # not found


  15.     #public void define(Symbol sym) {
  16.     def define(self,sym):
  17.         #self.symbols.put(sym.name, sym) # liunote use tuple list
  18.         self.symbols.append((sym.name, sym)) # no !!! liunote ??? name at Symbol.py , is parent class ,only derive method !!!
  19.                                               # liunote java sym.name diff getName ,sym.name only is parent Symbol class menber
  20.     sym.scope = self # track the scope in each symbol # liunote self will subclass instance's self !!!


  21.     #public Scope getEnclosingScope() { return enclosingScope; }
  22.     def getEnclosingScope(self):
  23.         return self.enclosingScope
  24.     
  25.     #public String toString() { return symbols.keySet().toString(); }
  26.     def toString(self):
  27.          #return self.symbols.keySet().toString() # liunote keySet
  28.          #str(self.symbols.items())
  29.          #return str(self.symbols.keys()) # liunote
  30.          return str([item[0] for item in self.symbols])
  31.         
  32.     def get(self,name):
  33.         for item in self.symbols:
  34.             if type(item) != tuple:
  35.                 raise RuntimeError('symbols item is not tuple

 

 

  1. from Symbol import Symbol
  2. from Type import Type

  3. class ArrayType(Symbol,Type) :
  4.     
  5.     def __init__(self,    elementType):
  6.         super(ArrayType,self).__init__("[]",elementType) #(elementType+"[]") liunote use (name,type) ,or (name) param ???
  7.         self.elementType = elementType

  8.     def getTypeIndex(self):
  9.         return 0
  10.         
  11.     #def getElementType(self):
  12.     #    return self.elementType
  13.         
  14.     #elementType = property(getElementType)

 

test.py

 

  1. from antlr3 import *
  2. from antlr3.tree import *

  3. from CymbolLexer import CymbolLexer
  4. from CymbolParser import CymbolParser

  5. from Def import Def
  6. from Types import Types

  7. from SymbolTable import SymbolTable

  8. from CymbolAST import CymbolAST


  9. #class MyNode(CommonTree):
  10. # def __init__(self):
  11. # self.Symbol={}
  12.         
  13. #class MyNodeAdaptor(CommonTreeAdaptor):
  14. # def create(payload):
  15. #     return MyNode(payload)

  16. class CymbolAdaptor(CommonTreeAdaptor):
  17.     
  18.         #def create(self,token):
  19.         # print ' liutest CymbolAdaptor creat CymbolAST!!!'
  20.         # return CymbolAST(token)
  21.         
  22.         def createWithPayload(self, payload):
  23.             #print ' liutest CymbolAdaptor creat CymbolAST!!!'
  24.             return CymbolAST(payload)

  25.         
  26.         def dupNode(self,t) :
  27.             if t == None :
  28.                 return None
  29.             return self.create(t.token) # (CymbolAST)t

  30.         def errorNode( self,input, start, stop, e):
  31.             print 'liu:CymbolAdaptor errorNode'
  32.             return CymbolErrorNode(input,start,stop,e)

  33. #ANTLRFileStream
  34. #CharStream input = null;
  35. filename = '/home/liu/promote/t.cymbol'

  36. input = ANTLRFileStream(filename);
  37. #input = antlr3.ANTLRInputStream(sys.stdin)

  38. lexer = CymbolLexer(input)

  39. #tokens = antlr3.CommonTokenStream(lexer)
  40. tokens = TokenRewriteStream(lexer)

  41. #parser = ExprParser(tokens)
  42. parser = CymbolParser(tokens)

  43. cymbolAdaptor = CymbolAdaptor()
  44. parser.setTreeAdaptor(cymbolAdaptor) # call to BaseTreeAdaptor

 

测试输入

t.symbol:

 

  1. int n;
  2. float a[];
  3. int d[];
  4. void f() {
  5.     a[0] = 4*'i'; // promote char to int to float
  6.     a[1] = d[0]; // promote int element to float
  7.     a['x'] = 1; // check array index promotion
  8.     g('q',10); // arg promotion
  9. }
  10. int g(int x, float y) { return 'k'; } // promote 'k' to int

u.cymbol:

 

  1. float a[];
  2. int d[];
  3. int c = 'z';
  4. void f() {
  5.     a[3] = a[0]+4*'i'+d[0];
  6.     boolean b = a[3+'a'] < 3.4;
  7.     float f = a['x']+g('q',10);
  8.     f = 3;
  9.     int i;
  10.     struct A { int x; };
  11.     struct A s;
  12.     int j = h(1)+4*a[i]+s.x;
  13. }
  14. int g(int x, float y) { return 'k'; }
  15. int h(int x) { return x; }

  16. float ff = g(3,3.4);
  17. float gg = d[0];

 

 

阅读(1170) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~