示例#1
0
 def scan(self):
     global build_errors,cuadruplos,ids,temp_counter,counter,types,values,pOper,pilaO,braces,pSaltos
     #Lex construction
     import ply.lex as lex
     lex.lex()
     #Sintax construction
     import ply.yacc as yacc
     yacc.yacc()
     del build_errors[:]
     #Structure cleaning
     ids.dispatch()
     types.dispatch()
     values.dispatch()
     pOper.dispatch()
     pilaO.dispatch()
     braces.dispatch()
     pSaltos.dispatch()
     cuadruplos.clear()
     #Counters restart
     temp_counter = 0
     counter = 0
     #Compiling entry
     yacc.parse(self.entrada)
     #Return the build error's array or null in case there weren't any
     return build_errors
示例#2
0
 def __init__(self,loglevel=logging.CRITICAL,logfile=''):
     self._log=self.initLogger(loglevel,logfile)
     self._reserved_map={}
     for r in RuleParser.reserved:
         self._reserved_map[r.upper()] = r
     lex.lex(module=self)
     yacc.yacc(module=self)
示例#3
0
def chek(entrada):
	lexer.input(entrada)
	
	while True:
		t = lexer.token()
		if not t: break
	lex.lex()
    def __init__(self, filenameargument, **kw):

        logging.info("Parsing file \"" + str(filenameargument) + "\"")

        ## this block is magic from calc.py example I do not understand (Voit)
        self.debug = kw.get('debug', 0)
        self.names = { }
        try:
            modname = os.path.split(os.path.splitext(__file__)[0])[1] + "_" + self.__class__.__name__
        except:
            modname = "parser"+"_"+self.__class__.__name__
        self.debugfile = modname + ".dbg"
        self.tabmodule = modname + "_" + "parsetab"
        #print self.debugfile, self.tabmodule


        # Build the lexer and parser
        lex.lex(module=self, debug=0)
        yacc.yacc(module=self,
                  debug=self.debug,
                  debugfile=self.debugfile,
                  tabmodule=self.tabmodule)

        ## store in lexer object
        logging.debug("initializing seconds to -1")
        self._seconds = -1
        logging.debug("initializing numerrors to 0")
        self._numerrors = 0
        logging.debug("initializing filename to ["+str(filenameargument) + "]")
        self._filename = str(filenameargument)
        
        f=open(self._filename, 'r');
        self._totalLines = sum(1 for line in f)
示例#5
0
def parse_file_to_ast(file_path):

  if len(queue) > 0:
    current_dir = os.path.dirname(queue[-1])
  else:
    current_dir = ""

  full_path_string = os.path.abspath(os.path.join(current_dir, file_path))

  if full_path_string in parsed_set:
    return []

  parsed_set.add(full_path_string)
  queue.append(full_path_string)

  import ply.lex as lex
  import ply.yacc as yacc

  lex.lex(nowarn=1)
  yacc.yacc(debug=False, tabmodule="_preprocessor", outputdir=ODIR)

  reader = open(full_path_string, 'r')
  input_string = reader.read()
  reader.close()
  ast = yacc.parse(input_string)

  update_cleanup()
  return ast
示例#6
0
 def __init__(self, transmite_erros=False):
     """ Inicializador, pode receber True como segundo argumento permitindo o não tratamento de erros. """
     self.transmite_erros = transmite_erros  # Caso queira rodar por linha de comando os erros são repassados ao invés de tratados.
     self.resposta = None                    # Resposta 
     self.names = { }                        # Nomes de variáveis
     lex.lex(module=self)
     yacc.yacc(module=self)
示例#7
0
    def __init__(self, line, **kw):
        self.debug = kw.get('debug', 0)
        self.line = line
        self.searchtree = []
        self.numcases = 1

        try:
            modname = os.path.split(os.path.splitext(__file__)[0])[1] + "_" + self.__class__.__name__
        except:
            modname = "parser"+"_"+self.__class__.__name__
        self.debugfile = modname + ".dbg"
        self.tabmodule = modname + "_" + "parsetab"
        #print self.debugfile, self.tabmodule
        #self.debug = True

        # Build the lexer and parser
        lex.lex(module=self, debug=self.debug)
        yacc.yacc(module=self,
                  debug=self.debug,
                  debugfile=self.debugfile,
                  tabmodule=self.tabmodule)

        yacc.parse(self.line)

        for s in self.searchtree:
            if isinstance(s, SearchSubSpace):
                self.numcases *= s.size
示例#8
0
def processcsg(filename):
    global doc
    
    print 'ImportCSG Version 0.5d'
    # Build the lexer
    print 'Start Lex'
    lex.lex(module=tokrules)
    print 'End Lex'

    # Build the parser   
    print 'Load Parser'
    # No debug out otherwise Linux has protection exception
    parser = yacc.yacc(debug=0)
    print 'Parser Loaded'
    # Give the lexer some input
    #f=open('test.scad', 'r')
    f = pythonopen(filename, 'r')
    #lexer.input(f.read())

    print 'Start Parser'
    # Swap statements to enable Parser debugging
    #result = parser.parse(f.read(),debug=1)
    result = parser.parse(f.read())
    print 'End Parser'
    print result  
    FreeCAD.Console.PrintMessage('End processing CSG file')
    doc.recompute()
示例#9
0
def run_lex(text):
	lex.lex()
	lex.input(text)
	while True:
		token = lex.token()
		if not token: break
		print token
示例#10
0
    def __init__(self, **kw):
        self.debug = kw.get('debug', 0)
        self.filename = kw.get('filename', None)
        self.names = { }

        try:
            modname = os.path.split(os.path.splitext(__file__)[0])[1] + "_" + self.__class__.__name__
        except:
            modname = "parser"+"_"+self.__class__.__name__
        self.debugfile = modname + ".dbg"
        self.tabmodule = modname + "_" + "parsetab"
        self.outputdir = os.path.dirname(__file__)

        # Build the lexer and parser
        lex.lex(module=self, debug=self.debug)
        # We can't assume that we can write to this directory (e.g., we've
        # been installed as a module). So go ahead and skip writing out. Our
        # grammers are simple enough that this shouldn't be much of a problem,
        # we just regenerate the grammer each time.
        #yacc.yacc(module=self,
        #          debug=self.debug,
        #          debugfile=self.debugfile,
        #          tabmodule=self.tabmodule,
        #          outputdir=self.outputdir,
        #          method='SLR')
        yacc.yacc(module=self, debug=self.debug, write_tables=0, method='SLR')
示例#11
0
 def __compile(self, code):
     lex.lex()
     parser = yacc.yacc(start = 'statements')
     
     statements = parser.parse(code)
     #self.__dump(statements)
     return statements
示例#12
0
def processcsg(filename):
    global doc

    if printverbose: print ('ImportCSG Version 0.6a')
    # Build the lexer
    if printverbose: print('Start Lex')
    lex.lex(module=tokrules)
    if printverbose: print('End Lex')

    # Build the parser   
    if printverbose: print('Load Parser')
    # No debug out otherwise Linux has protection exception
    parser = yacc.yacc(debug=0)
    if printverbose: print('Parser Loaded')
    # Give the lexer some input
    #f=open('test.scad', 'r')
    f = pythonopen(filename, 'r')
    #lexer.input(f.read())

    if printverbose: print('Start Parser')
    # Swap statements to enable Parser debugging
    #result = parser.parse(f.read(),debug=1)
    result = parser.parse(f.read())
    f.close()
    if printverbose:
        print('End Parser')
        print(result)
    FreeCAD.Console.PrintMessage('End processing CSG file\n')
    doc.recompute()
示例#13
0
 def __init__(self, **kw):
     self.debug = kw.get('debug', 0)
     self.results = {}
     try:
         modname = os.path.split(os.path.splitext(__file__)[0])[1] + "_" + self.__class__.__name__
     except:
         modname = "parser"+"_"+self.__class__.__name__
     # self.debugfile = modname + ".dbg"
     self.tabmodule = modname + "_" + "parsetab"
     #print self.debugfile, self.tabmodule
     # Build the lexer and parser
     lex.lex(
             module=self,
             #debug=self.debug
             optimize=True,
             debug=False
             )
     yacc.yacc(
             module=self,
             debug=False,
             write_tables=False,
             #debug=self.debug,
             #debugfile=self.debugfile,
             tabmodule=self.tabmodule
             )   
示例#14
0
def parseFPGAEnvironment (environmentFile):
    # build the compiler
    lex.lex()
    yacc.yacc()
    environmentDescription = (open(environmentFile, 'r')).read()
    environment = yacc.parse(environmentDescription)
    return environment
