#LL(1) parser as a solution for exercise 4.9 in "Compiler Construction: Principles and Practice"
#String given is (a,(b,(2)),(c))$
#deduce the grammar ;)
class Parser
def initialize()
@parsing_stack = []
@parsing_stack.push('$')
@parsing_stack.push('lexp-seq')
end
def launch!
introduction
result = nil
until result == :done
print "> "
user_response = gets.chomp
result = parse(user_response)
end
@parsing_stack.inspect
conclusion
end
def parse (input)
output = ""
counter = 0
puts "This is how your stack looks like: \n#{@parsing_stack.inspect}"
input.each_char do |single_char|
case single_char
when '('
while @parsing_stack[-1] != single_char
if @parsing_stack[-1] == 'lexp-seq'
@parsing_stack.pop
@parsing_stack.push('lexp-seq1','lexp')
puts @parsing_stack.inspect
elsif @parsing_stack[-1] == 'lexp'
@parsing_stack.pop
@parsing_stack.push('list')
puts @parsing_stack.inspect
elsif @parsing_stack[-1] == 'list'
@parsing_stack.pop
@parsing_stack.push(')','lexp-seq','(')
puts @parsing_stack.inspect
end
end
if @parsing_stack[-1] == single_char
@parsing_stack.pop
puts @parsing_stack.inspect
output += single_char
counter += 1
puts "read #{counter} characters so far"
end
when 'a','b','c'
while @parsing_stack[-1] != 'identifier'
if @parsing_stack[-1] == 'atom'
@parsing_stack.pop
@parsing_stack.push('identifier')
puts @parsing_stack.inspect
elsif @parsing_stack[-1] == 'lexp'
@parsing_stack.pop
@parsing_stack.push('atom')
puts @parsing_stack.inspect
elsif @parsing_stack[-1] == 'lexp-seq'
@parsing_stack.pop
@parsing_stack.push('lexp-seq1', 'lexp')
puts @parsing_stack.inspect
end
end
if @parsing_stack[-1] == 'identifier'
@parsing_stack.pop
puts @parsing_stack.inspect
output += single_char
counter += 1
puts "read #{counter} characters so far"
end
when '0'..'9'
while @parsing_stack[-1] != 'number'
if @parsing_stack[-1] == 'lexp'
@parsing_stack.pop
@parsing_stack.push('atom')
puts @parsing_stack.inspect
elsif @parsing_stack[-1] == 'atom'
@parsing_stack.pop
@parsing_stack.push('number')
puts @parsing_stack.inspect
elsif @parsing_stack[-1] == 'lexp-seq'
@parsing_stack.pop
@parsing_stack.push('lexp-seq1', 'lexp')
puts @parsing_stack.inspect
end
end
if @parsing_stack[-1] == 'number'
@parsing_stack.pop
puts @parsing_stack.inspect
output += single_char
counter += 1
puts "read #{counter} characters so far"
end
when ')'
while @parsing_stack[-1] != single_char
if @parsing_stack[-1] == 'lexp-seq1'
@parsing_stack.pop
puts @parsing_stack.inspect
end
end
if @parsing_stack[-1] == ')'
@parsing_stack.pop
puts @parsing_stack.inspect
output += single_char
counter += 1
puts "read #{counter} characters so far"
end
when ','
while @parsing_stack[-1] != single_char
if @parsing_stack[-1] == 'lexp-seq1'
@parsing_stack.pop
@parsing_stack.push('lexp-seq')
@parsing_stack.push(',')
puts @parsing_stack.inspect
end
end
if @parsing_stack[-1] == ','
@parsing_stack.pop
puts @parsing_stack.inspect
output += single_char
counter += 1
puts "read #{counter} characters so far"
end
when '$'
while @parsing_stack[-1] != single_char
if @parsing_stack[-1] == 'lexp-seq1'
@parsing_stack.pop
puts @parsing_stack.inspect
end
end
if @parsing_stack[-1] == '$'
@parsing_stack.pop
output += single_char
counter += 1
puts "read #{counter} characters so far"
puts "#{output}"
return :done
end
when "\s"
else
puts "\nInput not recognized!"
end
end
end
def introduction
puts "\n<<< Welcome to the LL(1) Parser >>>\n"
puts "This is an interactive LL(1) Parser for you to play with.\n\n"
end
def conclusion
puts "\n<<< Goodbye and happy parsing! >>>"
end
end
parser = Parser.new()
parser.launch!
#!/usr/bin/env python3
import re
import os
from bs4 import BeautifulSoup
with open('titles', 'r') as f:
html = f.read()
soup = BeautifulSoup(html)
titles = {}
for episode in soup.find_all('tr', attrs={'class': "vevent"}):
num = episode.find('td').text
title = episode.find('td', attrs={'class': 'summary'}).text
title = title.replace('"', '')
titles[int(num)] = [num.zfill(2), title]
reg = re.compile('.*S\d+E(\d+).*')
for i in os.listdir(os.getcwd()):
match = reg.match(i)
if match is not None:
episode = int(match.group(1))
path = os.path.join(os.getcwd(), i)
num, title = titles[episode]
ext = os.path.splitext(path)[-1]
filename = '{} - {}{}'.format(num, title, ext)
new_path = os.path.join(os.getcwd(), filename)
print(num, title)
os.rename(path, new_path)
bool ever = true;
DateTime start = DateTime.Now;
// Recommend running this part on a separate thread. It may block the UI.
for(; ever;)
{ }
Console.WriteLine(DateTime.Now - start);
# Piece superclass possible moves:
def possible_moves(deltas, allowed_steps, board)
possible_moves = []
deltas.each do |delta|
current_position = @position
allowed_steps.times do
move = [delta, current_position].transpose.map {|x| x.reduce(:+)}
break unless board.move_on_board?(move)
possible_moves << move if board.piece_at(move).nil?
if !board.piece_at(move).nil?
possible_moves << move if board.piece_at(move).color != self.color
break
end
current_position = move
end
end
possible_moves
end
### For example our Rook Piece subclass inherits this function like so: def possible_moves(board)
def possible_moves(board)
deltas = [[-1,0], [0,-1], [1,0], [0,1]]
super(deltas, 8, board)
end