daily pastebin goal
16%
SHARE
TWEET

scaner

a guest Apr 16th, 2018 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys
  2. import ply.lex as lex
  3.  
  4. reserved = {
  5.     'zeros' : 'ZEROS',
  6.     'ones' : 'ONES',
  7.     'eye' : 'EYE',
  8.     'while' :'WHILE',
  9.     'if' : 'IF',
  10.     'else' : 'ELSE',
  11.     'for' : 'FOR',
  12.     'continue' : 'CONTINUE',
  13.     'return' : 'RETURN',
  14.     'break' : 'BREAK',
  15.     'print' : 'PRINT'
  16. }
  17.  
  18. literals = [ '(',')',';','{','}','[',']',':',',']
  19.  
  20.  
  21. tokens = [
  22.     'PLUS',
  23.     'MINUS',
  24.     'TIMES',
  25.     'DIVIDE',
  26.     'ID',
  27.     'INT',
  28.     'FLOAT',
  29.     'ASSIGN',
  30.     'LESS',
  31.     'MORE',
  32.     'EQUAL',
  33.     'IN_EQUAL',
  34.     'MORE_EQUAL',
  35.     'LESS_EQUAL',
  36.     'DOTPLUS',
  37.     'DOTTIMES',
  38.     'DOTMINUS',
  39.     'DOTDIVIDE',
  40.     'TRANS',
  41.     'DIVASSIGN',
  42.     'PLUSASSIGN',
  43.     'MULASSIGN',
  44.     'MINUSASSIGN',
  45.     'ASSIGN',
  46.     'QUAT_MARKS'
  47. ]
  48.  
  49. tokens += reserved.values()
  50.  
  51.  
  52. t_PLUS = r'\+'
  53. t_MINUS = r'-'
  54. t_TIMES = r'\*'
  55. t_DIVIDE = r'/'
  56. t_TRANS = r'\''
  57. t_EQUAL = r'=='
  58. t_LESS = r'<'
  59. t_MORE = r'>'
  60. t_DOTPLUS = r'\.\+'
  61. t_DOTMINUS = r'\.-'
  62. t_DOTTIMES = r'\.\*'
  63. t_DOTDIVIDE = r'\./'
  64. t_LESS_EQUAL = r'<='
  65. t_MORE_EQUAL = r'>='
  66. t_IN_EQUAL = r'!='
  67. t_DIVASSIGN = r'/='
  68. t_PLUSASSIGN = r'\+='
  69. t_MINUSASSIGN = r'\-='
  70. t_MULASSIGN = r'\*='
  71. t_ASSIGN = r'='
  72. t_QUAT_MARKS = r'"'
  73.  
  74.  
  75. t_ignore = ' \t'
  76. t_ignore_COMMENT = r'\#.*'
  77.  
  78. def t_ID(t):
  79.     r'[a-zA-Z_]\w*'
  80.     if t.value in reserved:
  81.         t.type = reserved[t.value]
  82.     return t
  83.  
  84.  
  85. def t_FLOAT( t):
  86.     r'\d*\.\d+'
  87.     t.value = float(t.value)
  88.     return t
  89.  
  90. def t_INT( t):
  91.     r'\d+'
  92.     t.value = int(t.value)
  93.     return t
  94.  
  95.  
  96. def t_error(t):
  97.     print("line %d: illegal character '%s'" %(t.lineno, t.value[0]) )
  98.     t.lexer.skip(1)
  99.  
  100.  
  101. def t_newline(t):
  102.     r'\n+'
  103.     t.lexer.lineno += len(t.value)
  104.  
  105. try:
  106.     filename = sys.argv[1] if len(sys.argv) > 1 else "example.txt"
  107.     file = open(filename, "r")
  108. except IOError:
  109.     print("Cannot open {0} file".format(filename))
  110.     sys.exit(0)
  111.  
  112. lexer = lex.lex()
  113. text = file.read()
  114. lexer.input(text)  # Give the lexer some input
  115.  
  116.     # Tokenize
  117. while True:
  118.    tok = lexer.token()
  119.    if not tok:
  120.         break  # No more input
  121.    print("(%d) : %s(%s)" % (tok.lineno, tok.type, tok.value))
RAW Paste Data
Top