Class: Parser
- Inherits:
-
Object
- Object
- Parser
- Defined in:
- lib/parser.rb
Instance Method Summary collapse
- #Expr ⇒ Object
- #Factor ⇒ Object
-
#initialize(istream) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
-
#Prog ⇒ Object
private.
- #RestExpr(e) ⇒ Object
- #RestTerm(e) ⇒ Object
- #Storable ⇒ Object
- #Term ⇒ Object
Constructor Details
Instance Method Details
#Factor ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/parser.rb', line 84 def Factor() t = @scan.getToken if t.type == :number then return NumNode.new(t.lex.to_i) end if t.type == :keyword then if t.lex == "R" then return RecallNode.new else raise ParseError.new end end if t.type == :lparen then expr = Expr() t = scan.getToken if t.type == :rparen then return expr else raise ParseError.new end end raise ParseError.new end |
#parse ⇒ Object
11 12 13 |
# File 'lib/parser.rb', line 11 def parse() return Prog() end |
#Prog ⇒ Object
private
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/parser.rb', line 16 def Prog() result = Expr() t = @scan.getToken() if t.type != :eof then print "Expected EOF. Found ", t.type, ".\n" raise ParseError.new end return result end |
#RestExpr(e) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/parser.rb', line 32 def RestExpr(e) t = @scan.getToken() if t.type == :add then return RestExpr(AddNode.new(e,Term())) end if t.type == :sub then return RestExpr(SubNode.new(e,Term())) end @scan.putBackToken() return e end |
#RestTerm(e) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/parser.rb', line 52 def RestTerm(e) t = @scan.getToken if t.type == :times then return RestTerm(TimesNode.new(e,Storable())) end if t.type == :divide then return RestTerm(DivideNode.new(e,Storable())) end if t.type == :mod then return RestTerm(ModNode.new(e,Storable())) end @scan.putBackToken e end |
#Storable ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/parser.rb', line 69 def Storable() factor = Factor() t = @scan.getToken if t.type == :keyword then if t.lex == "S" then return StoreNode.new(factor) else raise ParseError.new end end @scan.putBackToken factor end |