Created
July 18, 2010 21:44
-
-
Save fables-tales/480735 to your computer and use it in GitHub Desktop.
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
{ | |
module BF where | |
import List | |
} | |
%name bfParse | |
%tokentype { BFToken } | |
%error { BFParseError } | |
%token | |
'+' {BFTokenIncrement} | |
'-' {BFTokenDecrement} | |
'[' {BFTokenLoopOB} | |
']' {BFTokenLoopCB} | |
',' {BFTokenRead} | |
'.' {BFTokenWrite} | |
'<' {BFTokenLeft} | |
'>' {BFTokenRight} | |
%% | |
Exp: Symbol Exp {[$1] : $2} | Symbol {[$1]} | |
Symbol: '+' {BFSymbol $1} | |
| '-' {BFSymbol $1} | |
| '[' {BFSymbol $1} | |
| ']' {BFSymbol $1} | |
| '>' {BFSymbol $1} | |
| '<' {BFSymbol $1} | |
| ',' {BFSymbol $1} | |
| '.' {BFSymbol $1} | |
{ | |
data Exp = BFSymbol BFToken | |
data BFToken = BFTokenIncrement | |
| BFTokenDecrement | |
| BFTokenLoopOB | |
| BFTokenLoopCB | |
| BFTokenRead | |
| BFTokenWrite | |
| BFTokenLeft | |
| BFTokenRight | |
BFParseError :: [BFToken] -> a | |
BFParseError _ = error "Parse error" | |
lexer :: String -> [BFToken] | |
lexer [] = [] | |
lexer (c : string) = if (find (==c) "+-.,[]><") then (makeToken c) : (lexer string) else (lexer string) | |
makeToken :: Char -> BFToken | |
makeToken '+' = BFTokenIncrement | |
makeToken '-' = BFTokenDecrement | |
makeToken '[' = BFTokenLoopOB | |
makeToken ']' = BFTokenLoopCB | |
makeToken ',' = BFTokenRead | |
makeToken '.' = BFTokenWrite | |
makeToken '>' = BFTokenRight | |
makeToken '<' = BFTokenLeft | |
parse :: String -> exp | |
parse s = bfParse (lexer s) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment