PageRenderTime 17ms CodeModel.GetById 14ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/peg/examples/calc.leg

http://github.com/feyeleanor/RubyGoLightly
Unknown | 46 lines | 36 code | 10 blank | 0 comment | 0 complexity | b58454ed1688209cb3005885466e0df4 MD5 | raw file
 1%{
 2#include <stdio.h>
 3int vars[26];
 4%}
 5
 6Stmt	= - e:Expr EOL			{ printf("%d\n", e); }
 7	| ( !EOL . )* EOL		{ printf("error\n"); }
 8
 9Expr	= i:ID ASSIGN s:Sum		{ $$= vars[i]= s; }
10	| s:Sum				{ $$= s; }
11
12Sum	= l:Product
13		( PLUS  r:Product	{ l += r; }
14		| MINUS r:Product	{ l -= r; }
15		)*			{ $$= l; }
16
17Product	= l:Value
18		( TIMES  r:Value	{ l *= r; }
19		| DIVIDE r:Value	{ l /= r; }
20		)*			{ $$= l; }
21
22Value	= i:NUMBER			{ $$= atoi(yytext); }
23	| i:ID !ASSIGN			{ $$= vars[i]; }
24	| OPEN i:Expr CLOSE		{ $$= i; }
25
26NUMBER	= < [0-9]+ >	-		{ $$= atoi(yytext); }
27ID	= < [a-z]  >	-		{ $$= yytext[0] - 'a'; }
28ASSIGN	= '='		-
29PLUS	= '+'		-
30MINUS	= '-'		-
31TIMES	= '*'		-
32DIVIDE	= '/'		-
33OPEN	= '('		-
34CLOSE	= ')'		-
35
36-	= [ \t]*
37EOL	= '\n' | '\r\n' | '\r' | ';'
38
39%%
40
41int main()
42{
43  while (yyparse());
44
45  return 0;
46}