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

Simon Urbanek simon.urbanek at r-project.org
Wed Nov 25 07:09:51 CET 2009


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
>
>



More information about the R-devel mailing list