Answered step by step
Verified Expert Solution
Link Copied!

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 initialize(filename)
super(filename)
consume()
@parse_errors =0
end
def consume()
@lookahead = nextToken()
while(@lookahead && @lookahead.type == Token::WS)
@lookahead = nextToken()
end
rescue @lookahead = Token.new(Token::EOF,"EOF")
end
def match(dtype)
if (@lookahead.type != dtype)
puts "Expected #{dtype} found #{@lookahead.text}"
@parse_errors +=1
end
consume()
end
def program()
while(@lookahead.type != Token::EOF)
puts "Entering STMT Rule"
statement()
end
puts "There were #{@parse_errors} parse errors found."
end
def factor()
puts "Entering FACTOR Rule"
if @lookahead.nil?
puts "Unexpected end of input"
return
end
if [@lookahead.type, @lookahead.text]==[Token::LPAREN, "("]
match(Token::LPAREN)
puts "Entering EXP Rule"
exp()
match(Token::RPAREN)
elsif @lookahead.type == Token::ID||@lookahead.type == Token::INT
puts "Found #{@lookahead.type} Token: #{@lookahead.text}"
match(@lookahead.type)
else
expected_tokens ="ID or INT or ("
puts "Expected #{expected_tokens} found #{@lookahead.text}"
consume()
end
puts "Exiting Factor RUle"
end
def term()
puts "Entering TERM Rule"
factor()
while ([@lookahead.type, @lookahead.text]==[Token::MULTOP, "*"])||([@lookahead.type, @lookahead.text]==[Token::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::SUBOP].include?(@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}"
match(Token::ID)
match(Token::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}"
match(Token::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__== $PROGRAM_NAME
filename = ARGV[0]
parser = Parser.new(filename)
parser.program()
end This is the correct result
image text in transcribed

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

Mastering Big Data Interview 751 Comprehensive Questions And Expert Answers

Authors: Mr Bhanu Pratap Mahato

1st Edition

B0CLNT3NVD, 979-8865047216

More Books

Students also viewed these Databases questions

Question

Prepare a short profile of Henry words worth Longfellow?

Answered: 1 week ago

Question

What is RAM as far as telecommunication is concerned?

Answered: 1 week ago

Question

Question 1: What is reproductive system? Question 2: What is Semen?

Answered: 1 week ago

Question

Describe the sources of long term financing.

Answered: 1 week ago

Question

a neglect of quality in relationship to international competitors;

Answered: 1 week ago