示例#15
0
def main():
    lex.lex()

    cmd, program_file = check_args()

    if program_file is not None and cmd == 'test':
        parser = yacc.yacc()
        with open(program_file) as f:
            input = f.read()
        progcode = parser.parse(input)
        program = Program(progcode)
        program.run_tests()
    elif program_file is not None and cmd == 'lex':
        with open(program_file) as f:
            input = f.read()
        lex.input(input)
        while True:
            tok = lex.token()
            if not tok:
                break
            print tok
    elif program_file is not None:
        parser = yacc.yacc()
        with open(program_file) as f:
            input = f.read()
        progcode = parser.parse(input)
        program = Program(progcode)
        program.call_function('main', [5])
示例#16
0
def textEditorParseMe(filename):
    
    tokens = ['FILENAME', 'NUMBERSEQUENCE']
    
    def t_FILENAME(t):
        r'[a-zA-Z_/.][a-zA-Z0-9_/.]*'
        return t
    
    def t_NUMBERSEQUENCE(t):
        r'[0-9 :]+'
        return t
        
    t_ignore = '\t: '
    
    def t_newline(t):
        r'\n+'
        t.lexer.lineno += t.value.count("\n")
        
    def t_error(t):
        print "Illegal character '%s'" % t.value[0]
        t.lexer.skip(1)
        
    lex.lex()
    
    count = []
    latency = []
    organized = {}
    
    def p_sentence(p):
      '''sentence : FILENAME NUMBERSEQUENCE'''
      tmp1 = []
      tmp = p[2].split(':')
      for x in tmp:
        x = x.strip()
        tmp1.append(x)
      organized[int(tmp1[0])] = tmp1[1].split(' ')
          
          
        
        
        

    def p_error(p):
      if p:
          print("Syntax error at '%s'" % p.value)
      else:
          print("Syntax error at EOF")

        
    yacc.yacc()
    
    file = open(filename, 'r')
    while file:
        line = file.readline()
        if not line : break
        yacc.parse(line[0:-1])
        
        
    return organized
示例#17
0
文件: lexer.py 项目: coma94/tidjidiss
def do_lex(file):
    """."""
    with open(file, 'r+') as f:
        data = f.read()
        lex.lex()
        lex.input(data)
        for tok in iter(lex.token, None):
            print(repr(tok.type), repr(tok.value))
示例#18
0
def run_parser(file):
	lex.lex()
	f = open(file)
	yacc.yacc()
	input = f.read()
	# print "\033[41m" + input[94:134] + "\033[0m Ha ha"
	ast = yacc.parse(input, tracking=True)
	return (input, ast)
示例#19
0
def Parse(source, filename):
  lexer = Lexer(filename)
  parser = Parser(lexer, source, filename)

  lex.lex(object=lexer)
  yacc.yacc(module=parser, debug=0, write_tables=0)

  tree = yacc.parse(source)
  return tree
示例#20
0
def Parse(filename):
  lexer = Lexer()
  parser = Parser(lexer)

  lex.lex(object=lexer)
  yacc.yacc(module=parser, debug=0, write_tables=0)

  tree = yacc.parse(open(filename).read())
  return tree
示例#21
0
def initialise():
    '''Performs lazy initialisation of the lexer and parser. Once called, further calls are no-ops.'''
    global _tgrep_initialised
    
    if not _tgrep_initialised:
        lex.lex(module=parse)
        yacc.yacc(module=parse)
    
        _tgrep_initialised = True
示例#22
0
def main():
    init_logger()
    log = logging.getLogger() 
    lex.lex(debug=True, debuglog=log)
    args = get_args()
    if args.file:
        parse_file(args.file, lex)
    else:
        get_tokens_from_cin(lex)
  def __init__(self,data,parselog,parsefile,tokenfile,graphfile,tacfile,writecode):
    data = data.replace("\t","    ")
    # To enable a more verbose logger -- this is so we can see all of the production that were taken in the grammar.
    self.parselog = parselog
    self.graphfile = "log/" + graphfile
    self.tacfile = "log/" + tacfile
    self.writecode = writecode
    # set up the logger to log info... We added a flag for when not to output this info
    if self.parselog:
      logging.basicConfig(
        level = logging.INFO,
        filename = "log/" + parsefile,
        filemode = "w",
        format = "%(filename)10s:%(lineno)4d:%(message)s"
      )

      # We need to make some kind of error logger.

      log = self.log = logging.getLogger()


    
    if self.parselog:
      self.lexer = lex.lex(module=self,debuglog=log)#,debug=True)
    else:
      self.lexer = lex.lex(module=self)

    # Give the lexer some input
    # self.lexer.input(data)
    self.input_data = data



    if self.parselog:
      self.yacc = yacc.yacc(module=self,debuglog=log)#,debug=True)
    else:
      self.yacc = yacc.yacc(module=self)



    self.source = "" # this will keep track of what source we have seen so far
    self.tokens = ""  # this will keep track the tokens that we have seen so far
    self.reduction_list = [] # this will keep track of what tokens we have acquiredrm 
    self.typelist = []

    # our token log file
    self.log_tokens("log/" + tokenfile)

    # The symbol table
    self.symbol_table = SymbolTable()

    # Keeps track of the beginnings of lines this so we can make nice errors like fff< void main().
    self.lines = [0]
    for i in data.split('\n'):
      self.lines.append(self.lines[-1]+len(i)+1)
    self.rootnode = []
    self.supportedtypes = self.GetSupportedTypes()
示例#24
0
def compile(template):
    lex.lex()
    # lex.lexer.push_state('mu')
    lex.input(template)
    while 1:
        tok = lex.token()
        if not tok: break
        print tok
    yacc.yacc()
    return yacc.parse(template)
示例#25
0
 def __init__(self):
     global parts, rules, part_order, config
     parts = {}
     rules = []
     part_order = []
     config = Config()
     
     lex.lex()
     from celltone import celltone_home
     yacc.yacc(debug = 0, tabmodule = 'ply_parsetab', outputdir = celltone_home)
示例#26
0
文件: parser.py 项目: efrainra/blaze
def rebuild():
    """ Rebuild the parser and lexer tables. """
    path = os.path.relpath(__file__)
    output = os.path.dirname(path)
    module = sys.modules[__name__]

    lex.lex(module=module, lextab="dlex", outputdir=output, debug=0, optimize=1)
    yacc.yacc(tabmodule='dyacc',outputdir=output, write_tables=1, debug=0, optimize=1)

    sys.stdout.write("Parse and lexer tables rebuilt.\n")
示例#27
0
def do_lex():
    # Build the lexer
    lex.lex()
    return

    lex.input(sometext)
    while 1:
        tok = lex.token()
        if not tok: break
        print tok
示例#28
0
文件: parsehaskell.py 项目: bien/lang
def evalloop(input):
    import ply.lex as lex
    lex.lex()
    import ply.yacc as yacc
    yacc.yacc()
    for line in input:
        try:
            yield yacc.parse(line)
        except:
            traceback.print_exc()
            yield ''
示例#29
0
    def parse(cls, lines):
        """ this function-call is required to get the yacc running! """
        lex.lex(reflags=re.DOTALL)

        parser = yacc.yacc(start="top", debug=0, outputdir=str(QDir.homePath()) + "/.ricodebug")
        r = []
        for line in lines:
            line = line.strip()
            r.append(parser.parse(line))
            r[-1].raw = line

        return r
示例#30
0
文件: cat.py 项目: lalokuyo/Cat
def startCompilation():
	intext = open('input.txt', 'r+')
	input_file = intext.read()
	
	#Build the lexer
	lex.lex()
	#Build the parser
	yacc.yacc()

	#File to open 
	yacc.parse(input_file)
	intext.close()
示例#31
0
 def __init__(self, *args, **kwargs):
     unittest.TestCase.__init__(self, *args, **kwargs)
     # Clone all lexer instances from this one, since making a lexer is slow.
     self._zygote_lexer = lex.lex(mojom.parse.lexer.Lexer("my_file.mojom"))
示例#32
0
 def build(self, **kwargs):
     return lex.lex(module=self, **kwargs)
