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

Saptarshi Guha saptarshi.guha at gmail.com
Wed Nov 25 07:03:17 CET 2009


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



More information about the R-devel mailing list