//?<tron>;

<compile>
	::sys<args #x>
	::sys<nth #inputfile #x 1>
	::sys<suffix #outputfile #inputfile c>
	<print inputfile #inputfile>
	<print outputfile #outputfile>
	::sys<openw #outputfile
		::sys<openr #inputfile <TinyRuby>>>
	;


<TinyRuby>				<print "#include <stdio.h>">
					<print "int main() {">
					<print "int a,b,c,d,e,f,g,h,i,j,k,l,m;">
					<print "int n,o,p,q,r,s,t,u,v,w,x,y,z;">
					<setVar exline 1>
		<program> <EOF>		<print "exit(0);">
					<print "}">
		;

<program>				<x <exline #l>
						<errormsg #l "syntax error">>
		{[<sentence> { ";" <sentence>}] (<Comment> | <CR>)};


<sentence>				<SKIPSPACE>
					<setVar exline ::sys <line #l>>
		(<If> | <While> | <Print> | <Times> | <Assignment>)
		;


<If>          "if"	 		<x <errormsg "if ERROR">>
					<printf "if (">
		<Conditional>		<printf ") {" <\_n>>
		<program> 
		[ 
			"else"	  	<x <errormsg "else ERROR">>
			 		<printf "} else {" <\_n>>
			<program> 
		] 
		"end"			<printf "}" <\_n>>
		;

<While>       "while"  			<x <errormsg "while ERROR">>
			 		<printf "while (">
		<Conditional> 		<printf ") {" <\_n>>
		<program> 
		"end"			<printf "}" <\_n>>
		;

<Times>       (<NUM #n> | <VARIABLE #n>)
		"." "times"	 	<x <errormsg "times ERROR">>
			 		<printf "{int ct; for (ct=0; ct<">
					<printf #n>
					<printf "; ct++) {" <\_n>>
		"do"
		<program> 
		"end"			<printf "}}" <\_n>>
		;

<Print>
	       "print"	  	 	<x <errormsg "print ERROR">>
		<Displayitem> {","  <Displayitem>}
		;

<Displayitem>				<printf 'printf('>
		(
		   <STRINGS #s>		<printf '"%s","' #s '"'>
		 | 
					<printf '"%d", '>
		   <Expression>
		)
					<printf ');' <\_n>>
		;

<Assignment>        <VARIABLE #v>
		"="  	 		<x <errormsg "Assignment ERROR">>
					<printf #v>
					<printf " = ">
		<Expression>			<printf ";" <\_n>>
		;

<Expression>          <expradd>;

<expradd>       <exprmul> 
		{ "+" 			<printf "+">
			<exprmul> 
		| "-" 			<printf "-">
			<exprmul> 
		}
		;

<exprmul>       <exprID> 
		{ "*" 			<printf "*">
			<exprID> 
		| "/" 			<printf "/">
			<exprID> 
		}
		;

<exprID>        "+" <exprterm> 
		| "-" 			<printf "-">
			<exprterm> 
		| <exprterm>
		;

<exprterm>      "(" 			<printf "(">
			<Expression> 
		")" 			<printf ")">
		| <NUM> 		<GETTOKEN #n>
					<printf #n>
		| <VARIABLE #v>		<printf #v>
		;

<Conditional> 		  	 	<x <errormsg "Conditional ERROR">>
		<Expression> 
		(">=" | ">" | "==" | "!=" | "<=" | "<") 
					<GETTOKEN #op>
					<printf #op>
		<Expression>
		;

<VARIABLE #x> 
		<RANGE #x a z>
		;

<Comment>	"#" <SKIPCR>
		;

<errormsg #x>
		::sys <line #n>
		<warn "error : " #n ":" #x>
		<exit>
		;
<errormsg #n #x>
		<warn "error : " #n ":" #x>
		<exit>
		;

?<compile>;

