//?<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>
		<プログラム> <EOF>	<print "exit(0);">
					<print "}">
		;

<プログラム>				<x <exline #l>
						<errormsg #l "構文エラー">>
		{[<実行文> { ";" <実行文>}] (<コメント> | <CR>)}
		;


<実行文>				<SKIPSPACE>
					<setVar exline ::sys <line #l>>
		(<if文> | <while文> | <print文> | <times文> | <代入文>)
		;


<if文>          "if"	 		<x <errormsg "if文にエラー">>
					<printf "if (">
		<条件式>		<printf ") {" <\_n>>
		<プログラム> 
		[ 
			"else"	  	<x <errormsg "else文にエラー">>
			 		<printf "} else {" <\_n>>
			<プログラム> 
		] 
		"end"			<printf "}" <\_n>>
		;

<while文>       "while"  		<x <errormsg "while文にエラー">>
			 		<printf "while (">
		<条件式> 		<printf ") {" <\_n>>
		<プログラム> 
		"end"			<printf "}" <\_n>>
		;

<times文>       (<NUM #n> | <変数 #n>)
		"." "times"	 	<x <errormsg "timesにエラー">>
			 		<printf "{int ct; for (ct=0; ct<">
					<printf #n>
					<printf "; ct++) {" <\_n>>
		"do"
		<プログラム> 
		"end"			<printf "}}" <\_n>>
		;

<print文>
	       "print"	  	 	<x <errormsg "print文にエラー">>
		<表示項目> {","  <表示項目>}
		;

<表示項目>				<printf 'printf('>
		(
		   <STRINGS #s>		<printf '"%s","' #s '"'>
		 | 
					<printf '"%d", '>
		   <数式>
		)
					<printf ');' <\_n>>
		;

<代入文>        <変数 #v>
		"="  	 		<x <errormsg "代入文にエラー">>
					<printf #v>
					<printf " = ">
		<数式>			<printf ";" <\_n>>
		;

<数式>          <expradd>;

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

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

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

<exprterm>      "(" 			<printf "(">
			<数式> 
		")" 			<printf ")">
		| <NUM> 		<GETTOKEN #n>
					<printf #n>
		| <変数 #v>		<printf #v>
		;

<条件式> 		  	 	<x <errormsg "条件式にエラー">>
		<数式> 
		(">=" | ">" | "==" | "!=" | "<=" | "<") 
					<GETTOKEN #op>
					<printf #op>
		<数式>
		;

<変数 #x> 
		<RANGE #x a z>
		;

<コメント>	"#" <SKIPCR>
		;

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

?<compile>;

