File: test.g

package info (click to toggle)
pccts 1.33MR33-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,212 kB
  • ctags: 5,280
  • sloc: ansic: 46,051; cpp: 3,234; makefile: 957
file content (79 lines) | stat: -rw-r--r-- 1,391 bytes parent folder | download | duplicates (19)
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
/* C++ interface test of Parser Exception Handling
 *
 * Given input:
 *
 *		if a+ then a=b+b;
 *
 * the program should respond with
 *
 *		invalid conditional in 'if' statement
 *		found assignment to a
 */
<<
#include <stream.h>
#include "DLGLexer.h"
#include "PBlackBox.h"
typedef ANTLRCommonToken ANTLRToken;

int main()
{
	ParserBlackBox<DLGLexer, PEHTest, ANTLRToken> p(stdin);
	int retsignal;
	p.parser()->rule(&retsignal);
	return 0;
}
>>

/*
Uncommenting this will make ANTLR think you put these handlers at the
end of each rule:

exception
	catch MismatchedToken	: <<printf("dflt:MismatchedToken\n");>>
	default : <<printf("dflt:dflt\n");>>
*/

#token "[\ \t]+"	<<skip();>>
#token "\n"			<<skip(); newline();>>
#token THEN	"then"
#tokclass DIE { "@" "if" ID "else" }

class PEHTest {

rule:	( stat )+
	;

stat:	"if" t:expr THEN stat { "else" stat }
	|	id:ID "=" expr ";"
		<<printf("found assignment to %s\n", $id->getText());>>
	;
  	exception[t]
		default	:
			<<
			printf("invalid conditional in 'if' statement\n");
			consumeUntilToken(THEN);
            suppressSignal;
			>>
  	exception
		catch MismatchedToken	:
		catch NoViableAlt		:
		catch NoSemViableAlt	:
			<<
			printf("stat:caught predefined signal\n");
			consumeUntil(DIE_set);
            suppressSignal;
			>>

expr:	expr1 ("\+" expr1)*
	;

expr1
	:	expr2 ("\*" expr2)*
	;

expr2:	ID
	;

}

#token ID "[a-z]+"