diff options
Diffstat (limited to 'examples/expRecAut.lua')
-rw-r--r-- | examples/expRecAut.lua | 37 |
1 files changed, 19 insertions, 18 deletions
diff --git a/examples/expRecAut.lua b/examples/expRecAut.lua index b8280a7..18d7606 100644 --- a/examples/expRecAut.lua +++ b/examples/expRecAut.lua | |||
@@ -5,13 +5,11 @@ local num = m.R("09")^1 / tonumber | |||
5 | local op = m.S("+-") | 5 | local op = m.S("+-") |
6 | 6 | ||
7 | local labels = {} | 7 | local labels = {} |
8 | local nlabels = 0 | ||
9 | 8 | ||
10 | local function newError(lab, msg, psync, pcap) | 9 | local function newError(lab, msg, psync, pcap) |
11 | nlabels = nlabels + 1 | ||
12 | psync = psync or m.P(-1) | 10 | psync = psync or m.P(-1) |
13 | pcap = pcap or m.P"" | 11 | pcap = pcap or m.P"" |
14 | labels[lab] = { id = nlabels, msg = msg, psync = psync, pcap = pcap } | 12 | labels[lab] = { msg = msg, psync = psync, pcap = pcap } |
15 | end | 13 | end |
16 | 14 | ||
17 | newError("ExpTermFirst", "expected an expression", op + ")", m.Cc(1000)) | 15 | newError("ExpTermFirst", "expected an expression", op + ")", m.Cc(1000)) |
@@ -21,9 +19,8 @@ newError("Extra", "extra characters found after the expression") | |||
21 | 19 | ||
22 | local errors, subject | 20 | local errors, subject |
23 | 21 | ||
24 | local function expect(patt, labname) | 22 | local function expect(patt, lab) |
25 | local i = labels[labname].id | 23 | return patt + m.T(lab) |
26 | return patt + m.T(i) | ||
27 | end | 24 | end |
28 | 25 | ||
29 | local function compute(tokens) | 26 | local function compute(tokens) |
@@ -40,15 +37,6 @@ local function compute(tokens) | |||
40 | return result | 37 | return result |
41 | end | 38 | end |
42 | 39 | ||
43 | local g = m.P { | ||
44 | "Exp", | ||
45 | Exp = m.Ct(m.V"OperandFirst" * (m.C(op) * m.V"Operand")^0) / compute, | ||
46 | OperandFirst = expect(m.V"Term", "ExpTermFirst"), | ||
47 | Operand = expect(m.V"Term", "ExpTermOp"), | ||
48 | Term = num + m.V"Group", | ||
49 | Group = "(" * m.V"Exp" * expect(")", "MisClose"), | ||
50 | } | ||
51 | |||
52 | function recorderror(pos, lab) | 40 | function recorderror(pos, lab) |
53 | local line, col = re.calcline(subject, pos) | 41 | local line, col = re.calcline(subject, pos) |
54 | table.insert(errors, { line = line, col = col, msg = labels[lab].msg }) | 42 | table.insert(errors, { line = line, col = col, msg = labels[lab].msg }) |
@@ -66,16 +54,29 @@ function defaultValue (p) | |||
66 | return p or m.Cc(1000) | 54 | return p or m.Cc(1000) |
67 | end | 55 | end |
68 | 56 | ||
69 | local grec = g * expect(m.P(-1), "Extra") | 57 | local g = { |
58 | "Exp", | ||
59 | Exp = m.Ct(m.V"OperandFirst" * (m.C(op) * m.V"Operand")^0) / compute, | ||
60 | OperandFirst = expect(m.V"Term", "ExpTermFirst"), | ||
61 | Operand = expect(m.V"Term", "ExpTermOp"), | ||
62 | Term = num + m.V"Group", | ||
63 | Group = "(" * m.V"Exp" * expect(")", "MisClose"), | ||
64 | } | ||
65 | |||
66 | -- set first rule | ||
67 | g[1] = "S" | ||
68 | g["S"] = g["Exp"] * expect(m.P(-1), "Extra") | ||
70 | for k, v in pairs(labels) do | 69 | for k, v in pairs(labels) do |
71 | grec = m.Rec(grec, record(k) * sync(v.psync) * v.pcap, v.id) | 70 | g[k] = record(k) * sync(v.psync) * v.pcap |
72 | end | 71 | end |
73 | 72 | ||
73 | g = m.P(g) | ||
74 | |||
74 | local function eval(input) | 75 | local function eval(input) |
75 | errors = {} | 76 | errors = {} |
76 | io.write("Input: ", input, "\n") | 77 | io.write("Input: ", input, "\n") |
77 | subject = input | 78 | subject = input |
78 | local result, label, suffix = grec:match(input) | 79 | local result, label, suffix = g:match(input) |
79 | io.write("Syntactic errors found: " .. #errors, "\n") | 80 | io.write("Syntactic errors found: " .. #errors, "\n") |
80 | if #errors > 0 then | 81 | if #errors > 0 then |
81 | local out = {} | 82 | local out = {} |