0

I am trying to create a lexxer and parser for a language and I'm having difficulty understanding the exact syntax when it comes to some more "robust" abstract syntax tree definitions.

The issue in question is :

fun_declaration : FUN ID param_list ':' type '{' fun_block '}' 
                 { M_fun ($2, [$3], $5, $7) }
fun_block : declarations fun_body                   { [$1] [$2] }

The definition of M_fun is as follows:

M_fun (String,[(String,Int,M_type)],M_type,[M_decl],[M_stmt])

So as you can see, $2 relates to string, $3 will return a [(String,Int,M_Type)], $5 will return an M_Type, but $7 is where the issue lies. It's to return a [M_decl], [M_stmt]. Is the above syntax correct? The happy file compiles without complaint, but when I compile the .hs file after, it explodes with ~2000 lines of errors, and I think things like this are the cause.

Aserian
  • 1,047
  • 1
  • 15
  • 31

1 Answers1

1

If you want the fun_block production to return an ([M_decl], [M_stmt]), you need to construct one properly in the associated haskell code. You have:

{ [$1] [$2] }

and this is not a valid expression. I believe you should have:

{ ( [$1], [$2] ) }

You then need to change the type definition of M_fun to

M_fun (String,[(String,Int,M_type)],M_type,([M_decl],[M_stmt]))

so that the last argument to M_fun (i.e. $7 has the same type as that returned by fun_block).

AlexJ136
  • 1,272
  • 1
  • 12
  • 21
  • Thank you, but wouldn't this return an data type of type M_fun (String,[(String,Int,M_type)],M_type,([M_decl],M_stmt])) instead of M_fun (String,[(String,Int,M_type)],M_type,[M_decl],[M_stmt])? Or will haskell figure out that they aren't paired by the data type it's trying to make? – Aserian Mar 26 '15 at 17:12
  • It will return something of type `( [ A ], [ B ] ) where `A` is the type of `declarations` i.e. `$1`, and `B` is the type of `fun_body` i.e. `$2`. I don't know if this is the type that you want, because you haven't posted the type declaration for the `fun_block` production. In any case, you must make sure that the type of the expression in the haskell code of the `fun_block` production matches the type given in the type definition of `fun_block` (if any), and matches the type required in all places that make use of the `fun_block` definition. – AlexJ136 Apr 01 '15 at 13:28
  • Since you define the type of `fun_declaration` as `M_fun (String,[(String,Int,M_type)],M_type,[M_decl],[M_stmt])`, you cannot construct an `M_fun` with the expression `M_fun ($2, [$3], $5, $7)`, as the tuple given to `M_fun` should be 5-ary, not 4-ary. However, you may be able to fix this by changing the definition of `M_fun` to `M_fun (String,[(String,Int,M_type)],M_type,([M_decl],[M_stmt]))`. Note the additional brackets around what was previously the last two arguments to `M_fun`, making `M_fun` 4-ary rather than 5-ary, (I think) making it match the type that `fun_block` is passing it. – AlexJ136 Apr 01 '15 at 13:33