Example 1: BNF
jjmeyer0 edited this page Jul 20, 2011
·
1 revision
simple
line_comment = "%".
Expr : expr -> ID PLUS expr.
Id : expr -> ID.
PLUS = "+".
ID = "x".
%{
(* auto-generated by gt *)
open Simple_syntax;;
let parse_error s =
let error =
s^(Simple_util.string_of_pos
(Simple_util.cur_pd()))
in
failwith error;;
%}
%start main
%token EOF
%token <Simple_syntax.__term_not_in_ast__> ID PLUS
%token <Simple_syntax.__terminal__>
%type <Simple_syntax.expr option> main
%type <Simple_syntax.expr> expr
%%
main:
| expr { Some($1) }
| EOF { None }
expr:
| ID PLUS expr { Expr(get_term_pd_not_in_ast $1, $1, $2, $3) }
expr:
| ID { Id(get_term_pd_not_in_ast $1, $1) }
(* auto-generated by gt *)
open Simple_util;;
(* This type is used for terminals.
We do this to have better position data.*)
type __terminal__ = (pd * string);;
type __term_not_in_ast__ = pd;;
type dummy = Dummy
and expr =
| Expr of pd * __term_not_in_ast__ * __term_not_in_ast__ * expr
| Id of pd * __term_not_in_ast__;;
(* pd stands for pos (position) *)
let rec dummy () = ()
and get_terminal_pd = function
| (pd,_) -> pd
and get_term_pd_not_in_ast = function
| (pd) -> pd
and pd_expr = function
| Expr(pd,_,_,_) -> pd
| Id(pd,_) -> pd;;
let pd e = pd_expr e;;