S
S
Semyon Lukyanov2020-02-16 19:40:24
Python
Semyon Lukyanov, 2020-02-16 19:40:24

Programming language lexer FOX wrote a lexer for his programming language, what can be changed or fixed?

Good day, I wrote more than a normal lexer for my language in python.
This lexer is simple because at the initial stage I don’t want to write a mountain of code.
here is the code

#Лексер
import sys
import re

def lex(characters, token_exprs):
  pos = 0
  token = []
  while pos < len(characters):
    match = None
    for token_expr in token_exprs:
      pattern, tag = token_expr
      regex = re.compile(pattern)
      match = regex.match(characters, pos)
      if match:
        text = match.group(0)
        if tag:
          token = (text, tag)
          tokens.append(token)
        break
    if not match:
      sys.stderr.write('Illegal characters: %s\n' % characters[pos])
      sys.exit(1)
    else:
      pos = match.end(0)
  return tokens

  import lexer

  RESERVED = 'RESERVED'
  INT = 'INT'
  ID = 'ID'

  token_exprs = [
    (r'[ \n\t]+',              None),
    (r'#[^\n]*',               None),
    (r'\:=',                   RESERVED),
    (r'\(',                    RESERVED),
    (r'\)',                    RESERVED),
    (r';',                     RESERVED),
    (r'\+',                    RESERVED),
    (r'-',                     RESERVED),
    (r'\*',                    RESERVED),
    (r'/',                     RESERVED),
    (r'<=', RESERVED),
    (r'<', RESERVED),
    (r'>=', RESERVED),
    (r'>', RESERVED),
    (r'=',                     RESERVED),
    (r'!=',                    RESERVED),
    (r'and',                   RESERVED),
    (r'or',                    RESERVED),
    (r'not',                   RESERVED),
    (r'if',                    RESERVED),
    (r'then',                  RESERVED),
    (r'else',                  RESERVED),
    (r'while',                 RESERVED),
    (r'do',                    RESERVED),
    (r'end',                   RESERVED),
    (r'[0-9]+',                INT),
    (r'[A-Za-z][A-Za-z0-9_]*', ID),
]

def imp_lex(characters):
    return lexer.lex(characters, token_exprs)

#Присваиваем глобальную переменую для integer
x := 1

#Условие
if x = 1 then
   y := 2
else
  y := 3
end

#Цикл while
while x < 10 do
  x := x + 1
end

#Составные операторы(Раздельные)
x := 1;
y := 2

#Вычесление факториала
n := 5;
p := 1;
while n > 0 do
  p := p * n;
  n := n - 1
end

I would like to ask what can be added, what can be removed if possible, you can send the corrected code

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question