Created
November 4, 2022 01:51
-
-
Save dhilst/4d1a32f6bf0665407e87a09c08738ffa to your computer and use it in GitHub Desktop.
Getting started with racc (ruby LALR(1) parser lib)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Calcparser | |
prechigh | |
left '*' '/' | |
left '+' '-' | |
preclow | |
options no_result_var | |
rule | |
target: expr | |
expr: expr "+" expr { [:+, val[0], val[2]] } | |
| expr "*" expr { [:*, val[0], val[2]] } | |
| NUMBER | |
end | |
---- inner | |
def make_tokens str | |
require 'strscan' | |
result = [] | |
scanner = StringScanner.new str | |
until scanner.empty? | |
case | |
when scanner.scan(/\s+/) | |
#ignore whitespace | |
when match = scanner.scan(/\+/) | |
result << ["+", "+"] | |
when match = scanner.scan(/\*/) | |
result << ["*", "*"] | |
when match = scanner.scan(/\d+/) | |
result << [:NUMBER, match[0].to_i] | |
else | |
raise "can't recognize <#{scanner.peek(5)}>" | |
end | |
end | |
result << [false, false] | |
return result | |
end | |
attr_accessor :result | |
def parse(str) | |
@result = [] | |
@tokens = make_tokens str | |
do_parse | |
end | |
def next_token | |
@tokens.shift | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './grammar.tab' | |
parser = Calcparser.new | |
x = parser.parse "1 + 2 * 1" | |
p x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
racc grammar.y.rb; echo; ruby main.rb | |
# output : [:+, 1, [:*, 2, 1]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment