Skip to content

Instantly share code, notes, and snippets.

@snowmantw
Created April 19, 2012 00:55
Show Gist options
  • Save snowmantw/2417631 to your computer and use it in GitHub Desktop.
Save snowmantw/2417631 to your computer and use it in GitHub Desktop.
FunTang-Lambda Parser
/* description: Parses end executes mathematical expressions. */
/* lexical grammar */
%lex
%%
\s+ { return 'SP'; }
"->" { return 'AR'; }
"(" { return 'LP'; }
")" { return 'RP'; }
"," { return 'TP'; }
"x"|"y" { return 'ID' }
<<EOF>> { return 'EOF'}
/lex
/* operator associations and precedence */
%left AR
%start exp
%% /* language grammar */
exp : fn EOF
%{
return $fn;
%}
;
fn
: arrow fn
%{
$$ = $arrow+$fn;
%}
| arrow apply
%{
$$ = $arrow+$apply ;
%}
;
arrow
: tuple AR
%{
$$ = $tuple+$AR;
%}
;
apply
: ID SP apply
%{
$$ = "ENV["+$ID+"]"+$apply;
%}
| ID
%{
$$ = "ENV["+$ID+"]";
%}
;
tuple
: LP tlist RP
%{
$$ = $LP+$tlist+$RP
%}
;
tlist
: ID TP tlist
%{
$$ = $ID+","+$tlist
%}
| ID
%{
$$ = $ID
%}
|
%{
$$ = ''
%}
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment