what to print for if statement?

34 views
Skip to first unread message

David Pellow

unread,
Nov 25, 2012, 3:03:22 PM11/25/12
to uoft_csc...@googlegroups.com
According to the grammar definition in parser.y a statement can be just a ";" so that an if-else statement can look like:
if (cond)
  ;
else
...
How should this be printed out? If we use a separate node type for "empty" statements do we only print it out when we are in the body of an if-else statement?

Peter Goodman

unread,
Nov 25, 2012, 3:25:06 PM11/25/12
to uoft_csc...@googlegroups.com
Excellent question!

Originally I had omitted the empty ";" from the AST printing spec because it seemed not to contribute anything to the meaning of the program, but you're question highlights an important corner case that I had not thought of!

You should print out "EMPTY" in place of a ";" used in an if/else statement (otherwise just omit it).

So, if you had "if(a) ;" then what would be printed is "(IF a EMPTY)". If you had "if(a) ; else ;" then what would be printed is "(IF a EMPTY EMPTY)", etc.

If your program is: "{ ; }" then, as before, what would be printed is "(SCOPE (DECLARATIONS) (STATEMENTS))".

Best Regards,

Peter Goodman,
http://www.petergoodman.me
65 High Park Ave.,
Toronto, Ontario
M6P 2R7



--
 
 

Ivan Shao

unread,
Nov 25, 2012, 3:35:55 PM11/25/12
to uoft_csc...@googlegroups.com
Hi Peter, 

If I understand you correctly, you are suggesting that if an empty statement appears under an if-statement, "EMPTY" should be printed, but if it appears in the scope, nothing should be printed.

But what if the if-statement is something like:

if(a) {
;
}

Which way should it follow? I don't think it makes much sense to distinguish the empty statement under an if-statement and within a scope.

Please clarify. Thanks

Peter Goodman

unread,
Nov 25, 2012, 3:41:21 PM11/25/12
to uoft_csc...@googlegroups.com

If it appears directly in a scope (regardless of where that scope appears), then don't print EMPTY, just omit it from the STATEMENTS list.

Only print EMPTY if it is *the* statement of an if/else.

Sent from my mobile device.

--
 
 

David Pellow

unread,
Nov 25, 2012, 4:39:22 PM11/25/12
to uoft_csc...@googlegroups.com
Another question:
The grammar allows for empty argument lists but there are no functions with 0 arguments, and the spec for printing says to print one or more arguments. Do we need to handle functions that have zero arguments?

David Pellow

unread,
Nov 25, 2012, 4:44:18 PM11/25/12
to uoft_csc...@googlegroups.com
To clarify:
would we be allowed to remove the possibility for an empty argument list from the grammar definition in the parser?

Peter Goodman

unread,
Nov 25, 2012, 6:18:32 PM11/25/12
to uoft_csc...@googlegroups.com
David,

Good question. The grammar was made to be "easy", i.e. it's simpler to right a grammar for an arbitrary number of arguments. The only functions available, however, have a fixed number of arguments, and you must type check those functions. As such, if a program uses "rsq", then it is not acceptable for "rsq" to take 0 arguments, nor is it acceptable for it to take 10 arguments. It must take exactly as many arguments are in the spec, all of which must have the correct type.

TL;DR: type check according to the pre-defined functions; the grammar generates a "bigger" language than what can be compiled.

Best Regards,

Peter Goodman,
http://www.petergoodman.me
65 High Park Ave.,
Toronto, Ontario
M6P 2R7


--
 
 

Reply all
Reply to author
Forward
0 new messages