Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Name – RUSHABH SHAH

Sem –5th
Roll No –19BCP108
Compiler Design

Lab1 – Lex Code to find the number of tokens

Output:

Input:
a = int(input())
a = b + 22
b=c
print(c)

Output: 20

Code:
import ply.lex as lex

tokens = (
'INT',
'FLOAT',
'PLUS',
'MINUS',
'NUMBERSTRING',
'MULTIPLICATION',
'DIVISON',
'MODULO',
'LPAREN',
'NAME',
'INAME',
'COMMA',
'RPAREN',
'DOUBLEQUOTES',
'EQUAL',
)

t_PLUS = r'\+'
t_MINUS = r'-'
t_MULTIPLICATION = r'\*'
t_DIVISON = r'\/'
t_MODULO = r'\%'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_COMMA = r','
t_EQUAL = r'\='

def t_INT(t):
r'\d+'
t.value = int(t.value)
return t

def t_INAME(t):
r'[0-9]+[a-zA-Z]+[0-9]*'
print("Compile time error.")
print("Inllegle character '%s'", t.value[0])
t.lexer.skip(1)

def t_NAME(t):
r'[a-zA-Z][a-zA-Z_0-9]*'
t.type="NAME"
return t

def t_FLOAT(t):
r'\d+\.\d+'
t.value = float(t.value)
return t

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

def t_DOUBLEQUOTES(t):
r'".*"'
t.value = t.value
return t

t_ignore = ' \t'

def t_error(t):
print("Illegal character", t)
t.lexer.skip(1)

lexer = lex.lex()
data = 'a = int(input()) \n a = b + 22 \n b = c \n print(c)'
print("input: ", data)
lexer.input(data)
count = 0
while True:
tok = lexer.token()
if not tok:
break
count += 1

print("number of tokens: ",count)

You might also like