Advertisement
jcramalho

LP - analex

May 13th, 2022
481
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import ply.lex as lex
  2. literals = [',', '=', '(', ')', '{', '}','<','>','+','-','*','/']
  3. tokens = ['COMMENT','INT','INPUT','STR','PRINT','IF','ELSE','OR','AND',
  4.           'NOT','GE','LE','EQ','NEQ','id','str','WHILE','TRUE','FALSE',
  5.           'num']
  6. def t_WHILE(t):
  7.     r'while'
  8.     return t
  9.  
  10. def t_TRUE(t):
  11.     r'True'
  12.     return t
  13.  
  14. def t_FALSE(t):
  15.     r'False'
  16.     return t
  17.  
  18. def t_IF(t):
  19.     r'if'
  20.     return t
  21.  
  22. def t_ELSE(t):
  23.     r'else'
  24.     return t
  25.  
  26. def t_OR(t):
  27.     r'or'
  28.     return t
  29.  
  30. def t_AND(t):
  31.     r'and'
  32.     return t
  33.  
  34. def t_NOT(t):
  35.     r'not'
  36.     return t
  37.  
  38. def t_GE(t):
  39.     r'>='
  40.     return t
  41.  
  42. def t_LE(t):
  43.     r'<='
  44.     return t
  45.  
  46. def t_EQ(t):
  47.     r'=='
  48.     return t
  49.  
  50. def t_NEQ(t):
  51.     r'!='
  52.     return t
  53.  
  54. def t_INT(t):
  55.     r'int'
  56.     return t
  57.  
  58. def t_INPUT(t):
  59.     r'input'
  60.     return t
  61.  
  62. def t_STR(t):
  63.     r'str'
  64.     return t
  65.  
  66. def t_PRINT(t):
  67.     r'print'
  68.     return t
  69.  
  70. def t_str(t):
  71.     r'\"[^"]*\"'
  72.     return t
  73.  
  74. def t_id(t):
  75.     r'[_a-zA-Z]\w*'
  76.     return t
  77.  
  78. def t_COMMENT(t):
  79.     r'\#.*'
  80.  
  81. def t_num(t):
  82.     r'\d+'
  83.     return t
  84.  
  85. t_ignore = " \t\n"
  86.  
  87. def t_error(t):
  88.     print('CarĂ¡ter ilegal: ', t.value[0])
  89.     t.lexer.skip(1)
  90.  
  91. lexer = lex.lex()
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement