PageRenderTime 13ms CodeModel.GetById 9ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/peg/examples/dcv.peg

http://github.com/feyeleanor/RubyGoLightly
Unknown | 34 lines | 26 code | 8 blank | 0 comment | 0 complexity | 87b31f461d5060c1e620b154e5b65ef9 MD5 | raw file
 1# Grammar
 2
 3Stmt	<- SPACE Expr EOL			{ printf("%d\n", pop()); }
 4	 / (!EOL .)* EOL			{ printf("error\n"); }
 5
 6Expr	<- ID { var= yytext[0] } ASSIGN Sum	{ vars[var - 'a']= top(); }
 7	 / Sum
 8
 9Sum	<- Product ( PLUS  Product		{ int r= pop(), l= pop();  push(l + r); }
10		   / MINUS Product		{ int r= pop(), l= pop();  push(l - r); }
11		   )*
12
13Product	<- Value ( TIMES  Value			{ int r= pop(), l= pop();  push(l * r); }
14                 / DIVIDE Value			{ int r= pop(), l= pop();  push(l / r); }
15		 )*
16
17Value	<- NUMBER				{ push(atoi(yytext)); }
18	 / < ID > !ASSIGN			{ push(vars[yytext[0] - 'a']); }
19	 / OPEN Expr CLOSE
20
21# Lexemes
22
23NUMBER	<- < [0-9]+ >	SPACE
24ID	<- < [a-z]  >	SPACE
25ASSIGN	<- '='		SPACE
26PLUS	<- '+'		SPACE
27MINUS	<- '-'		SPACE
28TIMES	<- '*'		SPACE
29DIVIDE	<- '/'		SPACE
30OPEN	<- '('		SPACE
31CLOSE	<- ')'		SPACE
32
33SPACE	<- [ \t]*
34EOL	<- '\n' / '\r\n' / '\r' / ';'