Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Not getting correct results form this code load TinyToken.rb load TinyLexer.rb class Parser Lexer def initialize ( filename ) super ( filename )
Not getting correct results form this code load "TinyToken.rb
load "TinyLexer.rb
class Parser Lexer
def initializefilename
superfilename
consume
@parseerrors
end
def consume
@lookahead nextToken
while@lookahead && @lookahead.type Token::WS
@lookahead nextToken
end
rescue @lookahead Token.newToken::EOF,"EOF"
end
def matchdtype
if @lookahead.type dtype
puts "Expected #dtype found #@lookahead.text
@parseerrors
end
consume
end
def program
while@lookahead.type Token::EOF
puts "Entering STMT Rule"
statement
end
puts "There were #@parseerrors parse errors found."
end
def factor
puts "Entering FACTOR Rule"
if @lookahead.nil?
puts "Unexpected end of input"
return
end
if @lookahead.type, @lookahead.textToken::LPAREN,
matchToken::LPAREN
puts "Entering EXP Rule"
exp
matchToken::RPAREN
elsif @lookahead.type Token::ID@lookahead.type Token::INT
puts "Found #@lookahead.type Token: #@lookahead.text
match@lookahead.type
else
expectedtokens ID or INT or
puts "Expected #expectedtokens found #@lookahead.text
consume
end
puts "Exiting Factor RUle"
end
def term
puts "Entering TERM Rule"
factor
while @lookahead.type, @lookahead.textToken::MULTOP, @lookahead.type, @lookahead.textToken::DIVOP,
puts "Found #@lookahead.type Token: #@lookahead.text
match@lookahead.type
puts "Entering FACTOR Rule"
factor
end
puts "Exiting TERM Rule"
end
def exp
puts "Entering EXP Rule"
term
while @lookahead && Token::ADDOP,Token::SUBOPinclude?@lookahead.type
puts "Found#@lookahead.type Token: #@lookahead.text
match@lookahead.type
puts "Entering TERM Rule"
term
end
puts "Exiting EXP Rule"
end
def assign
puts "Entering ASSGN Rule"
if @lookahead.type Token::ID
puts "Found ID Token: #@lookahead.text
matchToken::ID
matchToken::ASSGN
puts "Entering EXP Rule"
exp
else
puts "Expected ID found #@lookahead.text
consume
end
puts "Exiting ASSGN Rule"
end
def statement
if @lookahead.nil?
puts "Unexpected end of input"
return
end
if @lookahead.type Token::ID
puts "Entering ASSGN Rule"
assign
elsif @lookahead.type Token::PRINT
puts "Found PRINT Token: #@lookahead.text
matchToken::PRINT
puts "Entering EXP Rule"
exp
else
puts "Expected ID or PRINT found #@lookahead.text
consume
end
puts "Exiting STMT Rule"
end
end
if FILE $PROGRAMNAME
filename ARGV
parser Parser.newfilename
parser.program
end This is the correct result
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