From 6112d9b6ff7a0789f15abd8da2b4ca26757af6ef Mon Sep 17 00:00:00 2001 From: vedang6575 <72148375+vedang6575@users.noreply.github.com> Date: Fri, 2 Oct 2020 22:15:54 +0530 Subject: [PATCH] class BasicLexer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thus we will need some basic tokens such as NAME, NUMBER, STRING. In any programming language, there will be space between two characters. Thus we create an ignore literal. Then we also create the basic literals like ‘=’, ‘+’ etc., NAME tokens are basically names of variables, which can be defined by the regular expression [a-zA-Z_][a-zA-Z0-9_]*. STRING tokens are string values and are bounded by quotation marks(” “). This can be defined by the regular expression \”.*?\” --- class BasicLexer with python programming | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 class BasicLexer with python programming diff --git a/class BasicLexer with python programming b/class BasicLexer with python programming new file mode 100644 index 0000000..c991436 --- /dev/null +++ b/class BasicLexer with python programming @@ -0,0 +1,28 @@ + tokens = { NAME, NUMBER, STRING } + ignore = '\t ' + literals = { '=', '+', '-', '/', + '*', '(', ')', ',', ';'} + + + # Define tokens as regular expressions + # (stored as raw strings) + NAME = r'[a-zA-Z_][a-zA-Z0-9_]*' + STRING = r'\".*?\"' + + # Number token + @_(r'\d+') + def NUMBER(self, t): + + # convert it into a python integer + t.value = int(t.value) return t + + # Comment token + @_(r'//.*') + def COMMENT(self, t): + pass + + # Newline token(used only for showing + # errors in new line) + @_(r'\n+') + def newline(self, t): + self.lineno = t.value.count('\n')