File: tinyc.g

package info (click to toggle)
antlr 2.7.7%2Bdfsg-14
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,016 kB
  • sloc: java: 54,649; cs: 12,537; makefile: 8,854; cpp: 7,359; pascal: 5,273; sh: 4,333; python: 4,297; lisp: 1,969; xml: 220; lex: 192; ansic: 127
file content (262 lines) | stat: -rw-r--r-- 4,380 bytes parent folder | download | duplicates (11)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
header 
{
	using Stack						= System.Collections.Stack;
	using TokenStreamRewriteEngine 	= antlr.TokenStreamRewriteEngine;
	using TokenWithIndex 			= antlr.TokenWithIndex;
	using ParseTreeDebugParser 		= antlr.debug.ParseTreeDebugParser;
}

options 
{
	language = "CSharp";
}

// #if ANTLR_VER_2_7_3
	class TinyCParser extends Parser("ParseTreeDebugParser");
/*
// #else
	class TinyCParser extends Parser;
	{
		// Each new rule invocation must have it's own subtree.  Tokens
		// are added to the current root so we must have a stack of subtree roots.
		protected Stack currentParseTreeRoot = new Stack();
	
		// Track most recently created parse subtree so that when parsing
		// is finished, we can get to the root.
		protected ParseTreeRule mostRecentParseTreeRoot = null;
	
		// For every rule replacement with a production, we bump up count.
		protected int numberOfDerivationSteps = 1; // n replacements plus step 0
	
		public ParseTree getParseTree() 
		{
			return mostRecentParseTreeRoot;
		}
	
		public int getNumberOfDerivationSteps() 
		{
			return numberOfDerivationSteps;
		}
	
		public void match(int i)
		{
			addCurrentTokenToParseTree();
			base.match(i);
		}
	
		public void match(BitSet bitSet)
		{
			addCurrentTokenToParseTree();
			base.match(bitSet);
		}
	
		public void matchNot(int i)
		{
			addCurrentTokenToParseTree();
			base.matchNot(i);
		}
	
		// This adds LT(1) to the current parse subtree.  Note that the match()
		//  routines add the node before checking for correct match.  This means
		//  that, upon mismatched token, there will a token node in the tree
		//  corresponding to where that token was expected.  For no viable
		//  alternative errors, no node will be in the tree as nothing was
		//  matched() (the lookahead failed to predict an alternative).
		protected void addCurrentTokenToParseTree()
		{
			if (inputState.guessing > 0) 
			{
				return;
			}
			ParseTreeRule root = (ParseTreeRule) currentParseTreeRoot.Peek();
			ParseTreeToken tokenNode = null;
			if ( LA(1)==Token.EOF_TYPE ) 
			{
				tokenNode = new ParseTreeToken(new antlr.CommonToken("EOF"));
			}
			else 
			{
				tokenNode = new ParseTreeToken(LT(1));
			}
			root.addChild(tokenNode);
		}
	
		// Create a rule node, add to current tree, and make it current root
		public void traceIn(string s)
		{
			if (inputState.guessing > 0)
			{
				return;
			}
			ParseTreeRule subRoot = new ParseTreeRule(s);
			if ( currentParseTreeRoot.Count > 0 )
			{
				ParseTreeRule oldRoot = (ParseTreeRule) currentParseTreeRoot.Peek();
				oldRoot.addChild(subRoot);
			}
			currentParseTreeRoot.Push(subRoot);
			numberOfDerivationSteps++;
		}
	
		// Pop current root; back to adding to old root
		public void traceOut(string s) 
		{
			if (inputState.guessing > 0) 
			{
				return;
			}
			mostRecentParseTreeRoot = (ParseTreeRule) currentParseTreeRoot.Pop();
		}
	}
// #endif
*/

program
	:	( declaration )* EOF
	;

declaration
	:	(variable) => variable
	|	function
	;

declarator
	:	id:ID
	|	STAR id2:ID
	;

variable
	:	type declarator SEMI
	;

function
	:	type id:ID LPAREN
		(formalParameter (COMMA formalParameter)*)?
		RPAREN
		block
	;

formalParameter
	:	type declarator
	;

type:	"int" | "char" | ID ;

block
	:	LCURLY ( statement )* RCURLY
	;

statement
	:	(declaration) => declaration
	|	expr SEMI
	|	"if" LPAREN expr RPAREN statement
		( "else" statement )?
	|	"while" LPAREN expr RPAREN statement
	|	block
	;

expr:	assignExpr
	;

assignExpr
	:	aexpr (ASSIGN assignExpr)?
	;

aexpr
	:	mexpr (PLUS mexpr)*
	;

mexpr
	:	atom (STAR atom)*
	;

atom:	ID
	|	INT
	|	CHAR_LITERAL
	|	STRING_LITERAL
	;


class TinyCLexer extends Lexer;

options 
{
	k 				= 2;
	charVocabulary  = '\3'..'\377';
}

WS	:	(' '
	|	'\t'
	|	'\n'	{newline();}
	|	'\r')
		{ $setType(Token.SKIP); }
	;

SL_COMMENT : 
	"//" 
	(~'\n')* '\n'
	{ $setType(Token.SKIP); newline(); }
	;

ML_COMMENT
	:	"/*"
		(	{ LA(2)!='/' }? '*'
		|	'\n' { newline(); }
		|	~('*'|'\n')
		)*
		"*/"
			{ $setType(Token.SKIP); }
	;


LPAREN
	:	'('
	;

RPAREN
	:	')'
	;

LCURLY:	'{'
	;

RCURLY:	'}'
	;

STAR:	'*'
	;

PLUS:	'+'
	;

ASSIGN
	:	'='
	;

SEMI:	';'
	;

COMMA
	:	','
	;

CHAR_LITERAL
	:	'\'' (options {greedy=false;}:.)* '\''
	;

STRING_LITERAL
	:	'"' (options {greedy=false;}:.)* '"'
	;

protected
DIGIT
	:	'0'..'9'
	;

INT	:	(DIGIT)+
	;

ID	:	('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*
	;