











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












separated by %%
( user declarations ) datatype Re = empty of int | simple of string * int | concat of Re * Re | closure of Re | union of Re * Re; val count = ref 0; fun next() =(count := (!count)+1; !count);
%% ( declarations about the grammar ) %name XXX
%term EOF | STAR | BAR | LP | RP | HASH | SINGLE of string
%nonterm exp of Re %pos int
%% ( grammar rules )
... Docsity.com
%%
(* declarations about the grammar *)
%name XXX
%term EOF | STAR | BAR | LP |
RP | HASH | SINGLE of string
%nonterm exp of Re
%pos int
%%
exp: SINGLE ( simple(SINGLE ,next()) )
| HASH ( empty(next()) ) | LP exp RP ( exp ) | exp STAR ( closure exp ) | exp exp ( concat(exp1,exp2) ) | exp BAR exp ( union(exp1,exp2) )
Complete Example continued %%
go: exp EOF ( exp ) exp: SINGLE ( simple(SINGLE,next()) ) | HASH ( empty( next() ) ) | LP exp RP ( exp ) | exp STAR ( closure exp ) | exp exp %prec DUMMY ( concat(exp1,exp2) ) | exp BAR exp ( union(exp1,exp2) )
type pos = int type svalue = Tokens.svalue type ('a,'b) token = ('a,'b) Tokens.token type lexresult = (svalue,pos) token open Tokens val lineno = ref 0 val reset_lineno = fn () => lineno := 1 val eof = fn () => EOF(!lineno,!lineno) fun error (e,l : int,_) =...
Boiler plate in Sml-Lex source file
New Boiler plate for Parser
structure CommonTypes = struct ( Put type declarations here that you ) ( want to appear in both the parser ) ( and lexer. You can open this structure ) ( else where inside your application as well ) end;
group is CommonTypes.sml XXX.lex XXX.grm driver.sml
( Other user defined sml files go here )
$/basis.cm ( system library files ) $/smlnj-lib.cm $/ml-yacc-lib.cm
XXX.cm
CommonTypes.sml
The .lex files open CommonTypes;
type pos = int type svalue = Tokens.svalue
(* the type token is from the %term in XXX.grm *) type ('a,'b) token = ('a,'b) Tokens.token type lexresult = (svalue,pos) token
(* Defines constructor functions for "token" *) open Tokens val lineno = ref 0 val reset_lineno = fn () => lineno := 1
... (* YOUR USER DECLARATIONS (if any) GO HERE *)
%% %header (functor XXXLexFun(structure Tokens:XXX_TOKENS));
(* YOUR Lex-Definitions (if any) GO HERE *)
%%
(* YOUR RULES GO HERE *)
XXX.lex
The .grm file open CommonTypes;
(* YOUR USER DECLARATIONS (if any) GO HERE *)
%% (* declarations about the grammar *) %name XXX
%term EOF | ...
%nonterm go of? | ...
%pos int %start go %eop EOF %verbose
(* YOUR GRAMMAR DECLARATIONS LIKE %left ETC. (if any) GO HERE *)
%%
go: ... EOF ( ... )
(* YOUR ADDITINAL GRAMMAR RULES GO HERE *)
XXX.grm
Standard ML of New Jersey v110.57 [built: Mon Nov 21 21:46:28 2005]
Number of states = 12 Number of distinct rows = 2 Approx. memory size of trans. table = 258 bytes [parsing (regexp.cm):regexp.lex.sml] [library $/ml-yacc-lib.cm is stable] [library $SMLNJ-ML-YACC-LIB/ml-yacc-lib.cm is stable] [loading (regexp.cm):regexp.grm.sig] [loading (regexp.cm):CommonTypes.sml] [loading (regexp.cm):regexp.grm.sml] [compiling (regexp.cm):regexp.lex.sml] [code: 9617, data: 705, env: 1871 bytes] [loading (regexp.cm):driver.sml] [New bindings added.] val it = true : bool