Created
April 19, 2012 00:55
-
-
Save snowmantw/2417631 to your computer and use it in GitHub Desktop.
FunTang-Lambda Parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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