/* * parser.mly * Parser for lambda * There should be 10 shift-reduce conflicts. */ %{ (* This is where ocaml declarations go *) (* open Node *) %} %token INT %token IDENT %token LET IN EQUAL LAMBDA LPAREN RPAREN DOT EOF SEMI POUND %start program /* the entry point */ %type program %% program: declaration SEMI program { $3 } | expression SEMI program { $1::$3 } | SEMI program { $2 } | EOF { [] }; declaration: LET IDENT EQUAL dexp { (*print_string "Parsed: "; Lambda.print $4; print_newline();*) Lambda.var_env := ($2,$4)::!Lambda.var_env }; dexp: LET IDENT EQUAL expression IN dexp { (*print_string "Parsed sub exp"; print_newline();*) Lambda.Apply(Lambda.Lambda($2,$6),$4) } | expression { $1 } expression: simpexp { $1 } | application { $1 }; simpexp: IDENT { Lambda.Var($1,0) } | POUND INT { Lambda.Var("",$2) } | INT { Lambda.Num($1) } /* | LPAREN expression RPAREN */ | LPAREN dexp RPAREN { $2 } | abstraction { $1 } abstraction: /* LAMBDA IDENT DOT expression */ LAMBDA IDENT DOT dexp { Lambda.Lambda($2,$4) }; /* | LAMBDA expression */ | LAMBDA dexp { Lambda.Lambda("",$2) }; application: application simpexp { Lambda.Apply($1,$2) } | simpexp simpexp { Lambda.Apply($1,$2) }; %% (* End of parser.mly *)