Skip to content

Commit

Permalink
Added simplecalc example.
Browse files Browse the repository at this point in the history
  • Loading branch information
Berke Durak committed Dec 13, 2007
1 parent d0531ad commit c44c8c4
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 17 deletions.
3 changes: 3 additions & 0 deletions examples/astsaver/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

all:
@ocamlbuild astsaver.native

clean:
@rm -rf _build _log astsaver.native
7 changes: 5 additions & 2 deletions examples/calculator/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
.PHONY: all
.PHONY: all clean

all:
@ocamlbuild test_arith.native
@ocamlbuild calculator.native

clean:
@rm -rf _build _log calculator.native
2 changes: 1 addition & 1 deletion examples/calculator/_tags
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<test_arith.{byte,native}>: use_aurochs_lib, use_libaurochs, use_unix
<calculator.{byte,native}>: use_aurochs_lib, use_libaurochs, use_unix
File renamed without changes.
18 changes: 8 additions & 10 deletions examples/calculator/myocamlbuild.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ open Command;;
open Ocaml_specific;;
open Outcome;;

let aurochs = ref (S[A"aurochs";A"-target";A"ml";A"-generate"]);;
let aurochs = ref (S[A"aurochs";A"-target";A"ml"]);;
let system_lib_dir = "/usr/lib";;

dispatch
Expand All @@ -30,15 +30,13 @@ dispatch
(S[A"-ccopt"; A("-L"^system_lib_dir); A"-cclib"; A"-laurochs"]);

rule "aurochs: .peg -> .ml,.mli"

~prods:["%.ml";"%.mli"]

~dep:"%.peg"
begin fun env _build ->
let peg = env "%.peg" and ml = env "%.ml" in
let tags = tags_of_pathname ml++"aurochs" in
Cmd(S[!aurochs; T tags; P peg])
end
~prods:["%.ml";"%.mli"]
~dep:"%.peg"
begin fun env _build ->
let peg = env "%.peg" and ml = env "%.ml" in
let tags = tags_of_pathname ml++"aurochs" in
Cmd(S[!aurochs; T tags; P peg])
end
end
| _ -> ()
end
Expand Down
15 changes: 13 additions & 2 deletions examples/markup.peg
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ header ::= <title> line </title>;

body ::= section*;

section ::= h3 | h2 | h1 | list1 | paragraph | text;
section ::= h3 | h2 | h1 | list1 | paragraph | text | verbatim;