示例#33
0
    def test_lexer(self):
        lexer = lex.lex(module=olea.lexer)

        # contains test strings and their expected number of tokens
        tests = {
            '': 0,
            'blubber blub': 1,
            ' ~ISU-42': 1,
            ' ~42': 1,
            ' !4': 1,
            ' :Story': 1,
            ' #tag42': 1,
            ' #ta g42': 1,
            ' @user1234': 1,
            '>ISU-1337': 1,
            '>1337': 1,
            '>ISU-1337 @user !2 #1tag :Bug ~ISU-1': 6,
            'fancy text ~ISU-2 :Task': 3,
            'fancy te(x(t w)ith (Braces) ~ISU-2 :Task': 3,
            'another te(x(t w)ith (Braces( ~ISU-2 :Task': 3,
            'tes-_.?",/t-_.?" ~ISU-42': 2,
            '>ISU-42 +2d15h10m': 2,
            'blubber +': 2,
            '>ISSU-42 $10': 2,
            'Issue with ;fancy description': 2,
            '>ISSU-42 ;edit description': 2,
            'Issue with &Column': 2,
            'Bla ;(öaa)': 2,
        }

        for t in tests:
            lexer.input(t)

            count = 0
            for tok in lexer:
                count += 1

            self.assertEqual(count, tests[t])

        # negative tests
        tests = [
            ':Story',
            '#tag',
            '#tag#tag',
            '@user#tag',
            ' :Storyy',
            ' :Bugg',
            'fancy text@user',
            'test! ~ISU42',
            ' ~ISSUE-42',
            ' ~ISS42',
            '>Iss-1 +2m5h',
            '>ISS-1 $15b',
            '>ISS-1 ;',
            '>ISS-1 &',
        ]

        for t in tests:
            lexer.input(t)

            self.assertRaises(Exception, eval_lex_xpr, lexer)
示例#34
0
    #changed here
    print "Syntax Error: Near Token " + p.type
    print p.lineno
    pass
    # exit(-1)




import ply.lex as lex
# import ply.yacc as yacc
import NepLexer
# import NepParser
from NepLexer import tokens
# from NepParser import * 
lexer = lex.lex(module=NepLexer)
parser = yacc.yacc()




#write input text here to override it
#input = ""


ip = u'''क लेख
क, " मा हामीले  २ हालेका छौँ " लेख
"ख मा ", ख, " छ" लेख
'''
ip = u'''ख लेउ
'''
示例#35
0
    def __init__(self,
                 rng=None,
                 min_int=0,
                 max_int=19,
                 max_func_call=100,
                 debug=False,
                 **kwargs):

        self.names = {}
        self.debug = kwargs.get('debug', 0)

        # Build the lexer and parser
        modname = self.__class__.__name__

        self.lexer = lex.lex(module=self, debug=self.debug)

        self.yacc, self.grammar = yacc.yacc(module=self,
                                            debug=self.debug,
                                            tabmodule="_parsetab",
                                            with_grammar=True)

        self.prodnames = self.grammar.Prodnames

        #########
        # main
        #########

        self.debug = debug
        self.min_int = min_int
        self.max_int = max_int
        self.max_func_call = max_func_call
        self.int_range = list(range(min_int, max_int + 1))

        int_tokens = ['INT{}'.format(num) for num in self.int_range]
        self.tokens_details = list(set(self.tokens) -
                                   set(['INT'])) + int_tokens

        #self.idx_to_token = { idx: token for idx, token in enumerate(tokens) }
        #self.token_to_idx = { token:idx for idx, token in idx_to_token.items() }

        self.tokens_details.sort()
        self.tokens_details = ['END'] + self.tokens_details

        self.idx_to_token_details = {
            idx: token
            for idx, token in enumerate(self.tokens_details)
        }
        self.token_to_idx_details = {
            token: idx
            for idx, token in self.idx_to_token_details.items()
        }

        self.rng = get_rng(rng)
        self.flush_hit_info()
        self.call_counter = [0]

        def callout(f):
            def wrapped(*args, **kwargs):
                if self.call_counter[0] > self.max_func_call:
                    raise TimeoutError
                r = f(*args, **kwargs)
                self.call_counter[0] += 1
                return r

            return wrapped

        self.callout = callout
    lexer.input(input_string)
    result = []
    while True:
        tok = lexer.token()
        if not tok: break
        result = result + [tok.type, tok.value]
    return result


def test_parser(input_string):
    lexer.input(input_string)
    parse_tree = parser.parse(input_string, lexer=lexer)
    return parse_tree


