aboutsummaryrefslogtreecommitdiff
path: root/runtime/Ruby/test/functional/parser/backtracking.rb
blob: 3860012bcc2dd8aae66b7548576649c8d9f6e478 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/ruby
# encoding: utf-8

require 'antlr3/test/functional'

class TestBacktracking < ANTLR3::Test::Functional

  inline_grammar( <<-'END' )
    grammar Backtrack;
    options {
			language = Ruby;
			backtrack=true;
			memoize=true;
			k=2;
    }
    
    scope Symbols {
    	types;
    }
    
    @members {
      def is_type_name?(name)
        @Symbols_stack.reverse_each do |scope|
          scope.types.include?(name) and return true
        end
        return false
      end
      
      def report_error(e)
        # do nothing
      end
      
    }
    
    translation_unit
    scope Symbols; // entire file is a scope
    @init {
      $Symbols::types = Set.new
    }
    	: external_declaration+
    	;
    
    /** Either a function definition or any other kind of C decl/def.
     *  The LL(*) analysis algorithm fails to deal with this due to
     *  recursion in the declarator rules.  I'm putting in a
     *  manual predicate here so that we don't backtrack over
     *  the entire function.  Further, you get a better error
     *  as errors within the function itself don't make it fail
     *  to predict that it's a function.  Weird errors previously.
     *  Remember: the goal is to avoid backtrack like the plague
     *  because it makes debugging, actions, and errors harder.
     *
     *  Note that k=1 results in a much smaller predictor for the 
     *  fixed look; k=2 made a few extra thousand lines. ;)
     *  I'll have to optimize that in the future.
     */
    external_declaration
    options {k=1;}
    	: ( declaration_specifiers? declarator declaration* '{' )=> function_definition
    	| declaration
    	;
    
    function_definition
    scope Symbols; // put parameters and locals into same scope for now
    @init {
      $Symbols::types = set()
    }
    	:	declaration_specifiers? declarator
    	;
    
    declaration
    scope {
      is_type_def;
    }
    @init {
      $declaration::is_type_def = false
    }
    	: 'typedef' declaration_specifiers? {$declaration::is_type_def = true}
    	  init_declarator_list ';' // special case, looking for typedef	
    	| declaration_specifiers init_declarator_list? ';'
    	;
    
    declaration_specifiers
    	:   (   storage_class_specifier
    		|   type_specifier
            |   type_qualifier
            )+
    	;
    
    init_declarator_list
    	: init_declarator (',' init_declarator)*
    	;
    
    init_declarator
    	: declarator //('=' initializer)?
    	;
    
    storage_class_specifier
    	: 'extern'
    	| 'static'
    	| 'auto'
    	| 'register'
    	;
    
    type_specifier
    	: 'void'
    	| 'char'
    	| 'short'
    	| 'int'
    	| 'long'
    	| 'float'
    	| 'double'
    	| 'signed'
    	| 'unsigned'
    	| type_id
    	;
    
    type_id
        :   { is_type_name?(@input.look(1).text)}? IDENTIFIER
        ;
    
    type_qualifier
    	: 'const'
    	| 'volatile'
    	;
    
    declarator
    	: pointer? direct_declarator
    	| pointer
    	;
    
    direct_declarator
    	:   (	IDENTIFIER
    			{
    			if $declaration.length > 0 && $declaration::is_type_def
						$Symbols::types.add($IDENTIFIER.text)
					end
    			}
    		|	'(' declarator ')'
    		)
            declarator_suffix*
    	;
    
    declarator_suffix
    	:   /*'[' constant_expression ']'
        |*/   '[' ']'
        |   '(' ')'
    	;
    
    pointer
    	: '*' type_qualifier+ pointer?
    	| '*' pointer
    	| '*'
    	;
    
    IDENTIFIER
    	:	LETTER (LETTER|'0'..'9')*
    	;
    	
    fragment
    LETTER
    	:	'$'
    	|	'A'..'Z'
    	|	'a'..'z'
    	|	'_'
    	;
    
    CHARACTER_LITERAL
        :   '\'' ( EscapeSequence | ~('\''|'\\') ) '\''
        ;
    
    STRING_LITERAL
        :  '"' ( EscapeSequence | ~('\\'|'"') )* '"'
        ;
    
    HEX_LITERAL : '0' ('x'|'X') HexDigit+ IntegerTypeSuffix? ;
    
    DECIMAL_LITERAL : ('0' | '1'..'9' '0'..'9'*) IntegerTypeSuffix? ;
    
    OCTAL_LITERAL : '0' ('0'..'7')+ IntegerTypeSuffix? ;
    
    fragment
    HexDigit : ('0'..'9'|'a'..'f'|'A'..'F') ;
    
    fragment
    IntegerTypeSuffix
    	:	('u'|'U')? ('l'|'L')
    	|	('u'|'U')  ('l'|'L')?
    	;
    
    FLOATING_POINT_LITERAL
        :   ('0'..'9')+ '.' ('0'..'9')* Exponent? FloatTypeSuffix?
        |   '.' ('0'..'9')+ Exponent? FloatTypeSuffix?
        |   ('0'..'9')+ Exponent FloatTypeSuffix?
        |   ('0'..'9')+ Exponent? FloatTypeSuffix
    	;
    
    fragment
    Exponent : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;
    
    fragment
    FloatTypeSuffix : ('f'|'F'|'d'|'D') ;
    
    fragment
    EscapeSequence
        :   '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
        |   OctalEscape
        ;
    
    fragment
    OctalEscape
        :   '\\' ('0'..'3') ('0'..'7') ('0'..'7')
        |   '\\' ('0'..'7') ('0'..'7')
        |   '\\' ('0'..'7')
        ;
    
    fragment
    UnicodeEscape
        :   '\\' 'u' HexDigit HexDigit HexDigit HexDigit
        ;
    
    WS  :  (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;}
        ;
    
    COMMENT
        :   '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
        ;
    
    LINE_COMMENT
        : '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
        ;
    LINE_COMMAND 
        : '#' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
        ;
  END

  example "grammar with backtracking and memoization" do
    lexer = Backtrack::Lexer.new( 'int a;' )
    parser = Backtrack::Parser.new lexer
    events = parser.translation_unit
  end

end