Example Printed AST

125 views
Skip to first unread message

Peter Goodman

unread,
Nov 20, 2012, 6:48:07 PM11/20/12
to uoft_csc...@googlegroups.com
Here is an example of a printed AST for the following code.

{
   int a = 1;
   int b = a + 1;
   if(1 < a) { b = 1; }
}

AST:

(SCOPE
  (DECLARATIONS
    (DECLARATION a int 1)
    (DECLARATION b int (BINARY int + a 1)))
  (STATEMENTS
    (IF (BINARY bool < 1 a)
        (SCOPE
          (DECLARATIONS)
          (STATEMENTS
            (ASSIGN int b 1))))))


Hopefully the parentheses all match!

Best Regards,

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

Minsoo Jee

unread,
Nov 20, 2012, 11:33:07 PM11/20/12
to uoft_csc...@googlegroups.com
Hi,

I am confused about parse tree and syntax tree. The print statements above looks like a parse tree.

As you said that up to us to design, would it be still correct to simply have all non-terminals to be nodes and build a complete parse tree with the given grammar from starter file? I will try to omit "unnecessary complexities" but I am not sure it would fit the definition of AST.

Thanks,
Minsoo

Peter Goodman

unread,
Nov 20, 2012, 11:43:54 PM11/20/12
to uoft_csc...@googlegroups.com
You must follow the printer format exactly. You can represent your AST however you like; however, YOU MUST PRINT IT EXACTLY AS DESCRIBED IN THE ASSIGNMENT. You should not print things not listed in the assignment, nor should you omit things from your printed output.

I will mark the correctness/completeness of your implementation by comparing your assignment's printout against the expected printout for some test cases. If your printout does not match the expected printout (modulo whitespace) then marks will be deducted.

Does that clear things up?

Best Regards,

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



--
 
 

Minsoo Jee

unread,
Nov 20, 2012, 11:59:14 PM11/20/12
to uoft_csc...@googlegroups.com
So, it is okay to keep all the non-terminals? Would it still be an AST?

2012년 11월 20일 화요일 오후 11시 44분 15초 UTC-5, Peter Goodman 님의 말:

Peter Goodman

unread,
Nov 21, 2012, 8:27:00 AM11/21/12
to uoft_csc...@googlegroups.com

Some non-terminals map to AST nodes; this is expected.

Sent from my mobile device.

--
 
 

Ivan Shao

unread,
Nov 22, 2012, 12:41:02 AM11/22/12
to uoft_csc...@googlegroups.com
Hi Peter,

For expression "1 == 2", what is expected printout?

(BINARY int AND 1 2) 
or
(BINARY int == 1 2) ?

Thanks

Peter Goodman

unread,
Nov 22, 2012, 9:55:31 AM11/22/12
to uoft_csc...@googlegroups.com
Great question. (BINARY int == 1 2)

If the expression were a && false then the printed AST corresponding to that expression would be (BINARY bool && a false).

Best Regards,

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


--
 
 

Min Chen

unread,
Nov 22, 2012, 1:49:28 PM11/22/12
to uoft_csc...@googlegroups.com
Hi Peter,

I am wondering about why the expected printout for expression "1 == 2" is

(BINARY int == 1 2)

Shouldn't it be (BINARY bool == 1 2)?
i.e. for "if (1 == 2) ", the condition should be a bool-typed expression.

Thanks,

Min Chen

Peter Goodman

unread,
Nov 22, 2012, 1:53:12 PM11/22/12
to uoft_csc...@googlegroups.com
Good catch! My mistake.

Best Regards,

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


--
 
 

Ivan Shao

unread,
Nov 22, 2012, 4:38:14 PM11/22/12
to uoft_csc...@googlegroups.com
Hi Peter,

If I have a variable "a" which is of type "ivec3", what would be the output for the following statement?

a[1] = 10;

My guess would be (ASSIGN int (INDEX ivec3 a 1) 10). Is that right?

Thanks.


On Wednesday, November 21, 2012 7:48:29 AM UTC+8, Peter Goodman wrote:

Peter Goodman

unread,
Nov 22, 2012, 6:17:10 PM11/22/12
to uoft_csc...@googlegroups.com
Great question! You found a really interesting corner case in the AST printing spec :D

That is almost correct. The spec says that the type reported in INDEX is the type of the result, not of the variable. So, the output should be:

(ASSIGN int (INDEX int a 1) 10)

Best Regards,

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


--
 
 

Message has been deleted

Peter Goodman

unread,
Nov 22, 2012, 10:40:25 PM11/22/12
to uoft_csc...@googlegroups.com
Kevin,

There is a sort of implicit precedence, in that you will find errors in children AST nodes before you find them in parents. Otherwise, so long as you detect that there are errors in some erroneous file, and that the errors you report are accurate, then that is fine. You are not required to sort the errors by line/column number (in the event that you do the bonus). 

Best Regards,

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


On Thu, Nov 22, 2012 at 7:50 PM, Kevin Choi <code....@gmail.com> wrote:
Is there precedence on semantic errors to be reported? Does it have to follow line#, col# as in bonus?
--
 
 

Minsoo Jee

unread,
Nov 24, 2012, 5:40:05 PM11/24/12
to uoft_csc...@googlegroups.com
Hi,

Is printing newline also a requirement?

(SCOPE(DECLARATIONS(DECLARATION a int 1)(DECLARATION b int (BINARY int + a 1)))

is this wrong?

Thanks.

2012년 11월 20일 화요일 오후 6시 48분 29초 UTC-5, Peter Goodman 님의 말:

Peter Goodman

unread,
Nov 24, 2012, 6:09:14 PM11/24/12
to uoft_csc...@googlegroups.com

No, as stated in the assignment spec, whitespace is mostly ignored in the printing format.

Sent from my mobile device.

--
 
 

David Pellow

unread,
Nov 25, 2012, 1:14:24 PM11/25/12
to uoft_csc...@googlegroups.com
question about type for statement:
In the example you posted b = 1 is printed as int b 1.
Is this because the type of 1 is int, or because the type of b is int?
if b had been declared as a float should it still print out int b 1 because b is being assigned an int value?

Peter Goodman

unread,
Nov 25, 2012, 2:46:32 PM11/25/12
to uoft_csc...@googlegroups.com
From the spec:
(ASSIGN type variable-name new-value) where new-value is an expression form, and
type is the type form corresponding to the type of the variable.

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