This is like, a parser for, like, the world's most bitchen programming langugage, written in, like, y'know, the world's second most bitchen programming language?
This program totally demonstrates how to totally do recursive descent parsing in Erlang without using a seperate scanner.
Grammar: Program ::= Statement [Program]. Statement ::= ["LIKE,"] (Command | Expr). Command ::= "START" Program "BAG" ["THIS"] Label | "REALLY" Program "GOTO" ["THE"] Label | "IF" Expr "THEN" Statement | "FOR" Identifier "=" Expression "TO" Expression {Statement} "SURE". Expr ::= E(1) {"AND" E(1) | "OR" E(1)}. E(1) ::= E(2) {"=" E(2) | "-" E(2)}. E(2) ::= E(3) {"+" E(3) | "-" E(3)}. E(3) ::= E(4) {"*" E(4) | "/" E(4)}. E(4) ::= Prim ["**" E(4)]. Prim ::= ["LIKE"] Atom ["(" Expr ")"] ["MEAN"]. Atom ::= Ident | Literal | "(" Expr ")" | "MAYBE" Expr | "TOTALLY" Expr | "VALLEY" Expr.
Exported Functions | |
---|---|
parse/1 | Parses a VALGOL program. |
test/1 | Runs various tests on the VALGOL parser, including the original demo program from the Lesser-Known Languages VALGOL fortune cookie. |
Internal Documented Functions | |
command/2 | Parses a VALGOL command. |
error/2 | Generates an error. |
expect/3 | Expects a certain token in the input string. |
expr/2 | Parses a VALGOL expression. |
optional/3 | Allows a certain token to be in the input string. |
patom/2 | Parses a VALGOL atom. |
prim/2 | Parses a VALGOL primitive. |
program/2 | Parses a VALGOL program. |
statement/2 | Parses a VALGOL statement. |
parse(string()) -> tree()
Parses a VALGOL program. This is the main user interface to the parser.
test(test_id()) -> tree()
Runs various tests on the VALGOL parser, including the original demo program from the Lesser-Known Languages VALGOL fortune cookie.
command(string(), tree()) -> {string(), tree()}
Parses a VALGOL command.
error(string(), [term()]) -> ok
Generates an error. Not particularly sophisticated.
expect(string(), string(), tree()) -> {string(), tree()}
Expects a certain token in the input string. If it is there, it will be consumed; if not, an error is generated.
expr(string(), tree()) -> {string(), tree()}
Parses a VALGOL expression.
optional(string(), string(), tree()) -> {string(), tree()}
Allows a certain token to be in the input string. If it is there, it will be consumed.
patom(string(), tree()) -> {string(), tree()}
Parses a VALGOL atom.
prim(string(), tree()) -> {string(), tree()}
Parses a VALGOL primitive.
program(string(), tree()) -> {string(), tree()}
Parses a VALGOL program. This is the top-level production of the recursive descent parser.
statement(string(), tree()) -> {string(), tree()}
Parses a VALGOL statement.