Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 93 lines (78 sloc) 1.586 kb
64726c9 Mike Cooper Oh look, a parser.
mythmon authored
1 $LOAD_PATH << "./"
2
3 require "tokens.rb"
4 require "symboltable.rb"
5 require "lexer.rb"
6
7 def expect(enum, tag)
74a8ee1 fixed bug so parser now spits errors on invalid expressions.
uberj authored
8 begin
9 if enum.peek.tag == tag
10 enum.next
11 else
12 raise SyntaxError
13 end
14 rescue StopIteration
32ff3eb Mike Cooper Fix bug introduced in LUG.
mythmon authored
15 raise SyntaxError
64726c9 Mike Cooper Oh look, a parser.
mythmon authored
16 end
17 end
18
72f6ddc Kevin Ngo initial commit for translator
ngokevin authored
19 def parser(input)
64726c9 Mike Cooper Oh look, a parser.
mythmon authored
20 tree = []
21 tokens = tokenize(input).to_enum
22
32ff3eb Mike Cooper Fix bug introduced in LUG.
mythmon authored
23 loop do
24 # If we are at the end of the file, this will raise a StopIteration, and stop the loop. Otherwise, expect will throw an error.
25 t = tokens.peek
26 if t.nil?
27 break
28 elsif t.tag == :openparen
29 tree << tokens.next
927111f Mike Cooper Fix the parser.
mythmon authored
30 tree << expr(tokens)
31 tree << expect(tokens, :closeparen)
32ff3eb Mike Cooper Fix bug introduced in LUG.
mythmon authored
32 else
33 raise SyntaxError
927111f Mike Cooper Fix the parser.
mythmon authored
34 end
64726c9 Mike Cooper Oh look, a parser.
mythmon authored
35 end
36
72f6ddc Kevin Ngo initial commit for translator
ngokevin authored
37 # puts "+++ Tree Format +++"
38 # print_teh_tree(tree)
39 # puts ""
40 # puts "+++ Array Format +++"
41 # p tree
8f50570 Mike Cooper Fix things
mythmon authored
42 strip_parens(tree)
64726c9 Mike Cooper Oh look, a parser.
mythmon authored
43 end
44
45 def expr(tokens)
46 tree = []
47 loop do
48 t = tokens.peek
49 if t.tag == :closeparen
50 break
927111f Mike Cooper Fix the parser.
mythmon authored
51 elsif t.tag == :openparen
32ff3eb Mike Cooper Fix bug introduced in LUG.
mythmon authored
52 tree << tokens.next
927111f Mike Cooper Fix the parser.
mythmon authored
53 tree << expr(tokens)
54 tree << expect(tokens, :closeparen)
64726c9 Mike Cooper Oh look, a parser.
mythmon authored
55 else
56 tree << tokens.next
57 end
58 end
59
60 tree
61 end
62
63 def print_teh_tree(tree, level=0)
64 tree.each do |t|
65 if t.kind_of? Array
66 print_teh_tree(t, level+1)
67 else
72f6ddc Kevin Ngo initial commit for translator
ngokevin authored
68 puts("\t"*level + t.to_s)
64726c9 Mike Cooper Oh look, a parser.
mythmon authored
69 end
70 end
71 end
135d650 Kevin Ngo strip parens from tree
ngokevin authored
72
73 def strip_parens(tree)
74
75 arr = Array.new
76
77 tree.each do|token|
78 if token.is_a? Token
79 if token.tag == :openparen or token.tag == :closeparen
80 next
81 end
82 arr << token
83 elsif token.class == Array
84 arr << strip_parens(token)
85 end
86 end
87
88 return arr
89
90 end
6aecf35 Mike Cooper Fix indentation.
mythmon authored
91
92 #EOF vim: sw=2:ts=2
Something went wrong with that request. Please try again.