Question: I need help integrating a code generator ( outputs a . o object code of the C code read from a txt ) into this

I need help integrating a code generator (outputs a .o object code of the C code read from a txt)into this lexical, sintaxis and semantic analyzer: import re
import tkinter as tk
from tkinter import filedialog, Text
import pandas as pd
import numpy as np
# Lexical Analyzer
LEXER_TABLE = pd.DataFrame(columns=['TOKEN', 'IDENTITY'], index=range(1))
def lexer(code):
SYMBOLS =['(',')',';',',',':','\'']
SYMBOLS_1=['(',')',';',',',':','\'','+','=','>']
SYMBOLS_1_DUP =[['Open bracket', '('],['Close bracket', ')'],['Semi colon', ';'],
['Comma',','],['Colon',':'],['Single quote', '\''],['plus','+'],
['equal','='],['greater','>']]
keywords =['integer', 'main', 'while', 'begin', 'end', 'expr', '','
', 'Procedure',
'If', 'elseif', 'else', 'then', 'printf', 'and', 'or']
keywords_def =[['t', 'integer'],['m', 'main'],['l', 'while'],['b', 'begin'],['d', 'end'],
['e', 'expr'],['s',''],['o','+'],['o','='],['n','
'],['p', 'Procedure'],
['x','If'],['z', 'elseif'],['y', 'else'],['f', 'then'],['c', 'printf'],
['&', 'and'],['|','or'],['o','>']]
KEYWORDS = SYMBOLS_1+ keywords
white_space =''
lexeme =''
tokens =[]
code = code.replace('\t','')
for i, char in enumerate(code):
if char != white_space:
lexeme += char
if (i +1< len(code)):
if code[i +1]== white_space or code[i +1] in KEYWORDS or lexeme in KEYWORDS:
if lexeme !='':
tokens.append(lexeme.replace('
',''))
lexeme =''
j =0
for i in tokens:
for k in SYMBOLS_1_DUP:
if i == k[1]:
LEXER_TABLE.at[j, 'TOKEN']= i
LEXER_TABLE.at[j, 'IDENTITY']= k[0]
j +=1
break
if i in keywords:
LEXER_TABLE.at[j, 'TOKEN']= i
LEXER_TABLE.at[j, 'IDENTITY']= 'Keyword'
j +=1
continue
if i.isdigit():
LEXER_TABLE.at[j, 'TOKEN']= i
LEXER_TABLE.at[j, 'IDENTITY']= 'Digit'
j +=1
continue
elif i not in KEYWORDS:
LEXER_TABLE.at[j, 'TOKEN']= i
LEXER_TABLE.at[j, 'IDENTITY']= 'Identifier'
j +=1
print(f"Lexer Tokens: {tokens}") # Debugging output
return tokens
# Syntax and Semantic Analyzers (Simplified Example)
EPSILON ="\epsi "
grammar =[
"S->E",
"E->E+T",
"E->T",
"T->T*F",
"T->F",
"F->(E)",
"F->id"
]
def get_productions(X):
productions =[]
for prod in grammar:
lhs, rhs = prod.split('->')
if lhs == X:
rhs ='.'+rhs
productions.append('->'.join([lhs, rhs]))
return productions
def closure(I):
for production, a in I:
if production.endswith("."):
continue
lhs, rhs = production.split('->')
alpha, B_beta = rhs.split('.')
B = B_beta[0]
beta = B_beta[1:]
beta_a = a
if beta:
beta_a = beta[0]+a
first_beta_a = first(beta_a)
for b in first_beta_a:
B_productions = get_productions(B)
for gamma in B_productions:
new_item =(gamma, b)
if (new_item not in I):
I.append(new_item)
return I
def get_symbols(grammar):
terminals = set()
non_terminals = set()
for production in grammar:
lhs, rhs = production.split('->')
non_terminals.add(lhs)
for x in rhs:
terminals.add(x)
terminals = terminals.difference(non_terminals)
terminals.add('$')
return terminals, non_terminals
def first(symbols):
final_set = set()
for X in symbols:
first_set = set()
if isTerminal(X):
final_set.add(X)
else:
for production in grammar:
lhs, rhs = production.split('->')
if lhs == X:
for i in range(len(rhs)):
y = rhs[i]
if y == X:
continue
first_y = first(y)
if EPSILON in first_y:
first_y = first_y.replace(EPSILON,"")
first_set.add(first_y)
continue
else:
first_set.add(first_y)
break
for i in first_set:
if EPSILON not in i:
final_set.add(i)
else:
i = i.replace(EPSILON,"")
final_set.add(i)
return "".join(list(final_set))
def isTerminal(symbol):
return symbol in terminals
def shift_dot(production):
lhs, rhs = production.split('->')
x, y = rhs.split(".")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!