lexer = lex.lex(module=js_lexer)
parser = yacc.yacc(module=js_parser)
'''
test_tokens = """ - !  && () * , / % ; { || } + < <= = == > >= else false function
if return true var """
print test_lexer(test_tokens)

comments = """
if // else mystery
=/*=*/=
true /* false
*/ return"""
print test_lexer(comments)

false_prophets = 'false_prophets "false_prophets true prophets"'
print test_lexer(false_prophets)
示例#37
0
    r'(/\*(.|\n)*?\*/|//.*)'
    t.lexer.lineno += t.value.count('\n')


def t_newline(t):
    r'\n+'
    t.lexer.lineno += len(t.value)


def t_error(t):
    t.value = "Illegal character '{}'".format(t.value[0])
    t.type = 'ERROR'
    return t


plylexer = lex.lex()


class Lexer:
    def __init__(self, inFile=None, inString=None):
        self.tokens = []

        if inFile == None and inString != None:
            plylexer.input(inString)
        elif inString == None and inFile != None:
            plylexer.input(inFile.read())
        else:
            raise ValueError("Requires either a file or string")

        while True:
            curToken = plylexer.token()
示例#38
0
文件: run.py 项目: fmeci/nfql-testing
    pass


# A string containing ignored characters (spaces and tabs)
t_ignore = ' \t'


# Error handling rule
def t_error(t):
    print("Illegal character '%s'" % t.value[0])
    #t.lexer.skip(1)


# Build the lexer

lexer = lex.lex(debug=0)

data = '''
filter ff { sourceIPv6Address=2001::1}
'''

lexer.input(data)
# Tokenize
while True:
    tok = lexer.token()
    if not tok: break  # No more input
    print(tok)
##parser
filters = []

示例#39
0
def parse_data_commands(data=None, filename=None, debug=0, outputdir=None):

    global dat_lexer
    global dat_yaccer

    if outputdir is None:
        # Try and write this into the module source...
        outputdir = os.path.dirname(getfile( currentframe() ))
        # Ideally, we would pollute a per-user configuration directory
        # first -- something like ~/.pyomo.
        if not os.access(outputdir, os.W_OK):
            outputdir = os.getcwd()

    # if the lexer/yaccer haven't been initialized, do so.
    if dat_lexer is None:
        #
        # Always remove the parser.out file, which is generated to
        # create debugging
        #
        if os.path.exists("parser.out"):        #pragma:nocover
            os.remove("parser.out")
        if debug > 0:                           #pragma:nocover
            #
            # Remove the parsetab.py* files.  These apparently need to
            # be removed to ensure the creation of a parser.out file.
            #
            if os.path.exists(tabmodule+".py"):
                os.remove(tabmodule+".py")
            if os.path.exists(tabmodule+".pyc"):
                os.remove(tabmodule+".pyc")

        dat_lexer = lex.lex()
        #
        tmpsyspath = sys.path
        sys.path.append(outputdir)
        dat_yaccer = yacc.yacc(debug=debug,
                               tabmodule=tabmodule,
                               outputdir=outputdir,
                               optimize=True)
        sys.path = tmpsyspath

    #
    # Initialize parse object
    #
    dat_lexer.linepos = []
    global _parse_info
    _parse_info = {}
    _parse_info[None] = []

    #
    # Parse the file
    #
    if filename is not None:
        if data is not None:
            raise ValueError("parse_data_commands: cannot specify both "
                             "data and filename arguments")
        with open(filename, 'r') as FILE:
            data = FILE.read()

    if data is None:
        return None

    dat_yaccer.parse(data, lexer=dat_lexer, debug=debug)
    return _parse_info
import sys

from constants import *
from statics import findn
import turtlebot_move_base_actions as turtlebot
import turtlebot_actions_2 as turtlebot2

import traceback

from orientation import Orientation
import time

from messages.msg import euler

lexer = lex.lex(module=lexerIG)
parser = yacc.yacc(module=parserIG)


class IGServer(object):
    _feedback = ig_action_msgs.msg.InstructionGraphFeedback()
    _result = ig_action_msgs.msg.InstructionGraphResult()
    _init_time = None
    _init_yaw = None
    _yaw_with_drift = None
    _yaw_with_drift_time = None
    _goal_canceled = False

    def __init__(self, name):
        self._name = name
        self._as = actionlib.SimpleActionServer(
示例#41
0
文件: spec.py 项目: hiddenvs/pythran
 def __init__(self):
     self.lexer = lex.lex(module=self, debug=False)
     # Do not write the table for better compatibility across ply version
     self.parser = yacc.yacc(module=self, debug=False, write_tables=False)
示例#42
0
def process(data):
    lex.lex()
    yacc.yacc()
    yacc.parse(data)
示例#43
0
def t_WORD(token):
    r'[^ <>\n]+'
    return token


webpage = "This is <b>my</b> webpage!"

page_newline_test = """ This is
 my webpage.
"""

page_comment_test = "hello <!-- comment --> all"

# this function call tells our lexical analysis library that we want to use
# all of the token definitions above to make a lexical analyzer and break up strings.
htmllexer = lex.lex()
htmllexer.input(webpage)

while True:
    tok = htmllexer.token()
    if tok is None:
        break
    else:
        print(tok)

f = open("index.html", "r")
webpage = f.read()

htmllexer.input(webpage)
print("\nTokens of html file index.html")
while True:
def t_FLOAT(t):
    r"""([0-9]*\.[0-9]+ | [0-9]+\.[0-9]*)"""
    return t


def t_INT(t):
    r"""([0-9]+)"""
    return t


def t_ID(t):
    r"""[a-zA-Z][a-zA-Z0-9]*"""
    return t


def t_newline(t):
    r"""\n+"""
    t.lexer.lineno += len(t.value)

t_ignore = ' \t'


def t_error(t):
    print("\033[91mWTF is this?! (mathematicaLex.py:64)")
    print(t)
    print("Ignoring it...\033[0m")
    t.lexer.skip(1)

mathematicaLexer = lex.lex()
示例#45
0
 def __init__(self):
     self.lexer = lex.lex(debug=False, module=self, optimize=False)
示例#46
0
def t_COMMENT(t):
    r"//.*"
    return t


def t_CO(t):
    r":"
    return t


def t_error(t):
    print("TOKEN ERROR: " + str(t))


lex.lex()

#----------------------------------------------------------

commands = []
symbols = {}


def p_input(p):
    """input :
            | command input"""
    pass


def p_command_comment(p):
    'command : COMMENT'
示例#47
0
# type="text/javascript"> somewhere inside that HTML.
#
# Beyond that, you will receive full credit for whatever you submit. Our
# web browser is not as "forgiving" as commercial browsers, but it is still
# possible to make interesting webpages with it. Use your creativity!

webpage = """<html>
Put something here. You must include embedded JavaScript for full credit,
but beyond that, do whatever you like.
</html>
"""

import ply.lex as lex
import ply.yacc as yacc
from Web import htmltokens
from Web import htmlgrammar
from Web import htmlinterp
from Web import graphics as graphics
from Web import jstokens

htmllexer = lex.lex(module=htmltokens)
htmlparser = yacc.yacc(module=htmlgrammar, tabmodule="parsetabhtml")
ast = htmlparser.parse(webpage, lexer=htmllexer)
jslexer = lex.lex(module=jstokens)
graphics.initialize()  # Enables display of output.
htmlinterp.interpret(ast)
graphics.finalize()  # Enables display of output.

# We would also like to take this opportunity to thank everyone in Udacity for
# their support in making this class happen, especially those behind the scenes.
# Check out team.jpeg for a group photo!
示例#48
0
 def build(self, **kwargs):
     self.lexer = lex.lex(module=self, **kwargs)
示例#49
0
            escaped = 0
        else:
            if c == "\\":
                escaped = 1
            else:
                new_str += c
    t.value = new_str
    return t


# Read in a symbol. This rule must be practically last since there are so
# few rules concerning what constitutes a symbol
def t_SYMBOL(t):
    r'[a-zA-Z_][a-zA-Z0-9_]*'
    # See if symbol is a reserved keyword
    t.type = reserved.get(t.value, 'SYMBOL')
    return t


t_newline = token_newline

t_ignore = ' \t'

t_error = token_error

# Build the lexer
lex.lex(reflags=RE_UNICODE, errorlog=lex.NullLogger())

if __name__ == '__main__':
    lex.runmain()
示例#50
0
 def __init__(self):
     super(SimpleMichelsonLexer, self).__init__()
     self.lexer = lex(module=self, reflags=re.MULTILINE)
示例#51
0
def new():
    """
    Entry point for lexer.  Defines tokens as varialbes or functions of
    same name.
    """
    t_AND = r"\&"
    t_ANDAND = r"\&\&"
    t_ANDEQ = r"\&="
    t_BACKSLASH = r"\\"
    t_COLON = r":"
    t_DIV = r"\/"
    t_DIVEQ = r"\/="
    t_DOT = r"\."
    t_DOTDIV = r"\./"
    t_DOTDIVEQ = r"\./="
    t_DOTEXP = r"\.\^"
    t_DOTMUL = r"\.\*"
    t_DOTMULEQ = r"\.\*="
    t_EQ = r"="
    t_EQEQ = r"=="
    t_EXP = r"\^"
    t_EXPEQ = r"\^="
    t_GE = r">="
    t_GT = r"\>"
    t_HANDLE = r"\@"
    t_LE = r"<="
    t_LT = r"\<"
    t_MINUS = r"\-"
    t_MINUSEQ = r"\-="
    t_MINUSMINUS = r"\--"
    t_MUL = r"\*"
    t_POW = r"\*\*"
    t_MULEQ = r"\*="
    t_NE = r"(~=)|(!=)"
    t_NEG = r"\~|\!"
    t_OR = r"\|"
    t_OREQ = r"\|="
    t_OROR = r"\|\|"
    t_PLUS = r"\+"
    t_PLUSEQ = r"\+="
    t_PLUSPLUS = r"\+\+"

    states = (("matrix", "inclusive"), ("afterkeyword", "exclusive"))

    states = (("matrix", "inclusive"), ("afterkeyword", "exclusive"))
    # white space, a space or "... plus anything"
    ws = r"(\s|\.\.\..*\n|\\\n)"
    #ws  = r"(\s|(\#|(%[^!])).*\n|\.\.\..*\n|\\\n)"
    # match one or more white spaces
    ws1 = ws + "+"
    # match 0 or more white spaces
    ws0 = ws + "*"
    # match string?
    ms = r"'([^']|(''))*'"
    os = r'"([^"\a\b\f\r\t\0\v\n\\]|(\\[abfn0vtr\"\n\\])|(""))*"'
    # mos is any kind of matlab string, I think.
    mos = "(%s)|(%s)" % (os, ms)
    # variable name
    id = r"[a-zA-Z_][a-zA-Z_0-9]*"

    def unescape(s):
        if s[0] == "'":
            return s[1:-1].replace("''", "'")
        else:
            return s[1:-1].decode("string_escape")

    @TOKEN(mos)
    def t_afterkeyword_STRING(t):
        t.value = unescape(t.value)
        t.lexer.begin("INITIAL")
        return t

    def t_afterkeyword_error(t):
        t_error(t)

    # A quote, immediately following any of: (1) an alphanumeric
    # charater, (2) right bracket, parenthesis or brace,
    # or (3) another TRANSPOSE, is a TRANSPOSE.  Otherwise, it starts a
    # string.  The order of the rules for TRANSPOSE (first) and STRING
    # (second) is important.  Luckily, if the quote is separated from
    # the term by line continuation (...), matlab starts a string, so
    # the above rule still holds.

    def t_TRANSPOSE(t):
        r"(?<=\w|\]|\)|\})((\.')|')+"
        # <---context ---><-quotes->
        # We let the parser figure out what that mix of quotes and
        # dot-quotes, which is kept in t.value, really means.
        return t

    @TOKEN(mos)
    def t_STRING(t):
        t.value = unescape(t.value)
        return t

    @TOKEN(r"(\.%s)?%s" % (ws0, id))
    def t_IDENT(t):
        if t.value == "parfor":
            t.value = "for"
        if t.value == "classdef":
            raise_exception(SyntaxError, "Not implemented: %s" % t.value,
                            t.lexer)
        t.lexer.lineno += t.value.count("\n")
        if t.value[0] == ".":
            # Reserved words are not reserved
            # when used as fields.  So return=1
            # is illegal, but foo.return=1 is fine.
            t.type = "FIELD"
            return t
        if (t.value == "end" and (t.lexer.parens > 0 or t.lexer.brackets > 0
                                  or t.lexer.braces > 0)):
            t.type = "END_EXPR"
            return t
        if t.value in ("end", "endif", "endfunction", "endwhile", "endfor",
                       "endswitch", "end_try_catch"):
            keyword = t.lexer.stack.pop()  # if,while,etc.
            #assert keyword == t.value or keyword == "try"
            if keyword == "function":
                t.type = "END_FUNCTION"
            else:
                t.type = "END_STMT"
            return t
        else:
            t.type = reserved.get(t.value, "IDENT")
            if t.value in ("if", "function", "while", "for", "switch", "try"):
                # lexer stack may contain only these
                # six words, ever, because there is
                # one place to push -- here
                t.lexer.stack.append(t.value)
            if (t.type != "IDENT" and t.lexer.lexdata[t.lexer.lexpos] == "'"):
                t.lexer.begin("afterkeyword")
        return t

    def t_LPAREN(t):
        r"\("
        t.lexer.parens += 1
        return t

    def t_RPAREN(t):
        r"\)"
        t.lexer.parens -= 1
        return t

    @TOKEN(ws0 + r"\]")
    def t_RBRACKET(t):  # compare w t_LBRACKET
        t.lexer.lineno += t.value.count("\n")
        t.lexer.brackets -= 1
        if t.lexer.brackets + t.lexer.braces == 0:
            t.lexer.begin("INITIAL")
        return t

    @TOKEN(r"\[" + ws0)
    def t_LBRACKET(t):  # compare w t_SEMI
        t.lexer.lineno += t.value.count("\n")
        t.lexer.brackets += 1
        if t.lexer.brackets + t.lexer.braces == 1:
            t.lexer.begin("matrix")
        return t

    # maybe we need a dedicated CELLARRAY state ???
    @TOKEN(ws0 + r"\}")
    def t_RBRACE(t):
        t.lexer.lineno += t.value.count("\n")
        t.lexer.braces -= 1
        if t.lexer.braces + t.lexer.brackets == 0:
            t.lexer.begin("INITIAL")
        return t

    @TOKEN(r"\{" + ws0)
    def t_LBRACE(t):
        t.lexer.lineno += t.value.count("\n")
        t.lexer.braces += 1
        if t.lexer.brackets + t.lexer.braces == 1:
            t.lexer.begin("matrix")
        return t

    @TOKEN(r"," + ws0)
    def t_COMMA(t):  # eating spaces is important inside brackets
        t.lexer.lineno += t.value.count("\n")
        if (t.lexer.brackets == 0 and t.lexer.parens == 0
                and t.lexer.braces == 0):
            t.type = "SEMI"
            return t
        return t

    @TOKEN(r"\;" + ws0)
    def t_SEMI(t):
        t.lexer.lineno += t.value.count("\n")
        #        if t.lexer.brackets or t.lexer.braces > 0:
        #            t.type = "CONCAT"
        return t

    def t_NUMBER(t):
        r"(0x[0-9A-Fa-f]+)|((\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?[ij]?)"
        #  <-------------> <------------------><------------->
        #   int,oct,hex        float               exp
        if t.value[-1] == 'i':
            t.value = t.value[:-1] + 'j'
        t.value = eval(t.value)
        return t

    def t_NEWLINE(t):
        r'\n+'
        t.lexer.lineno += len(t.value)
        if not t.lexer.parens and not t.lexer.braces:
            t.value = ";"
            t.type = "SEMI"
            return t

    def t_ERROR_STMT(t):
        r"%!(error|warning|test).*\n"
        t.lexer.lineno += 1

    # keep multiline comments
    def t_COMMENT(t):
        r"(^[ \t]*[%#][^!\n].*\n)+"
        t.lexer.lineno += t.value.count("\n")
        if not options.no_comments:
            t.type = "COMMENT"
            return t

    # drop end-of-line comments
    def t_comment(t):
        r"(%|\#)!?"
        if not options.testing_mode or t.value[-1] != "!":
            t.lexer.lexpos = t.lexer.lexdata.find("\n", t.lexer.lexpos)

    @TOKEN(r"(?<=\w)" + ws1 + r"(?=\()")
    def t_matrix_BAR(t):
        # Consume whitespace which follows end of name
        # and is followed a left paren.  This properly handles
        # a space between a func name and the arguments
        pass

    tend = r"(?<=[])}'\".]|\w)"
    tbeg = r"(?=[-+]?([[({'\"]|\w|\.\d))"

    @TOKEN(tend + ws1 + tbeg)
    def t_matrix_FOO(t):
        # In matrix state, consume whitespace separating two
        # terms and return a fake COMMA token.  This allows
        # parsing [1 2 3] as if it was [1,2,3].  Handle
        # with care: [x + y] vs [x +y]
        #
        # A term T is
        # (a) a name or a number
        # (b) literal string using single or doble quote
        # (c) (T) or [T] or {T} or T' or +T or -T
        #
        # Terms end with
        # (1) an alphanumeric charater \w
        # (2) single quote (in octave also double-quote)
        # (3) right parenthesis, bracket, or brace
        # (4) a dot (after a number, such as 3.
        #
        # The pattern for whitespace accounts for ellipsis as a
        # whitespace, and for the trailing junk.
        #
        # Terms start with
        # (1) an alphanumeric character
        # (2) a single or double quote,
        # (3) left paren, bracket, or brace and finally
        # (4) a dot before a digit, such as .3  .

        # TODO: what about curly brackets ???
        # TODO: what about dot followed by a letter, as in field
        #   [foo  .bar]

        t.lexer.lineno += t.value.count("\n")
        t.type = "COMMA"
        return t

    def t_ELLIPSIS(t):
        r"\.\.\..*\n"
        t.lexer.lineno += 1
        pass

    def t_SPACES(t):
        r"(\\\n|[ \t\r])+"
        pass

    def t_error(t):
        raise_exception(SyntaxError, ('Unexpected "%s" (lexer)' % t.value),
                        t.lexer)

    lexer = lex.lex(reflags=re.MULTILINE)
    lexer.brackets = 0  # count open square brackets
    lexer.parens = 0  # count open parentheses
    lexer.braces = 0  # count open curly braces
    lexer.stack = []
    return lexer
示例#52
0
文件: utest.py 项目: zhanglei/esql5
def exec_query(stmt):

    my_lexer=lex(module=lexer,optimize=True,debug=True)
       
    my_parser=yacc(debug=True,module=parser)
    
    val = my_parser.parse(lexer=my_lexer.clone(),debug=False,input=sql)

    es = Elasticsearch([{'host':"10.68.23.81","port":9201}])
    
    
    val.debug()
    
    if val.get_type() == TK.TOK_QUERY:
        
        query = Query(val)
        
        print(query.dsl())
        
        print(query._index,query._type)
        
        res = es.search(index=query._index, doc_type = query._type, body=query.dsl(), request_timeout=100)
      
        stmt_res = response_hits(res)
      
        print(json.dumps(stmt_res,indent=4))
        
    elif val.get_type() == TK.TOK_CREATE_TABLE:
        
        stmt = Create(val)
        
        res = es.indices.create(index=stmt._index,body = stmt._options,request_timeout=100,ignore= 400)
    
        res = es.indices.put_mapping(index = stmt._index, doc_type = stmt._type, body = stmt.dsl(), request_timeout=100)
    
        print(json.dumps(res,indent=4))
        
    elif val.get_type() == TK.TOK_INSERT_INTO:
        
#         val.debug()
        
        stmt = Insert(val)
        
        parms = stmt.metas
        
        res = es.index(index = stmt._index,doc_type =  stmt._type, body = stmt.dsl(),**parms)
        
        print(json.dumps(res,indent=4))
        
    elif val.get_type() == TK.TOK_BULK_INTO:
        
#         val.debug()
        
        
        stmt = Bulk(val)
        
        res = es.bulk(index = stmt._index,doc_type = stmt._type, body = stmt.dsl())
        
        print(json.dumps(res,indent=4))
        
    
    elif val.get_type() == TK.TOK_UPDATE:
        
        val.debug()
        
        stmt = Update(val)
        
        print(json.dumps(stmt.dsl(),indent=4))
        
        res = es.update(index = stmt._index, doc_type = stmt._type, body = stmt.dsl(), **stmt.conditions)
        
        
        print(json.dumps(res,indent=4))
    
    
    elif val.get_type() == TK.TOK_UPSERT_INTO:
        
        val.debug()
        
        stmt = Upsert(val)
        
        print(json.dumps(stmt.dsl(),indent=4))
        
        res = es.update(index = stmt._index, doc_type = stmt._type, body = stmt.dsl(), **stmt.conditions)
        
        
        print(json.dumps(res,indent=4))
    
    
    elif val.get_type() == TK.TOK_DELETE:
        
        val.debug()
        
        stmt = Delete(val)
        
        res = es.delete(index = stmt._index, doc_type = stmt._type, **stmt.conditions,ignore= 404)

        print(json.dumps(res,indent=4))
        
        
    elif val.get_type() == TK.TOK_EXPLAIN:
        stmt = Explain(val)
        print(stmt.curl_str)
        print(json.dumps(stmt.dsl(),indent=4))
    
    elif val.get_type() == TK.TOK_DESC_TABLE:
        
        stmt = Describe(val)
        
        
        res = es.indices.get_mapping(index = stmt._index,doc_type=stmt._type)
        
        print(res)
        
        
    else:
        res = es.cat.indices(index = 'qs_test*', v=True)
        val.debug()
        print(res)
示例#53
0
def pyimportgenerate(hfile, path):

    # LEX AND PARSE THE FILE
    filename = hfile
    if not os.path.isfile(path + hfile):
        script_path = re.match(r'(?P<path>.*)/[^/]+', \
                                   os.path.realpath(__file__)).group('path')

        if os.path.isfile(script_path + "/cspy_headers/" + hfile):
            # found header in cspy_headers folder
            try:
                hfile = open(script_path + "/cspy_headers/" + hfile,
                             'rU').read() + "\n"
            except:
                print >> sys.stderr, "Header file '" + script_path + \
                    "/cspy_headers/" + hfile + "' could not be read."
                exit()
        else:
            print >> sys.stderr, "Header file '" + path + hfile + \
                "' could not be found."
            exit()
    else:
        try:
            hfile = open(path + hfile, 'rU').read() + "\n"
        except:
            print >> sys.stderr, "Header file '" + path + hfile + \
                "' could not be read."
            exit()

    lexer = lex.lex()
    lexer.indentwith = None
    lexer.indentedline = None
    lexer.indentstack = [0]
    lexer.input(hfile)

    parser = yacc.yacc(tabmodule="header_parsetab")
    lexer.parser = parser
    parsetree = parser.parse(tracking=True)

    if parsetree:
        parsetree.set_column_num(hfile)
    else:
        remove_files()
        exit(1)

    # GET IMPORTED FILES
    imports = parsetree.flatten("IMPORTBLOCK_SINGLE")

    name = filename[:len(filename) - 5]

    if len(filename) <= 5:
        print >> sys.stderr, "Compiler error in cspy_pyimport.py:\nTried " + \
            "to import a non-cspyh file."
        exit(1)

    pyfilename = filename[:-6] + ".py"

    File = None

    # NO FILES IMPORTED
    if not imports:

        # PROCESS SELF
        generate_environments(parsetree)  # add environments
        det_type(parsetree)  # determine any remaining types

        return parsetree

    # FILES IMPORTED
    for statement in imports:

        # PROCESS IMPORTS
        statement = statement[0]
        importtype = statement.label

        # imports must be python files with cspyh headers
        importfile = statement[0] + ".cspyh"
        importtree = pyimportgenerate(importfile, path)

        # ADD IMPORTED CODE TO PARSE TREE

        # pyimport simple : pyimport module
        if importtype == "PYIMPORT_SIMPLE":
            importmodule = type_obj("import module",
                                    builtins["ImportModule"],
                                    methods=importtree.env)
            parsetree.initiate_var(statement[0], importmodule)

        # pyimport alias : pyimport module as id
        elif importtype == "PYIMPORT_ALIAS":
            alias = statement[1]
            importmodule = type_obj("import module",
                                    builtins["ImportModule"],
                                    methods=importtree.env)
            parsetree.initiate_var(alias, importmodule)

        # pyimport bulk : from module pyimport *
        elif importtype == "PYIMPORT_BULK":
            for variable in importtree.env:
                parsetree.initate_var(variable, importtree.env[variable])

        # pyimport discrete : from module pyimport id1, id2, ...
        elif importtype == "PYIMPORT_DISCRETE":
            importids = statement.flatten("IMPORTLIST_SIMPLE")
            for identifier in importids:
                name = identifier[0]
                value = importtree.lookup(name)
                parsetree.initiate_var(name, value)

        else:
            # Should never reach this point
            print >> sys.stderr, "\nCompiler error with imports in " + \
                "cspy_pyimport.py.\nNo import type matched"

    # PROCESS SELF
    generate_environments(parsetree)  # add environments
    det_type(parsetree)  # determine any remaining types
    return parsetree
示例#54
0
def to_pascal_case(val):
    words = val.split('.')
    new_words = ""
    for word_count in range(len(words)):
        if(len(words[word_count])>0):
            words[word_count]=words[word_count][0].upper()+words[word_count][1:]
            if(word_count!=0):
                new_words+="."+words[word_count]
            else:
                new_words = words[word_count]
    return new_words


def t_ID(t):
    r'[a-zA-Z_][(.)?a-zA-Z_0-9]*'
    if ("." in t.value):
        t.type = "PACKAGE_ID"
        t.value = to_pascal_case(t.value)
    else:
        t.type = reserved.get(t.value, 'ID')  # Check for reserved words
    return t


def t_error(t):
    t.lexer.skip(1)
    t.lineno = t.lexer.lineno
    return t


lexer.lex()
示例#55
0
def myparse(filepath):
    # associate to string their expression
    dic = {}
    # associate to string their type (or default when variable are not yet define) !! float are treated as int
    type = defaultdict(lambda: "default")

    # The pmc we are building
    pmc = PmcModules()

    # The module that is currently built
    curentMod = None

    # ##############################@ LEXER #########################@
    tokens = (
        'DOUBLE',
        'FLOAT',
        # basic operators
        'PLUS',
        'MINUS',
        'MULT',
        'DIV',
        'EQUAL',
        'AND',
        'NOT',
        'OR',
        'LEQ',
        'GEQ',
        'GS',
        'LS',
        # type of the parsed file
        'DTMC',
        'CTMC',
        'MDP',
        # keywords
        'MODULE',
        'ENDMODULE',
        'REWARDS',
        'ENDREWARDS',
        'INIT',
        'ENDINIT',
        'PARAM',
        'CONST',
        'LABEL',
        'GLOBALL',
        'FORMULA',
        # string for variable names
        'NAME',
        # special char
        'DDOT',
        'LCROCHET',
        'RCROCHET',
        'POINTPOINT',
        'LPAR',
        'RPAR',
        'FLECHE',
        'NEW',
        'SC',
        'VIRGULE',
        'QUOTE',
        'LACCO',
        'RACCO',
        # types
        'INT',
        'TYPEFLOAT',
        'BOOL',
        # boolean
        'TRUE',
        'FALSE',
    )

    def t_ccode_comment(t):
        r'//.*(\n|\r\n)'
        pass

    def t_POINTPOINT(t):
        r'\.\.'
        return t

    def t_FLECHE(t):
        r'->'
        return t

    def t_LEQ(t):
        r'<='
        return t

    def t_GEQ(t):
        r'>='
        return t

    def t_GS(t):
        r'>'
        return t

    def t_LS(t):
        r'<'
        return t

    def t_NOT(t):
        r'!'
        return t

    def t_FLOAT(t):
        r"""(\d+(\.\d+)?)([eE][-+]? \d+)?"""
        return t

    def t_MDP(t):
        r"(mdp)|(nondeterministic)"
        return t

    def t_CTMC(t):
        r"(ctmc)|(stochastic)"
        return t

    def t_DTMC(t):
        r"(probabilistic)|(dtmc)"
        return t

    def t_PARAM(t):
        r"param"
        return t

    def t_CONST(t):
        r"const"
        return t

    def t_FORMULA(t):
        r"formula"
        return t

    def t_INT(t):
        r"int"
        return t

    def t_BOOL(t):
        r"bool"
        return t

    def t_TYPEFLOAT(t):
        r"float"
        return t

    def t_DOUBLE(t):
        r"double"
        return t

    def t_GLOBALL(t):
        r"global"
        return t

    def t_INIT(t):
        r"init"
        return t

    def t_ENDINIT(t):
        r"endinit"
        return t

    def t_MODULE(t):
        r"module"
        return t

    def t_ENDMODULE(t):
        r"endmodule"
        return t

    def t_REWARDS(t):
        r"rewards"
        return t

    def t_ENDREWARDS(t):
        r"endrewards"
        return t

    def t_TRUE(t):
        r"true"
        return t

    def t_FALSE(t):
        r"false"
        return t

    def t_LABEL(t):
        r'label'
        return t

    t_PLUS = r'\+'
    t_MINUS = r'-'
    t_DIV = r'/'
    t_MULT = r'\*'
    t_NAME = r'[a-zA-Z_][a-zA-Z_0-9]*'
    t_EQUAL = r'='
    t_DDOT = r':'
    t_LCROCHET = r'\['
    t_RCROCHET = r'\]'
    t_LACCO = r'\{'
    t_RACCO = r'\}'
    t_LPAR = r'\('
    t_RPAR = r'\)'
    t_NEW = r'\''
    t_SC = r';'
    t_AND = r'&'
    t_VIRGULE = r','
    t_QUOTE = r'\"'
    t_OR = r'\|'

    # ignore space tab and new line
    t_ignore = ' \r\n\t'

    # error handeling in lexer
    def t_error(t):
        print("Illegal character '%s'" % t.value[0])
        t.lexer.skip(1)
        raise Exception("Illegal char ", t)

    lexer = lex.lex()

    # ###################### PARSER ############################

    # PRECEDENCE of opreators
    precedence = (
        ('left', 'OR'),
        ('left', 'AND'),
        ('left', 'EQUAL'),
        ('nonassoc', 'GEQ', 'LEQ', 'LS', 'GS'),
        ('left', 'PLUS', 'MINUS'),
        ('left', 'MULT', 'DIV'),
        ('right', 'NOT'),
    )

    # STARTING rule
    start = 'def'

    # empty for list
    def p_empty(p):
        'empty :'

    def p_begining(p):
        'def : mdptype unfold'
        # print(1)

    def p_unfold(p):
        '''unfold : declParamList unfold
                | declConstList unfold
                | declGlobalList unfold
                | moduleList unfold
                | labelList unfold
                | rewards unfold
                | initdef unfold
                | formulas unfold
                | empty'''

    def p_formulas(p):
        '''formulas : formula SC formulas
                | empty'''

    def p_formula(p):
        'formula : FORMULA NAME EQUAL funexp'
        t, e = p[4]
        dic[p[2]] = rea(e, dic)

    # type : MDP
    def p_mdptype(p):
        '''mdptype : MDP
                | CTMC
                | DTMC'''
        if p[1] not in ("dtmc", "probabilistic"):
            print(p[1])
            print(" WARNING !! only probabilistic model are supported yet")

    # list of PARAMETERS separted by a semicolon
    def p_declParamList(p):
        '''declParamList : declParam SC declParamList
            | declParam SC'''

    def p_declParaml(p):
        '''declParam : PARAM type NAME DDOT LCROCHET funexp POINTPOINT funexp RCROCHET
                    | PARAM type NAME'''
        dic[p[3]] = rea(p[3], dic)
        type[p[3]] = "int"
        pmc.add_parameter(dic[p[3]])

    def p_declParamMultiple(p):
        'declParam : PARAM type NAME LACCO funexp POINTPOINT funexp RACCO'
        global paramnameglob
        paramnameglob = p[3]
        dic[p[3]] = my_func
        t1, e1 = p[5]
        t2, e2 = p[7]
        for i in range(rea(e1, dic), rea(e2, dic) + 1):
            pmc.add_parameter(Symbol(p[3] + str(i)))

    def p_type(p):
        '''type : empty
                | INT
                | TYPEFLOAT
                | BOOL
                | DOUBLE'''
        if p[1] == "bool":
            p[0] = "bool"
        else:
            p[0] = "int"

    # list of CONSTANTS separated by a semicolon
    def p_declConstListl(p):
        '''declConstList : declConst SC declConstList
            | declConst SC'''

    def p_declConstl(p):
        'declConst : CONST type NAME EQUAL funexp'
        t, e = p[5]
        if t == p[2]:
            dic[p[3]] = rea(e, dic)
            type[p[3]] = p[2]
        else:
            raise Exception("invalid type cons decl : " + p[3] + " " + t +
                            " " + p[2])

    # list of GLOBAL VARIABLES separated by a semicolon
    def p_globallList(p):
        '''declGlobalList : declGlobal SC declGlobalList
                          | declGlobal SC'''

    def p_globall(p):
        '''declGlobal : GLOBALL NAME DDOT LCROCHET funexp POINTPOINT funexp RCROCHET
            | GLOBALL NAME DDOT LCROCHET funexp POINTPOINT funexp RCROCHET INIT funexp
            | GLOBALL NAME DDOT BOOL'''
        dic[p[2]] = rea(p[2], dic)
        if len(p) > 10:
            type[p[2]] = "int"
            t1, e1 = p[5]
            t2, e2 = p[7]
            pmc.add_global_variable(dic[p[2]], rea(e1, dic), rea(e2, dic))
        elif len(p) > 6:
            t1, e1 = p[5]
            t2, e2 = p[7]
            t3, e3 = p[10]
            type[p[2]] = "int"
            pmc.add_global_variable(dic[p[2]], rea(e1, dic), rea(e2, dic),
                                    rea(e3, dic))
        else:
            type[p[2]] = "bool"
            pmc.add_global_variable(dic[p[2]], rea("true", dic),
                                    rea("false", dic))

    # list of MODULES
    def p_moduleList(p):
        '''moduleList : module moduleList
            | module'''

    # For a module either
    # 1 define a new module
    # 2 define a module as renaming a previous one
    def p_module(p):
        '''module : modName stateList transList endmodule
            | reModName  LCROCHET listIdState RCROCHET endmodule'''

    def p_newMod(p):
        'modName : MODULE NAME'
        nonlocal curentMod
        curentMod = Module(p[2])

    def p_renewmod(p):
        'reModName : MODULE NAME EQUAL NAME'
        nonlocal curentMod
        mod = pmc.get_module(p[4])
        curentMod = mod.copy(p[2])

    # renaming a module
    def p_listIdState(p):
        '''listIdState : NAME EQUAL NAME
            | NAME EQUAL NAME VIRGULE listIdState'''
        dic[p[3]] = rea(p[3], dic)
        type[p[3]] = type[p[1]]
        try:
            curentMod.replace(dic[p[1]], dic[p[3]])
        except:
            curentMod.replace(p[1], p[3])

    # when finished add the created module to pmc
    def p_endmodule(p):
        'endmodule : ENDMODULE'
        nonlocal curentMod
        pmc.add_module(curentMod)
        curentMod = None

    # list of declarition of states
    def p_stateList(p):
        '''stateList : stateDecl SC stateList
            | empty'''

    # state declaration with our without initial value
    def p_statedecl(p):
        '''stateDecl : NAME DDOT LCROCHET funexp POINTPOINT funexp RCROCHET
            | NAME DDOT LCROCHET funexp POINTPOINT funexp RCROCHET INIT funexp
            | NAME DDOT BOOL'''
        dic[p[1]] = rea(p[1], dic)
        if len(p) > 8:
            _, e1 = p[4]
            _, e2 = p[5]
            _, e3 = p[9]
            curentMod.add_state(dic[p[1]], rea(e1, dic), rea(e2, dic),
                                rea(e3, dic))
            type[p[1]] = "int"
        elif len(p) > 5:
            type[p[1]] = "int"
            _, e1 = p[4]
            _, e2 = p[6]
            curentMod.add_state(dic[p[1]], rea(e1, dic), rea(e2, dic))
        else:
            type[p[1]] = "bool"
            curentMod.add_state(dic[p[1]], True, False)

    # list of transition
    def p_transList(p):
        '''transList : trans SC transList
            | empty'''

    # transition without or with name
    def p_trans(p):
        '''trans : LCROCHET RCROCHET funexp FLECHE updatesProb
                | LCROCHET NAME RCROCHET funexp FLECHE updatesProb'''
        if len(p) == 6:
            t, e = p[3]
            if t == "bool":
                curentMod.add_transition("", rea(e, dic), p[5])
            else:
                raise Exception('Not bool in cond' + e)
        else:
            t, e = p[4]
            if t == "bool":
                curentMod.add_transition(p[2], rea(e, dic), p[6])
            else:
                raise Exception('Not bool in cond' + e)

    def p_updatesProb(p):
        '''updatesProb : funexp DDOT updates PLUS updatesProb
            | funexp DDOT updates
            | updates'''
        if len(p) > 4:
            _, e = p[1]
            p[0] = p[5] + [[rea(e, dic), p[3]]]
        elif len(p) > 3:
            _, e = p[1]
            p[0] = [[rea(e, dic), p[3]]]
        else:
            p[0] = [[1, p[1]]]

    def p_updates(p):
        '''updates : upd AND updates
            | upd'''
        if len(p) > 2:
            p[0] = {}
            for a in p[1]:
                p[0][a] = p[1][a]
            for b in p[3]:
                p[0][b] = p[3][b]
        else:
            p[0] = p[1]

    def p_upd(p):
        'upd : LPAR NAME NEW EQUAL funexp RPAR'
        _, e = p[5]
        p[0] = {rea(p[2], dic): rea(e, dic)}

    # list of LABELS separated by a semicolon
    def p_labelList(p):
        '''labelList : label SC labelList
            | label SC'''

    def p_label(p):
        'label : LABEL QUOTE NAME QUOTE EQUAL listCond'

    def p_listCond(p):
        '''listCond : NAME EQUAL funexp AND listCond
            | NAME EQUAL funexp'''

    #REWARDS
    def p_rewards(p):
        '''rewards : REWARDS rew ENDREWARDS rewards
            | REWARDS rew ENDREWARDS'''

    def p_rew(p):
        '''rew : QUOTE NAME QUOTE funexp DDOT funexp SC rew
            | LCROCHET NAME RCROCHET funexp DDOT funexp SC rew
            | LCROCHET RCROCHET funexp DDOT funexp SC rew
            | empty'''
        if p[1] == "[":
            if len(p) == 9:
                t, e = p[4]
                _, er = p[6]
                if t == 'bool':
                    pmc.add_reward(p[2], rea(e, dic), rea(er, dic))
                else:
                    raise Exception("Invalid type in condition of reward " +
                                    p[2])
            else:
                t, e = p[3]
                _, er = p[5]
                if t == 'bool':
                    pmc.add_reward('', rea(e, dic), rea(er, dic))
                else:
                    raise Exception("Invalid type in condition of reward " +
                                    p[2])

    # init def:
    def p_initdef(p):
        '''initdef : INIT initlist ENDINIT'''

    def p_inilist(p):
        '''initlist : ainit
            | ainit AND initlist'''

    def p_ainit(p):
        'ainit : NAME EQUAL funexp'
        t1, e = p[3]
        t2 = type[p[1]]
        if t1 == t2:
            pmc.set_init_value(rea(p[1], dic), rea(e, dic))
        else:
            raise Exception("bad type in init :" + e + " = " + p[1])

    # EXPRESSION AS FUNCTION (with parameters and CONSTANTs
    def p_funexpbinop(p):
        '''funexp : funexp PLUS funexp
            | funexp MINUS funexp
            | funexp DIV funexp
            | funexp MULT funexp'''
        t1, e1 = p[1]
        t2, e2 = p[3]
        if t1 == t2 or t1 == "default" or t2 == "default":
            p[0] = ["int", "(%s)" % (e1 + p[2] + e2)]
        else:
            raise Exception("Incompatible type in : " + e1 + p[2] + e2)

    def p_funexpbinopcomp(p):
        '''funexp : funexp GEQ funexp
            | funexp GS funexp
            | funexp LS funexp
            | funexp LEQ funexp'''
        t1, e1 = p[1]
        t2, e2 = p[3]
        if t1 == t2 or t1 == "default" or t2 == "default":
            p[0] = ["bool", "(%s)" % (e1 + p[2] + e2)]
        else:
            raise Exception("Incompatible type in : " + e1 + p[2] + e2)

    def p_funexpequality(p):
        '''funexp : funexp EQUAL funexp'''
        t1, e1 = p[1]
        t2, e2 = p[3]
        if (t1 == t2 and
            (t1 == "bool" or t1 == "default")) or ("default" in (t1, t2)
                                                   and "bool" in (t1, t2)):
            p[0] = ["bool", "(%s&%s)" % (e1, e2)]
        elif t1 == t2 or t1 == "default" or t2 == "default":
            p[0] = [
                "bool",
                "((%s >= 0)&(%s <= 0))" % (e1 + "-" + e2, e1 + "-" + e2)
            ]
        else:
            raise Exception("Incompatible type in : " + e1 + p[2] + e2)

    def p_funexpand(p):
        '''funexp : funexp AND funexp
            | funexp OR funexp'''
        t1, e1 = p[1]
        t2, e2 = p[3]
        if t1 == t2 or t1 == "default" or t2 == "default":
            p[0] = ["bool", "(%s)" % (e1 + p[2] + e2)]
        else:
            raise Exception("Incompatible type in : " + e1 + p[2] + e2)

    def p_funexpunary(p):
        '''funexp : LPAR funexp RPAR
            | NOT funexp
            | MINUS funexp'''
        if len(p) > 3:
            t, e = p[2]
            p[0] = [t, "(%s)" % e]
        elif p[1] == '!':
            t, e = p[2]
            if t == "bool" or t == "default":
                p[0] = ["bool", "(~%s)" % e]
            else:
                raise Exception("incompatible type : ~" + e)
        else:
            t, e = p[2]
            if t == "int" or t == "default":
                p[0] = ["int", "(- %s)" % e]
            else:
                raise Exception("incompatible type : -" + e)

    def p_funexpFloat(p):
        'funexp : FLOAT'
        p[0] = ["int", p[1]]

    def p_funexpTrueFalse(p):
        '''funexp : TRUE
            | FALSE'''
        p[0] = ["bool", p[1]]

    def p_funexpVar(p):
        'funexp : NAME'
        p[0] = [type[p[1]], p[1]]

    def p_funexpParam(p):
        'funexp : NAME LACCO funexp RACCO'
        _, e = p[3]
        p[0] = ["int", "%s(%s)" % (p[1], e)]

    # handeling error in parsing
    def p_error(p):
        print("Syntax error in input!")
        print(p)
        raise Exception("Erreur de syntaxe en entrée")

    parser = yacc.yacc()

    # with open(filepath, 'r') as myfile:
    # parser.parse(myfile.read())
    parser.parse(filepath)

    print("\nparsing OK\n")

    pmc.reinit()
    return pmc
示例#56
0
#    for file in files:
#        print (str(cont)+". "+file)
#        cont = cont + 1

#    while respuesta == False:
#        numArchivo = 1
#        for file in files:
#            if file == files[int(numArchivo)-1]:
#                respuesta = True
#                break

#    print("Has escogido \" %s\" \n" %files[int(numArchivo)-1])

#    return files[int(numArchivo)-1]

#directorio = 'C:/Users/usuario/Downloads/ProyectoAnalisis/test/'
#archivo = buscarFicheros(directorio)
#test = directorio + archivo
#fp = codecs.open(test, "r", "utf-8")
#cadena = fp.read()
#fp.close()

analizador = lex.lex()

#analizador.input(cadena)

#while True:
#    tok = analizador.token()
#    if not tok : break
#    print (tok)
示例#57
0
    '''term : NUMBER'''
    t[0] = t[1]


def p_factor_boolean(t):
    '''term : BOOLEAN'''
    t[0] = t[1]


#Error
def p_error(t):
    #print('Error')
    #print(t)
    raise SyntaxError("SYNTAX ERROR 9")


if len(sys.argv) <= 0 or len(sys.argv) > 2:
    print("Improper command execution")
    exit(0)
debug = False
parser = yacc.yacc(debug=debug)
lexer = lex.lex(debug=debug)
parsing = None
with open(sys.argv[1], 'r') as inputfile:
    parsing = inputfile.read()
try:
    x = parser.parse(parsing, debug=0).check()
except Exception as e:
    #print(e)
    raise SemanticError("Semantic Error")
示例#58
0
 def __init__(self):
     fh = open("log", 'r')
     self.data = fh.read()
     fh.close()
     self.lexer = lex.lex()
     self.lexer.input(self.data)
示例#59
0
t_ignore = " \t"


def t_newline(t):
    r'\n+'
    t.lexer.lineno += t.value.count("\n")


def t_error(t):
    print("Illegal character '%s'" % t.value[0])
    t.lexer.skip(1)


# Build the lexer
import ply.lex as lex
lexer = lex.lex()

# Parsing rules

# precedence = (
#     ('left','PLUS','MINUS'),
#     ('left','TIMES','DIVIDE'),
#     ('right','UMINUS'),
#     )

# dictionary of names
names = {}


def p_statement_assign(t):
    'statement : ID EQUALS expression'
示例#60
0
    t.value = r'\\r"@s"'
    return t


def t_LINUX_NEWLINE(t):
    r"\n"
    t.value = r'\\n"@s"'
    return t


def t_CHARACTER(t):
    "."
    return t


def t_error(t):
    raise AssertionError("All characters are valid for this lexer!")


# Define tokens
tokens = tuple(gen_tokens(globals()))

# Build lexer
lexer = lex()


def str2c(s):
    "Adapts a Python string for both C language and QDT boilerplate generator"
    lexer.input(s)
    return '"' + "".join(t.value for t in lexer) + '"'