ocamlweb-yacc.opp.exp 1.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
%{

  open Lex_syntax
  open Yacc_syntax
    
  let dummy_loc =
    { start_pos = Lexing.dummy_pos;
      end_pos = Lexing.dummy_pos;
      start_line = 0 ;
      start_col = 0 }

%}
%start yacc_definitions
%token <Yacc_syntax.location> Ttypedecl
%token Ttype
%token Ttoken
%token Tstart
%token Tsemicolon
%token Tright
%token Tprec
%token Tor
%token Tnonassoc
%token Tmark
%token Tleft
%token <Yacc_syntax.ident> Tident
%token Terror
%token Tcolon
%token <Yacc_syntax.location> Taction
%token EOF

%type <Yacc_syntax.yacc_definitions> yacc_definitions
%%

yacc_definitions:
| header tokendecls Tmark rules header EOF
    { { header = $1 ; 
	  decls = $2;
	  rules = $4;
	  trailer = $5 } }

header:
| Taction
    { $1 }
| 
    { dummy_loc }

tokendecls:
| tokendecl tokendecls
    { $1::$2 }
| 
    { [] }

tokendecl:
| Ttoken Ttypedecl idlist
    { Typed_tokens($2,$3) }
| Ttoken idlist
    { Untyped_tokens($2) }
| Ttype Ttypedecl idlist
    { Non_terminals_type($2,$3) }
| Tstart idlist
    { Start_symbols($2) }
| Tleft idlist
    { Tokens_assoc($2) }
| Tnonassoc idlist
    { Tokens_assoc($2) }
| Tright idlist
    { Tokens_assoc($2) }

idlist:
| Tident
    { [$1] }
| Tident idlist
    { $1 :: $2 }

rules:
| 
    { [] }
| general_rule rules
    { $1 :: $2 }

general_rule:
| rule Tsemicolon
    { $1 }
| rule
    { Yacc_syntax.issue_warning "ocamlyacc documentation recommends adding a semicolon at end of each grammar rules";
      $1 }

rule:
| Tident Tcolon right_part
    { ($1,$3) }
| Tident Tcolon Tor right_part
    { ($1,$4) }

right_part:
| word Taction
    { [($1,$2)] }
| word Taction Tor right_part
    { ($1,$2) :: $4 }

word:
| 
    { [] }
| Tident word
    { $1 :: $2 }
| Tprec Tident word
    { $2 :: $3 }
| Terror word
    { $2 }

%%