Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need help reviewing this code and making sure it reads from a file C programming language and it processes it through a lexical, sintaxis
I need help reviewing this code and making sure it reads from a file C programming language and it processes it through a lexical, sintaxis semantic analyzer and then it outputs an object file o with code transformed to assembly code. import pandas as pd
LEXERTABLE pdDataFramecolumns TOKEN'IDENTITY' index range
def lexercode:
SYMBOLS ;:
SYMBOLS;:
SYMBOLSDUP
Open bracket',
close bracket',
semicolon;
comma
colon:
single quote',
plus
equal
greater than',
keywords integers 'main', 'while', 'begin', 'end', 'expr', 'procedure', If 'elseif', 'else', 'then', 'printf', 'and', or
KEYWORDS SYMBOLS keywords
whitespace
lexeme
tokens
code code.replacet
for i char in enumeratecode:
if char whitespace:
lexeme char
if i lencode:
if codei whitespace or codei in KEYWORDS or lexeme in KEYWORDS:
if lexeme :
tokens.appendlexemereplace
lexeme
j
for i in tokens:
for k in SYMBOLSDUP:
if i k:
LEXERTABLE.atj 'TOKEN' i
LEXERTABLE.atj 'IDENTITY' k
j
break
if i in keywords:
LEXERTABLE.atj 'TOKEN' i
LEXERTABLE.atj 'IDENTITY' 'keyword'
j
continue
if iisdigit:
LEXERTABLE.atj 'TOKEN' i
LEXERTABLE.atj 'IDENTITY' 'Digit'
j
continue
elif i not in KEYWORDS:
LEXERTABLE.atj 'TOKEN' i
LEXERTABLE.atj 'IDENTITY' 'Identifier'
j
continue
printfLexer Tokens: tokens
return tokens
if namemain:
code
tokens lexercode
printtokens: tokens
class Node:
def initself type, value None:
self.type type
self.value value
self.children
def addchildselfnode:
self.children.appendnode
class Parser:
def initself tokens:
self.tokens tokens
self.currenttokenindex
def parseself:
return self.expr
def exprself:
node self.term
while self.currenttoken in :
token self.currenttoken
self.eattoken
node Nodetype token, value token
node.addchildselfterm
return node
def termself:
node self.factor
while self.currenttoken in :
token self.current.token
self.eattoken
node Nodetype token, value token
node.addchildselffactor
return node
def factorself:
token self.currenttoken
if token.isdigit:
self.eattoken
return Nodetype 'NUMBER', value inttoken
elif token :
self.eat
node self.expr
self.eat
return node
def currenttokenself:
return self.tokensselfcurrenttokenindex
def eatselftokentype:
if self.currenttoken tokentype:
self.currenttokenindex
else:
raise ExceptionfUnexpected token: selfcurrenttoken
if namemain:
code
tokens lexercode
parser Parsertokens
try:
ast parser.parse
printParsed AST sucessfully"
except Exception as e:
printfparsing error: e
class CodeGenerator:
def initself ast:
self.ast ast
self.code
self.tempcount
def generatecodeself:
self.visitselfast
return self.code
def visitself node:
if node.type 'NUMBER':
self.code.appendfLOAD nodevalue
elif node.type in :
left node.children
self.visitleft
right node.children
self.visitright
self.code.appendfnodetype TEMPselftempcount
self.tempcount
if namemain:
code
tokens lexercode
parser Parsertokens
try:
ast parser.parse
codegen CodeGeneratorast
objectcode codegen.generatecode
printGenerated object code:
for line in objectcode:
printline
except Exception as e:
printfparsing error: e
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started