sentenceParser.mly 2.01 KB
Newer Older
1 2
/* This is two parsers in one. */

3
/* This parser is used to read the sentences provided on the standard input
4
   channel when [--interpret] is set. The entry point is [optional_sentence]. */
5

6
/* It is used also to read a [.messages] file. The entry point is [entry]. */
7 8 9

/* ------------------------------------------------------------------------ */
/* Tokens. */
10 11 12 13

%token COLON EOF EOL
%token<Grammar.Terminal.t> TERMINAL
%token<Grammar.Nonterminal.t> NONTERMINAL
14 15 16 17 18 19 20 21 22 23 24 25

/* ------------------------------------------------------------------------ */
/* Types. */

%{

  open Grammar
  type terminals = Terminal.t list
  type sentence = Nonterminal.t option * terminals
  type located_sentence = Positions.positions * sentence

%}
26

27 28 29 30 31
%type <terminals> terminals
%type <sentence> sentence
%type <located_sentence> located_sentence

/* %start <sentence option> optional_sentence */
32 33 34
%type
  <(Grammar.Nonterminal.t option * Grammar.Terminal.t list) option>
optional_sentence
35 36
%start optional_sentence

37 38 39 40 41
/* %start<located_sentence list> entry */
%type
  <(Positions.positions * (Grammar.Nonterminal.t option * Grammar.Terminal.t list)) list>
entry
%start entry
42 43 44

%%

45 46
/* ------------------------------------------------------------------------ */

47 48
/* An entry is a non-empty list of located sentences. */
entry: located_sentence located_sentences EOF
49 50 51 52 53
  { $1 :: $2 }

/* A list of located sentences. */
located_sentences: { [] } | located_sentence located_sentences { $1 :: $2 }

54 55 56 57 58
/* A located sentence. */
located_sentence: sentence
  { let pos = Positions.two
      (Parsing.symbol_start_pos())
      (Parsing.symbol_end_pos()) in
59 60 61 62
    pos, $1 }

/* An optional sentence. */
optional_sentence:
63 64
| EOF
    { None } 
65 66 67 68 69 70
| sentence
    { Some $1 }

/* A sentence is a pair of an optional non-terminal start symbol and a list
   of terminal symbols. It is terminated by a newline. */
sentence:
71
| NONTERMINAL COLON terminals EOL
72
    { Some $1, $3 }
73
| terminals EOL
74 75 76
    { None, $1 }

/* A list of terminal symbols. */
77 78 79 80 81 82
terminals:
| 
    { [] } 
| TERMINAL terminals
    { $1 :: $2 }