promote,python 代码,最后test.py 是测试代码
MyTreeFilter.py (3.12 对python不直接支持tree filter ,参考了antlr 论坛上的一个帖子)
- from antlr3.tree import *
- from antlr3 import TokenStream
- from antlr3 import RecognizerSharedState, RecognitionException
- from antlr3 import NoViableAltException, BacktrackingFailed
- class MyTreeFilter(TreeParser):
- '''
- public TreeFilter(TreeNodeStream input) {
- super(input);
- }
- '''
- def __init__(self,input, state=None):
- if state == None:
- super(MyTreeFilter,self).__init__(input)
- else:
- super(MyTreeFilter,self).__init__(input, state)
-
- self.originalAdaptor = input.getTreeAdaptor();
- #print 'liutest original adaptor'
- #print self.originalAdaptor
- self.originalTokenStream = input.getTokenStream();
- def applyOnce(self, t, rule):
- if t == None: return None
-
- try:
- self._state = RecognizerSharedState()
- self.input = CommonTreeNodeStream(self.originalAdaptor, t)
- self.input.setTokenStream(self.originalTokenStream)
- self.setBacktrackingLevel(1)
- r = rule()
- self.setBacktrackingLevel(0)
-
- # there is no failed state so skipping this check; failure should cause an exception to be thrown?
- #if (self.failed() ): return t
-
- #if ( self.showTransformations and r != None and t != r.tree and r.tree != None):
- # self.reportTransformation(t, r.tree)
-
- if ( r != None and r.tree != None ):
- return r.tree
- else:
- return t
- #except RecognitionException,re:
- except RecognitionException:
- pass
- #print 'liutest applyOnce RecognitionException'
- #self.reportError(re)
- #self.recover(self.input, re)
- #except BacktrackingFailed:
- # pass
- return t
- #def applyRepeatedly(self, t, rule):
- # treeChanged = True
- # while treeChanged:
- # u = self.applyOnce(t, rule)
- # treeChanged = (t != u)
- # t = u
- # return t
-
- #def downup(self, t, showTransformations=False):
- def downup(self, t):
- #self.showTransformations = showTransformations
- v = TreeVisitor(self.originalAdaptor) #TreeVisitor(CommonTreeAdaptor())
- #t = v.visit(t, lambda t: self.applyOnce(t, self.topdown), lambda t: self.applyRepeatedly(t, self.bottomup))
- t = v.visit(t, lambda t: self.applyOnce(t, self.topdown), lambda t: self.applyOnce(t, self.bottomup))
- return t
-
- #def reportTransformation(self, oldTree, newTree):
- # print oldTree.toStringTree() + " -> " + newTree.toStringTree()
-
- # methods the downup strategy uses to do the up and down rules.
- # to override, just define tree grammar rule topdown and turn on
- # filter=true.
-
- def topdown(self):
- return None
- #raise NotImplementedError("topdown abstract")
-
- def bottomup(self):
- return None
- #raise NotImplementedError("bottomup abstract")
MethodSymbol.py
- from ScopedSymbol import ScopedSymbol
- class MethodSymbol(ScopedSymbol):
- #Map<String, Symbol> orderedArgs = new LinkedHashMap<String, Symbol>();
- def __init__(self,name,retType,parent):
- #self.orderedArgs = {} # liunote dict is not ordered
- self.orderedArgs = []
- #super(MethodSymbol,self).__init__(name, retType, parent) # liunote error see MethodSymbol init func param
- super(MethodSymbol,self).__init__(name, parent,retType)
- def getMembers(self):
- return self.orderedArgs
-
- def get(self,name):
- for item in self.orderedArgs:
- if type(item) != tuple:
- raise RuntimeError('orderedArgs item is not tuple')
- else:
- if item[0] == name:
- return item[1]
-
- return None
- def getName(self):
- #return name+"("+ stripBrackets(orderedArgs.keySet().toString())+")";
- #return name+"("+ stripBrackets(str(self.symbols.items())+")"
- #return self.name + "("+ stripBrackets(str(self.orderedArgs.keys())+")"
- return self.name + "(" + ScopedSymbol.stripBrackets(str([item[0] for item in self.orderedArgs])) + ")"
LocalScope.py
- from BaseScope import BaseScope
- class LocalScope(BaseScope):
-
- #public LocalScope(Scope parent) { super(parent); }
- def __init__(self, parent):
- super(LocalScope,self).__init__(parent)
-
- #public String getScopeName() { return "local"; }
- def getScopeName(self):
- return "local"
GlobalScope.py
- from BaseScope import *
- class GlobalScope( BaseScope ):
- #public GlobalScope() { super(null); }
- def __init__(self):
- super(GlobalScope,self).__init__(None)
-
- #public String getScopeName() { return "global"; }
- def getScopeName(self):
- return "global"
CymbolListener.py
- from Scope import abstract
- class CymbolListener(object):
-
- #def __init__(self):
- # if self.__class__ is Scope:
- # abstract()
- def info(self,msg):
- #abstract()
- print msg
-
- def error(self, msg):
- #abstract()
- print msg
CymbolErrorNode.py
- from antlr3 import RecognitionException
- from antlr3 import Token
- from antlr3 import TokenStream
- from antlr3.tree import CommonErrorNode
- class CymbolErrorNode (CymbolAST) :
-
- #CommonErrorNode delegate
- def __init__(self,input, start, stop, e):
- self.delegate = CommonErrorNode(input,start,stop,e)
-
- def isNil(self) :
- return self.delegate.isNil() # Indicates the node is a nil node but may still have children, meaning the tree is a flat list
- def getType(self) :
- return self.delegate.getType()
- def getText(self) :
- return self.delegate.getText()
-
- def toString(self) :
- return self.delegate.toString()
CymbolAST.py
- from antlr3 import RecognitionException
- from antlr3 import Token
- from antlr3 import TokenStream
- from antlr3.tree import CommonErrorNode
- class CymbolErrorNode (CymbolAST) :
-
- #CommonErrorNode delegate
- def __init__(self,input, start, stop, e):
- self.delegate = CommonErrorNode(input,start,stop,e)
-
- def isNil(self) :
- return self.delegate.isNil() # Indicates the node is a nil node but may still have children, meaning the tree is a flat list
- def getType(self) :
- return self.delegate.getType()
- def getText(self) :
- return self.delegate.getText()
-
- def toString(self) :
- return self.delegate.toString()
ClassSymbol.py
- from ScopedSymbol import ScopedSymbol
- from Scope import Scope
- from Type import Type
- class ClassSymbol ( ScopedSymbol, Scope, Type ):
- #/** This is the superclass not enclosingScope field. We still record
- # * the enclosing scope so we can push in and pop out of class defs.
- # */
-
- #ClassSymbol superClass;
-
- # /** List of all fields and methods */
- # public Map<String,Symbol> members =new LinkedHashMap<String,Symbol>();
-
-
- #public ClassSymbol(String name, Scope enclosingScope, ClassSymbol superClass) {
- # super(name, enclosingScope);
- # this.superClass = superClass;
- #}
- def __init__(self, name, enclosingScope, superClass) {
- self.members = []
- super(ClassSymbol,self).__init__(name, enclosingScope)
- self.superClass = superClass;
-
-
- def getParentScope(self) :
- if self.name == "Object" : return enclosingScope # globals ,liunote self.name at Symbol.py
- return superClass # if not Object, return super
- }
- # /** For a.b, only look in a's class hierarchy to resolve b, not globals */
- #public Symbol resolveMember(String name) {
- # Symbol s = members.get(name);
- # if ( s!=null ) return s;
- # // if not here, check any enclosing scope
- # if ( superClass != null ) {
- # return superClass.resolveMember(name);
- # }
- # return null; // not found
- #}
- def resolveMember(self, name) :
- s = get(name)
- if s != None : return s
- # if not here, check any enclosing scope
- if self.superClass != None :
- return self.superClass.resolveMember(name)
-
- return None # not found
-
-
- def get(self,name):
- for item in self.members:
- if type(item) != tuple:
- raise RuntimeError('orderedArgs item is not tuple
BuiltInTypeSymbol.py
- from Symbol import Symbol
- from Type import Type
- #public class BuiltInTypeSymbol extends Symbol implements Type {
- class BuiltInTypeSymbol (Symbol,Type): # liunote if def Interface type , but not implement it method ,then raise no implement error
-
- #int typeIndex;
-
- def __init__(self,name,typeIndex):
- print 'liutest super class Symbol init'
- Symbol.__init__(self,name)
- self.typeIndex = typeIndex
-
- def getTypeIndex(self):
- return self.typeIndex
-
- def toString(self):
- return self.getName() # liunote will call Symbol
BaseScope.py
- from Scope import Scope
- class BaseScope(Scope): # BaseScope is abstract class
- def __init__(self,parent):
- #self.symbols = {} # liunote dict dnot use as linked hash map
- self.symbols = [] # [(sym.name),(sym)] => ([x][0],[x],[1])
- self.enclosingScope = parent # Scope enclosingScope; null if global (outermost) scope
-
- def resolve(self,name):
- # s = self.symbols.get(name) # liunote
- s = self.get(name) # liunote :get(self,name) call instance method,not use self param
- if s != None: return s
- # if not here, check any enclosing scope
- if self.enclosingScope != None: return self.enclosingScope.resolve(name)
- return None # not found
- #public void define(Symbol sym) {
- def define(self,sym):
- #self.symbols.put(sym.name, sym) # liunote use tuple list
- self.symbols.append((sym.name, sym)) # no !!! liunote ??? name at Symbol.py , is parent class ,only derive method !!!
- # liunote java sym.name diff getName ,sym.name only is parent Symbol class menber
- sym.scope = self # track the scope in each symbol # liunote self will subclass instance's self !!!
- #public Scope getEnclosingScope() { return enclosingScope; }
- def getEnclosingScope(self):
- return self.enclosingScope
-
- #public String toString() { return symbols.keySet().toString(); }
- def toString(self):
- #return self.symbols.keySet().toString() # liunote keySet
- #str(self.symbols.items())
- #return str(self.symbols.keys()) # liunote
- return str([item[0] for item in self.symbols])
-
- def get(self,name):
- for item in self.symbols:
- if type(item) != tuple:
- raise RuntimeError('symbols item is not tuple
- from Symbol import Symbol
- from Type import Type
- class ArrayType(Symbol,Type) :
-
- def __init__(self, elementType):
- super(ArrayType,self).__init__("[]",elementType) #(elementType+"[]") liunote use (name,type) ,or (name) param ???
- self.elementType = elementType
- def getTypeIndex(self):
- return 0
-
- #def getElementType(self):
- # return self.elementType
-
- #elementType = property(getElementType)
test.py
- from antlr3 import *
- from antlr3.tree import *
- from CymbolLexer import CymbolLexer
- from CymbolParser import CymbolParser
- from Def import Def
- from Types import Types
- from SymbolTable import SymbolTable
- from CymbolAST import CymbolAST
- #class MyNode(CommonTree):
- # def __init__(self):
- # self.Symbol={}
-
- #class MyNodeAdaptor(CommonTreeAdaptor):
- # def create(payload):
- # return MyNode(payload)
- class CymbolAdaptor(CommonTreeAdaptor):
-
- #def create(self,token):
- # print ' liutest CymbolAdaptor creat CymbolAST!!!'
- # return CymbolAST(token)
-
- def createWithPayload(self, payload):
- #print ' liutest CymbolAdaptor creat CymbolAST!!!'
- return CymbolAST(payload)
-
- def dupNode(self,t) :
- if t == None :
- return None
- return self.create(t.token) # (CymbolAST)t
- def errorNode( self,input, start, stop, e):
- print 'liu:CymbolAdaptor errorNode'
- return CymbolErrorNode(input,start,stop,e)
- #ANTLRFileStream
- #CharStream input = null;
- filename = '/home/liu/promote/t.cymbol'
- input = ANTLRFileStream(filename);
- #input = antlr3.ANTLRInputStream(sys.stdin)
- lexer = CymbolLexer(input)
- #tokens = antlr3.CommonTokenStream(lexer)
- tokens = TokenRewriteStream(lexer)
- #parser = ExprParser(tokens)
- parser = CymbolParser(tokens)
- cymbolAdaptor = CymbolAdaptor()
- parser.setTreeAdaptor(cymbolAdaptor) # call to BaseTreeAdaptor
测试输入
t.symbol:
- int n;
- float a[];
- int d[];
- void f() {
- a[0] = 4*'i'; // promote char to int to float
- a[1] = d[0]; // promote int element to float
- a['x'] = 1; // check array index promotion
- g('q',10); // arg promotion
- }
- int g(int x, float y) { return 'k'; } // promote 'k' to int
u.cymbol:
- float a[];
- int d[];
- int c = 'z';
- void f() {
- a[3] = a[0]+4*'i'+d[0];
- boolean b = a[3+'a'] < 3.4;
- float f = a['x']+g('q',10);
- f = 3;
- int i;
- struct A { int x; };
- struct A s;
- int j = h(1)+4*a[i]+s.x;
- }
- int g(int x, float y) { return 'k'; }
- int h(int x) { return x; }
- float ff = g(3,3.4);
- float gg = d[0];
阅读(1909) | 评论(0) | 转发(0) |