Can it write an interpreter without building the tree of parse?

View: New views
3 Messages — Rating Filter:   Alert me  

Can it write an interpreter without building the tree of parse?

by Daneel Yaitskov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,


I learn Parsec library now. Parsers from all articles which I found at
first parse input stream entirely and only then call an eval function.
Such method requires to define many data-types for each an element of a
language. And I guess that it hard to write bash interpreter with it
help. Bash makes immediately its commands. For example:

#!/bin/bash
echo HEELLO
)
echo DDDD

dan@tmp$ bash a.sh
HEELLO
a.sh: line 5:  syntax error near unexpected token  `)'
a.sh: line 5: `)'
dan@tmp$

First command of the script is made by bash nonetheless the error in the
next line.

I want to write a interpreter which does actions (IO) inside the parse
function. But I don't know how to do it. That code show that What I
need. Does trick exist that the block function can contain an action
(putStr)?

import Text.ParserCombinators.Parsec
import Data.Map

interpret s = runParser mainf () "" s

mainf :: GenParser Char () (IO ())
mainf = do rr <- block
            eof
            return $ putStrLn rr

block :: GenParser Char () String
block = do s <- string "HELLO WORLD"
            ----- putStrLn "There is an action"
            return s



Daneel Yaitskov.

_______________________________________________
Libraries mailing list
Libraries@...
http://www.haskell.org/mailman/listinfo/libraries

Re: Can it write an interpreter without building the tree of parse?

by Jean-Philippe Bernardy :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

You can use lazy evaluation of achieve this. The idea is that the
parser will throw an exception upon finding an error,
but parsing will proceed only up to the point in the parse tree that
you are currently interpreting.

Malcolm Wallace has written a simple parsing library that you can use
for that purpose.

http://www.springerlink.com/content/n74p58jku234j761/

Cheers,
JP.

On Thu, Oct 15, 2009 at 3:33 PM, Daneel Yaitskov
<rtfm.rtfm.rtfm@...> wrote:

> Hi,
>
>
> I learn Parsec library now. Parsers from all articles which I found at first
> parse input stream entirely and only then call an eval function. Such method
> requires to define many data-types for each an element of a language. And I
> guess that it hard to write bash interpreter with it help. Bash makes
> immediately its commands. For example:
>
> #!/bin/bash
> echo HEELLO
> )
> echo DDDD
>
> dan@tmp$ bash a.sh
> HEELLO
> a.sh: line 5:  syntax error near unexpected token  `)'
> a.sh: line 5: `)'
> dan@tmp$
>
> First command of the script is made by bash nonetheless the error in the
> next line.
>
> I want to write a interpreter which does actions (IO) inside the parse
> function. But I don't know how to do it. That code show that What I need.
> Does trick exist that the block function can contain an action (putStr)?
>
> import Text.ParserCombinators.Parsec
> import Data.Map
>
> interpret s = runParser mainf () "" s
>
> mainf :: GenParser Char () (IO ())
> mainf = do rr <- block
>           eof
>           return $ putStrLn rr
>
> block :: GenParser Char () String
> block = do s <- string "HELLO WORLD"
>           ----- putStrLn "There is an action"
>           return s
>
>
>
> Daneel Yaitskov.
>
> _______________________________________________
> Libraries mailing list
> Libraries@...
> http://www.haskell.org/mailman/listinfo/libraries
>
_______________________________________________
Libraries mailing list
Libraries@...
http://www.haskell.org/mailman/listinfo/libraries

Re: Can it write an interpreter without building the tree of parse?

by Jeff Zaroyko :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, Oct 16, 2009 at 12:33 AM, Daneel Yaitskov
<rtfm.rtfm.rtfm@...> wrote:
> I want to write a interpreter which does actions (IO) inside the parse
> function. But I don't know how to do it. That code show that What I need.
> Does trick exist that the block function can contain an action (putStr)?
>

IO actions inside the parse function? I don't think you should want to
do this, what may work better is to have a parse function which
consumes part of the input, producing a value representing as much as
you're willing to interpret, along with the rest of the input.  You
can use Parsec's getInput function for this.  eg:

get1 = do token <- instruction
          remaining <- getInput
          return (remaining,token)

Jeff.
_______________________________________________
Libraries mailing list
Libraries@...
http://www.haskell.org/mailman/listinfo/libraries