Mini Parser

  • Thread starter Karthikeyan Balasubramanian
  • Start date
K

Karthikeyan Balasubramanian

Hi all,

I have a requirement. I have a to read some commands from file and
output as another file. For example if the input file is like below:

6
WRITE Today
WRITE is
UNDO 1
WRITE my
UNDO 2
WRITE birthday

Output file will be like this.

Today is birthday

WRITE basically write's string after the command and UNDO will undo N
number of line preceding it. Complexity here is last UNDO has to UNDO
the previous undo's work.

So far I have been able to only come up with the below code:

begin
file = File.new("abc.txt", "r")
while (line = file.gets)
line.grep(/WRITE/) { |s|
puts s
}
end
file.close

rescue => err
puts "Exception: #{err}"
err
end

Any help here?
 
P

Paul Smith

Hi all,

=A0I have a requirement. =A0I have a to read some commands from file and
output as another file. =A0For example if the input file is like below:

6
WRITE =A0Today
WRITE =A0is
UNDO =A01
WRITE =A0my
UNDO =A02
WRITE birthday

Output file will be like this.

Today is birthday

WRITE basically write's string after the command and UNDO will undo N
number of line preceding it. =A0Complexity here is last UNDO has to UNDO
the previous undo's work.

So far I have been able to only come up with the below code:

begin
=A0file =3D File.new("abc.txt", "r")
=A0while (line =3D file.gets)
=A0 =A0line.grep(/WRITE/) { |s|
=A0 =A0 =A0puts s
=A0 =A0}
=A0end
=A0file.close

rescue =3D> err
=A0puts "Exception: #{err}"
=A0err
end

Any help here?

If you want undoable commands you need to keep a command history. I'd
check out the Command design pattern if I were you.

--=20
Paul Smith
http://www.nomadicfun.co.uk

(e-mail address removed)
 
S

Sebastian Hungerecker

Am Mittwoch 23 September 2009 11:55:24 schrieb Karthikeyan Balasubramanian:
WRITE basically write's string after the command and UNDO will undo N
number of line preceding it. Complexity here is last UNDO has to UNDO
the previous undo's work.

Obviously you need to find out which WRITEs actually execute, before producing
any output, since it's hard to undo output that's already been printed to the
screen.

Since there are no conditional statements, you know that all the UNDOs will
execute, so you can execute all the UNDOs in phase 1 and then execute the
WRITEs that haven't been UNDOne in PHASE 2.

The problem of UNDOing UNDOs can be solved by simply going through the UNDOs
in reverse order.

This should work:

commands = File.readlines(sourcefile).map {|line| line.chomp.split(/\s+/, 2)}

commands.reverse!
undos = 0
commands.reject! do |command, argument|
if undos > 0
undos -= 1
true
elsif command == "UNDO"
undos = argument.to_i
true
else
false
end
end
commands.reverse!

commands.each do |command, argument|
print argument, " "
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,828
Latest member
LauraCastr

Latest Threads

Top