This is an excercise of building a parser and an interpreter in Lua using LPegLabel, hence taking profit from error labels.
Program -> (Cmd | Exp)*
Cmd -> var '=' Exp
Exp -> Exp '+' Term | Exp '-' Term | Term
Term -> Term '*' Factor | Term '/' Factor | Factor
Factor -> num | var | '('Exp')'
First import the higher order function genParser
with
genParser = dofile "exp_grammar.lua"
genParser
receives as parameter a function that "writes to an output" when an expression is evaluated as a statement. The print
function is used by default.
For example:
parser = genParser()
parser("3") --> would print `3`
Now look at this other example:
output = {}
parser = genParser(function (val)
table.insert(output, val)
end)
parser("3 4")
print(output[2]) --> would print `4`
Another example with default output function:
input = [[
a = 1/2
a * 4
]]
parser(input) --> would print `2`
You can read more examples (including some syntatic wrong ones) on test.lua file.
- Currently, error recovery is not implemented.
- Parser evaluates the expressions on the fly. This implies some output results before a syntax error.