Debian Codegraph @

lib/mllpt-lib/ml-lpt/ml-antlr/README

raw
1 ml-antlr
2
3 The tool is invoked using the command
4
5 ml-antlr grammar.g
6
7 The basic specification format is described in the doc directory,
8 in design.pdf.
9
10 ================================================================
11 NOTE: differences from the design document
12 ================================================================
13 - %keywords, %start, %drop, %replace, %extend, %import are
14 unsupported at present
15 ================================================================
16
17 The following example grammar illustrates the format:
18
19 %defs ( (* SML code goes here *) )
20
21 %tokens
22 : LP
23 | RP
24 | PLUS
25 | NUM of int
26 | ID of string
27 ;
28
29 exp
30 : atomicExp (PLUS atomicExp)* => ( foldl op+ atomicExp SR1 )
31 ;
32
33 atomicExp
34 : LP exp RP
35 | NUM
36 | ID => ( lookupVal(ID) )
37 ;
38
39 Actions must always occur in the tail position of rules or subrules.
40 The yield of a token or nonterminal is bound to the name of that
41 token or nonterminal; if there are multiple occurences with the same
42 name, the occurences are numbered starting from one as in ml-yacc.
43
44 Subrules are generated any time items are grouped with parentheses,
45 including for EBNF constructs. In fact, EBNF constructs can only be
46 used on a parenthesized subrule. Subrules are always numbered, and
47 are bound to the names SR1, SR2, ... within each production.
48
49 Actions are optional. The default action is to create a tuple that
50 includes the yield of each data-containing token and each nonterminal
51 referenced. Thus in the example above, the production "LP exp RP"
52 needs no action, since the default action will simply return the yield
53 of the nested exp.
54
55 When ml-antlr analyzes a grammar, it attempts to create a prediction-
56 decision tree for each nonterminal. In the usual case, this decision
57 is made using lookahead token sets. The tool will start with k = 1
58 lookahead and increment to a set value (e.g. k = 5) until it can
59 uniquely predict each production. Subtrees of the decision tree
60 remember the tokens chosen by their parents, and take this into account
61 when computing lookahead. For example, suppose we have two productions
62 at the top level that generate the following sentences:
63
64 prod1 ==> AA
65 prod1 ==> AB
66 prod1 ==> BC
67 prod2 ==> AC
68 prod2 ==> C
69
70 At k = 1, the productions can generate the following sets:
71
72 prod1 {A, B}
73 prod2 {A, C}
74
75 and k = 2,
76
77 prod1 {A, B, C}
78 prod2 {C, $}
79
80 Examining the lookahead sets alone, this grammar fragment looks ambiguous
81 even for k = 2. However, ml-antlr will generate the following decision
82 tree:
83
84 if LA(0) = A then
85 if LA(1) = A or LA(1) = B then
86 predict prod1
87 else if LA(1) = C then
88 predict prod2
89 else if LA(0) = B then
90 predict prod1
91 else if LA(1) = C then
92 predict prod2
93
94 When predictive parsing is not powerful enough to disambiguate a nonterminal,
95 backtracking can be used. Productions are marked as eligible for
96 backtracking by prepending a "%try":
97
98 exp
99 : %try LP VAR (COMMA VAR)* RP COLON VAR
100 | %try LP VAR (COMMA VAR)* RP
101 ;
102
103 When analyzing a grammar, ml-antlr uses roughly the following algorithm:
104
105 computePredictions(k) =
106 computeLookahead(k) // taking into account parent decisions
107 for each ambiguous prediction token set
108 if each predicted production is marked for backtracking
109 use backtracking to make determination
110 else
111 computePredictions(k+1)
112
113 Thus, backtracking will only be used when (1) lookahead prediction fails for
114 at least k = 1 and (2) ALL productions involved in an ambiguity are marked
115 as backtracking. Lookahead can be used to narrow down the choices to a
116 small number of ambiguous productions, which can then be marked as
117 backtracking.
118
119 Note that when backtracking is used, productions are attempted in the
120 order in which they appear in the grammar.
121
122 Semantic predicates may be used anywhere in a production to specify further
123 constraints on the production succeeding. By default, semantic predicates
124 are only used to cause a parse error after a productions has been
125 *unambiguously* selected. To use a semantic predicate to disambiguate
126 a grammar, simply mark the production as backtracking. The exception
127 caused by a predicate failure will be caught by the backtracker, which
128 will attempt the next production.
129
130 The following short grammar illustrates these constructs:
131
132 %tokens
133 : LP
134 | RP
135 | VAR of string
136 | COMMA
137 | COLON
138 ;
139
140 exp
141 : %try LP VAR (COMMA VAR)* RP COLON VAR => ( "prod1" )
142 | %try LP VAR RP => ( "prod2" )
143 %where ( VAR = "x" )
144 | %try LP VAR (COMMA VAR)* RP => ( "prod3" )
145 ;
146
147 Finally, a more substantial example grammar is available in the
148 tests/dragon directory. This grammar is based on the simplified Pascal
149 grammar presented in the appendix to the dragon book. It includes a
150 lexer, a testing program, and an example Pascal program. Use "make" to
151 build the example and "dragon example.pas" to run it.