That was actually a really amateurish attempt. I should have known better. Regular expressions simply can't do that job. I am thinking of trying again, but this time using Parsec.
wtd
Posted: Tue Apr 05, 2005 3:32 pm Post subject: (No subject)
Success! Parsing a basic Turing declaration.
code:
module TuringParser where
import Text.ParserCombinators.Parsec
identifier = (do
l <- letter
r <- many (letter <|> digit <|> char '_')
return (l : r)) <?> "identifier"
basicDeclaration = do
vars <- sepBy1 identifier (try sep <|> return "")
typeSep
typeName <- identifier
return (typeName, vars)
where
sep = many space >> char ',' >> many space
typeSep = many space >> char ':' >> many space
Then running it:
code:
insaneones@ubuntu:~/Programming/Parse $ ghci TuringParser
___ ___ _
/ _ \ /\ /\/ __(_)
/ /_\// /_/ / / | | GHC Interactive, version 6.2.2, for Haskell 98.
/ /_\\/ __ / /___| | http://www.haskell.org/ghc/
\____/\/ /_/\____/|_| Type :? for help.
Loading package base ... linking ... done.
Compiling TuringParser ( TuringParser.hs, interpreted )
Ok, modules loaded: TuringParser.
*TuringParser> parse basicDeclaration "" "foo, bar, baz : int"
Right ("int",["foo","bar","baz"])
bugzpodder
Posted: Thu Apr 07, 2005 6:31 pm Post subject: (No subject)
I've written a SL compiler as a CS assignment (its basic C++ with variables declared in the beginning) using JLex and believe me, that is painful. martin will do it soon... hey martin, you interested in some of the CS books i've got??
Sponsor Sponsor
rizzix
Posted: Thu Apr 07, 2005 8:30 pm Post subject: (No subject)
JLex eh? i believe javacc is the more popular one use now-a-days..