Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please Remove Errors from this code This code does not gives the desired output and goes into infinite loop Make the code error free and

Please Remove Errors from this code

This code does not gives the desired output and goes into infinite loop

Make the code error free and provide us the desired output

Basically what the code had to do:

Generate Tokens and Identify tokens and provide the output on the basis of input

import token import ply.lex as lex tokens=( 'NUMBER', 'FLOAT', 'IDENTIFIER', 'LPAREN', 'RPAREN', 'PLUS', 'MINUS', 'DIVIDE', 'EQUAL', 'TIMES', 'KEYWORD', 'COLON' )

t_NUMBER=r'[0-9]+' t_FLOAT=r'[0-9][.][0-9]*' t_IDENTIFIER= r' [a-zA-Z_][a-zA-Z0-9]*' t_LPAREN =r'\(' t_RPAREN =r'\)' t_PLUS = r'\+' t_MINUS = r'\-' t_DIVIDE = r'/' t_EQUAL = r'=' t_TIMES = r'\*' t_COLON = r';'

def t_KEYWORD(t): r'int|real|char|float|string|double' return t

def t_error(t): print("Illegal character '%s'" % t.value[0]) t.lexer.skip(1)

lexer = lex.lex() data='25*3+2' element=[] lexer.input(data) while True: Tok = lexer.token() if not Tok: break element.append(Tok.value) input="" for values in element: if values.isdigit(): input+='digit' else: input+=values print(input)

Flag = 1 Stack = ['0'] Rules = ['+','*','(', ')', 'digit', '$', 'L', 'E' ,'T', 'F'] S_R = ['S5', 'S6', 'S7', 'S8','S12','R1', 'R2', 'R3', 'R4', 'R5', 'R6', 'R7'] Production = ['E','E+T','T', 'T*F', 'F', '(E)', 'digit'] Replace = ['L', 'E', 'E', 'T', 'T', 'F', 'F'] Parser_Table = [ ['Error', 'Error', 'S5', 'Error', 'S6', 'Error', 'Error', '1', '2', '3', '4'], ['Error', 'Error', 'Error', 'Error', 'Error', 'Accept', 'Error', 'Error', 'Error', 'Error', 'Error'], ['S7', 'Error', 'S5', 'Error', 'S6', 'R1', 'Error', 'Error', 'Error', 'Error', 'Error'], ['R3', 'S8', 'Error', 'R3', 'Error', 'R3', 'Error', 'Error', 'Error', 'Error', 'Error'], ['R5', 'R5', 'Error', 'R5', 'Error', 'R5', 'Error', 'Error', 'Error', 'Error', 'Error'], ['Error', 'Error', 'S5', 'Error', 'S6', 'Error', 'Error', 'Error', '9', '3', '4'], ['R7', 'R7', 'Error', 'R7', 'Error', 'R7', 'Error', 'Error', 'Error', 'Error', 'Error'], ['Error', 'Error', 'S5', 'Error', 'S6', 'Error', 'Error', 'Error', 'Error', '10', '4'], ['Error', 'Error', 'S5', 'Error', 'S6', 'Error', 'Error', 'Error', 'Error', 'Error', '11'], ['S7', 'Error', 'Error', 'S12', 'Error', 'Error', 'Error', 'Error', 'Error', 'Error', 'Error'], ['R2', 'S8', 'Error', 'R2', 'Error', 'R2', 'Error', 'Error', 'Error', 'Error', 'Error'], ['R4', 'R4', 'Error', 'R4', 'Error', 'R4', 'Error', 'Error', 'Error', 'Error', 'Error'], ['R6', 'R6', 'Error', 'R6', 'Error', 'R6', 'Error', 'Error', 'Error', 'Error', 'Error'], ] from random import seed from random import randint text = input global p,pop, post, temp, i, a ,row temp=0 print('length of text : ', len(text))

def Reduce(msg, Product, a): global p, pop, post, temp, i , row

if text[a]==Rules[i]: temp=Stack[len(Stack)-1] post=int(temp) print("Start new stack is: ", post) print("new SR is: ", Parser_Table[post][i])

for j in range(0,len(S_R),1): if Parser_Table[post][i]==S_R[j]: print('Condition is match is : ', S_R[j]) msg = S_R[j] if msg[0] =='S': Stack.append(text[a]) Stack.append(msg[1]) print('Product rule second value is: ',msg[1]) print(Stack) if Flag =='1': i=i+1

elif msg[0] =='R': print(Stack) temp=Stack[len(Stack)-1] post= int(temp) p = int(msg[1]) print('Product rule second value is: ',p) pop = len(Product[p-1])*2 print('pop values: ', pop) for k in range(0,pop,1): Stack.pop() Stack.append(Replace[p-1]) ss= Replace[p-1] row = int(Stack[len(Stack)-2]) for i in range(0,len(Rules),1): if ss==Rules[i]: print('new stack is: ', post) print('last value of Stack is: ', Parser_Table[row][i]) Stack.append(Parser_Table[row][i]) print(Stack) if Stack[len(Stack)-2] == 'S': print(Stack) else: Reduce(msg,Product,a) a=0 check = 2 stop =len(text) print('while loop is going to stop on: ',stop) while a< stop: print('-------------------') print('your a is ',a, ' and input is ', text[a])

for i in range(0,len(Rules),1): if text[a] ==Rules[i]: temp =Stack[len(Stack)-1] post = int(temp) print('Start new stack is: ', post) print('new SR is: ',Parser_Table[post][i])

if Parser_Table[post][i]=='Accept': Stack.append(Parser_Table[post][i]) print(Stack) a=a+1 break if Parser_Table[post][i]=='Error': print("invalid grammar not passed") a=a+1 break else: for j in range(0,len(S_R),1): if Parser_Table[post][i] ==S_R[j]: print('Condition is match is : ',S_R[j]) msg = S_R[j]

if msg[0] == 'S': Stack.append(text[a]) Stack.append(msg[1]) print('Product rule second value is: ',msg[1]) print(Stack) if Flag ==1: a=a+1 elif msg[0] == 'R': Flag =0 Reduce(msg,Production,a) else: print("Error_Occurred")

if Stack[len(Stack)-check] =='S': a=a+1 check = check+1 break

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

5. How would you describe your typical day at work?

Answered: 1 week ago

Question

7. What qualities do you see as necessary for your line of work?

Answered: 1 week ago