-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathll1_test.lua
55 lines (52 loc) · 1.74 KB
/
ll1_test.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
local ll1 = require 'll1.ll1'
local utils = require 'common.utils'
local ignore = function(...) return end
local id = function(...)
return setmetatable({...}, {__tostring = function(self)
return '{' .. table.concat(utils.map(tostring, self), ', ') .. '}'
end})
end
-- expr = $consts rexpr' | identifier rexpr' | fun $x -> $expr | ($expr) $rexpr
-- rexpr' = EPS | $expr | + $expr
-- consts = number | string | true | false
local parser = ll1 {
-- '/Users/leegao/sideproject/ParserSiProMo/testing/test_parser.lua',
root = {
{'$expr', action = id},
},
rexpr = {
{'', action = id},
{'$expr', action = id},
{'+', '$expr', action = id},
},
expr = {
{'$consts', '$rexpr', action = id},
{'identifier', '$rexpr', action = id},
{'fun', 'identifier', '->', '$expr', action = id},
{'(', '$expr', ')', '$rexpr', action = id},
},
consts = {
{'number', action = id},
{'string', action = id},
{'true', action = id},
{'false', action = id},
{'a', '1', action = id, tag = 'a1'},
{'a', '2', action = id, tag = 'a2'},
conflict = {
a = function(state, tokens)
if tostring(tokens[2]) == '1' then
return state:go 'a1'
else
return state:go 'a2'
end
end
}
}
}
local tree, trace = parser:parse{"fun", "identifier", "->", "fun", "identifier", "->", "identifier", "+", "a", "1"}
for state, token, tokens, production, args in utils.uloop(trace) do
local args_str = args and table.concat(utils.map(tostring, args), ', ') or 'ERROR'
local prod = (production ~= ERROR and table.concat(production, ' ')) or 'ERROR'
print(state, table.concat(tokens, ' '))
print(' Prod', prod, '{'..args_str..'}')
end