h1 ::= "==" <h1> line </h1>;
h2 ::= "===" <h2> line </h2>;
Expand All @@ -32,9 +32,20 @@ indented ::= ~(list1 | list2 | list3) ' '+ segment* '\n';
segment ::=
link
| tt
| {[^{\[\n]+}
| {[^%{\[\n]+}
;

verbatim ::=
"%verbatim"

<pre>
{ ([^%]+ | ~"%verbatim" "%")* }
</pre>

"%verbatim"

;

tt ::= <tt> '{' {[^}]*} '}' </tt>;

space ::= [ \t]+;
Expand Down
7 changes: 7 additions & 0 deletions examples/simplecalc/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.PHONY: all clean

all:
@ocamlbuild simplecalc.native

clean:
@rm -rf _build _log
1 change: 1 addition & 0 deletions examples/simplecalc/_tags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<simplecalc.{byte,native}>: use_aurochs_lib, use_libaurochs, use_unix
43 changes: 43 additions & 0 deletions examples/simplecalc/myocamlbuild.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
(* Ocamlbuild plugin *)

open Ocamlbuild_pack;;
open Ocamlbuild_plugin;;
open Command;;
open Ocaml_specific;;
open Outcome;;

let aurochs = ref (S[A"aurochs";A"-target";A"ml";A"-generate"]);;
let system_lib_dir = "/usr/lib";;

dispatch
begin function
| After_rules ->
begin
List.iter
begin fun dir ->
flag ["ocaml"; "link"] (S[A"-I"; A dir]);
flag ["ocaml"; "compile"] (S[A"-I"; A dir]);
flag ["ocaml"; "doc"] (S[A"-I"; A dir])
end
["+aurochs_lib"];

ocaml_lib ~extern:true "aurochs_lib";

flag ["link"; "ocaml"; "byte"; "use_libaurochs"]
(S[A"-dllib";A("-laurochs"); A"-cclib";A("-laurochs")]);

flag ["link"; "ocaml"; "use_libaurochs"]
(S[A"-ccopt"; A("-L"^system_lib_dir); A"-cclib"; A"-laurochs"]);

rule "aurochs: .peg -> .ml,.mli"
~prods:["%.ml";"%.mli"]
~dep:"%.peg"
begin fun env _build ->
let peg = env "%.peg" and ml = env "%.ml" in
let tags = tags_of_pathname ml++"aurochs" in
Cmd(S[!aurochs; T tags; P peg])
end
end
| _ -> ()
end
;;
35 changes: 35 additions & 0 deletions examples/simplecalc/simplecalc.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(* Test_arith *)

open Aurochs_pack
open Peg

let _ =
let grammar ="
int ::= <num>val:[0-9]+</num>;
outfixing [ \\n\\t]* do
start ::= sum EOF;
sum ::= <add> term '+' sum </add> | term;
term ::= <mul> simple '*' term </mul> | simple;
simple ::= int | '(' sum ')';
done;"
in
let rec eval = function
| Node("root", _, [x]) -> eval x
| Node("add", _, [x;y]) -> (eval x) + (eval y)
| Node("mul", _, [x;y]) -> (eval x) * (eval y)
| Node("sub", _, [x;y]) -> (eval x) - (eval y)
| Node("neg", _, [x]) -> - (eval x)
| Node("num", ["val",x], []) -> int_of_string x
| _ -> assert false
in
while true do
Printf.printf "> %!";
let u = input_line stdin in
try
let t = Aurochs.see ~grammar:(`Source(`String grammar)) ~text:(`String u) in
let x = eval t in
Printf.printf "%d\n%!" x;
with
| x ->
Printf.printf "Exception: %s\n%!" (Printexc.to_string x)
done
41 changes: 39 additions & 2 deletions manual/manual.mku
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,46 @@ In fact, there are many different ways to use Aurochs:
* Use Aurochs to generate a parser in Ocaml, C or amd64 assembly code.
* Use Aurochs to generate a stub code for Ocaml.

=== Way 1: Use Aurochs as a stand-alone parser
Write your grammar in a file {grammar.peg}, then parse a file {file.txt} using
{aurochs -parse file.txt grammar.peg}. The output is a parse tree in XML format.

=== Way 2: Use Aurochs from Ocaml

The simplest Ocaml code would be
%verbatim
open Aurochs_pack;;
open Peg;;
open Arith;;

let rec eval = function
| Node(N_Root, _, [x]) -> eval x
| Node(N_add, _, [x;y]) -> (eval x) + (eval y)
| Node(N_mul, _, [x;y]) -> (eval x) * (eval y)
| Node(N_sub, _, [x;y]) -> (eval x) - (eval y)
| Node(N_neg, _, [x]) -> - (eval x)
| Node(N_number, [A_value,x], []) -> int_of_string x
| _ -> invalid_arg "eval"
;;

let _ =
while true do
let u = input_line stdin in
try
let t = Aurochs.read ~grammar:(`Program Arith.program) ~text:(`String u) in
let x = eval t in
Printf.printf ">>> %d\n%!" x;
Util.with_binary_file_output "foo.bin" (fun oc ->
Marshal.to_channel oc t []);
with
| x ->
Printf.printf "Exception: %s\n%!" (Printexc.to_string x)
done
;;
%verbatim

== Summary of options
* -target {c|amd64|nog|ml|mli|ml_classic}
target language (default c)
* -target {c|amd64|nog|ml|mli|ml_classic} target language (default c)
* -start <string> Set start symbol (default "start")
* -save-nog <file> Save NOG into file
* -load-nog <file> Load NOG from file
Expand Down

0 comments on commit c44c8c4

Please sign in to comment.