Skip to content

Instantly share code, notes, and snippets.

@fables-tales
Created July 18, 2010 21:44
Show Gist options
  • Save fables-tales/480735 to your computer and use it in GitHub Desktop.
Save fables-tales/480735 to your computer and use it in GitHub Desktop.
{
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