From simon.urbanek at r-project.org  Wed Nov 25 07:09:51 2009
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Wed, 25 Nov 2009 01:09:51 -0500
Subject: [Rd] Question R's parser : - parsing "x=\n 1"
In-Reply-To: <1e7471d50911242203t4e610d3bkbfdddd3816f3df6b@mail.gmail.com>
References: <1e7471d50911242203t4e610d3bkbfdddd3816f3df6b@mail.gmail.com>
Message-ID: <5F1754BC-7BEC-4EC8-9AFE-C399B9F7EEF3@r-project.org>


On Nov 25, 2009, at 1:03 AM, Saptarshi Guha wrote:

> Hello
> I was reading the source main/src/gram.y and had one question, how  
> does R parse
>
> x =
>  1
>
> According the grammar:
>
> prog	:	END_OF_INPUT			{ return 0; }
> 	|	'\n'				{ return xxvalue(NULL,2,NULL); }
> 	|	expr_or_assign '\n'			{ return xxvalue($1,3,&@1); }
> 	|	expr_or_assign ';'			{ return xxvalue($1,4,&@1); }
> 	|	error	 			{ YYABORT; }
> 	;
>
>
> So this should be of the 3rd form.
> Also, the expr_or_assign is of the 2nd form in
>
> expr_or_assign  :    expr                       { $$ = $1; }
>                |    equal_assign               { $$ = $1; }
>                ;
>
> where equal_assign is
>
> equal_assign    :    expr EQ_ASSIGN expr_or_assign              { $$ =
> xxbinary($2,$1,$3); }
>
> When the parser sees 'x' and '=' it expects an expr_or_assign and we
> know it will receive an expr. However, the expr cannot be a new
> line(according to the defn of expr)
>
> So instead of an expr, the parse gets a newline and should fail.
> Q: So how does R parse this?
> I think it fails with a Parse_Incomplete and keeps on reading till EOF
> (or till an expression is complete).
> But this is not really an incomplete expression

It is. "x=" is incomplete -- try it in R and you'll see that you get a  
continuation prompt because it's incomplete:

 > x=
+ 1

Cheers,
S


> but if my
> interpretation is correct a syntax error, yet R parses it.
> So i understood something else or R's engine manages to do things  
> differently
> Regards
> Saptarshi
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>



[Rd] Question R's parser : - parsing "x=\n 1"
Simon Urbanek