|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Aug 2011
Messaggi: 672
|
[HASKELL] Parser (e Interprete) di un espressione
Ciao a Tutti!
Ho creato un parser in Haskell di un espressione matematica (per ora senza le parentesi, ma con la precedenza degli operatori). Il programma funziona, tuttavia essendo nuovo della programmazione funzionale potreste dirmi se ho fatto tutto correttamente e se ci sono eventuali migliorie applicabili? Codice:
import Text.ParserCombinators.Parsec
type Var = String
data IntExp
= IVar Var
| ICon Int
| Add IntExp IntExp
| Sub IntExp IntExp
| Mul IntExp IntExp
| Div IntExp IntExp
deriving (Read, Show)
number = do
x <- many1(digit)
return (read x :: Int)
-- Parserizza l'espressione ed esegue i calcoli
eseguiTutto:: Parser Int
eseguiTutto =
do
x <- expression
return (executeFunc(x))
-- Parserizza l'espressione dividento in termini (moltiplicazioni e divisioni),
-- e li somma/sottrae (in modo da mantenere la precedenza degli operatori)
expression:: Parser IntExp
expression =
do
x <- term
op <- optionMaybe(char '+' <|> char '-')
case op of
Nothing -> return x
Just op -> do
y <- expression
return (createOperation op x y)
-- Parserizza moltiplicazioni e divisioni consecutive
term :: Parser IntExp
term =
do
x <- number
op <- optionMaybe(char '*' <|> char '/')
case op of
Nothing -> return (ICon x)
Just op -> do
y <- term
return ( createOperation op (ICon x) y )
-- Crea l'operazione a seconda del contenuto del char op
createOperation:: Char -> IntExp -> IntExp -> IntExp
createOperation op x y
| op == '+' = Add x y
| op == '-' = Sub x y
| op == '*' = Mul x y
| op == '/' = Div x y
-- Esegue i calcoli utilizzando la struttura dati
executeFunc :: IntExp -> Int
executeFunc (ICon x) = x
executeFunc (Add x y) = executeFunc(x) + executeFunc(y)
executeFunc (Sub x y) = executeFunc(x) - executeFunc(y)
executeFunc (Mul x y) = executeFunc(x) * executeFunc(y)
executeFunc (Div x y) = executeFunc(x) `div` executeFunc(y)
-- Funzione di utilità, trovata in rete
run :: Show a => Parser a -> String -> IO ()
run p input
= case (parse p "" input) of
Left err -> do{ putStr "parse error at "
; print err
}
Right x -> print x
Codice:
:load Scrivania/haskell/parser.hs run eseguiTutto "100/2+50*2-4" 146 |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Aug 2011
Messaggi: 672
|
Nessuno?
|
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Dec 2005
Città: Istanbul
Messaggi: 1817
|
Due osservazioni:
- Non devi gestire le parentesi ? - Costruisci l'albero sbagliato perche' associ a destra. In altri termini, l'espressione 100/20/2 viene letta come 100/(20/2) invece che (100/20)/2, cosi' ottieni 10 invece che 2(.5)
__________________
One of the conclusions that we reached was that the "object" need not be a primitive notion in a programming language; one can build objects and their behaviour from little more than assignable value cells and good old lambda expressions. —Guy Steele |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Aug 2011
Messaggi: 672
|
uhm Grazie..
Devo ancora aggiungere la gestione delle parentesi.. Sai dirmi come correggere il mio errore?? |
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: Dec 2005
Città: Istanbul
Messaggi: 1817
|
faccio l'esempio per la expression, ma il discorso vale anche per il resto
La grammatica che implementi ora e' fatta piu' o meno cosi' expr ::= term '+' expr | term '-' expr | term e hai implementato l'or semplicemente verificando la presenza o meno della '+' (e la meno). Perche' sia left recursive dovresti cambiarla in expr ::= expr '+' term | expr '-' term | term che pero' non puoi fare direttamente con una libreria combinatoriale come parsec. Per ottenere il risultato devi usare chainl che aiuta ad associare a sinistra. Per un esempio su come va usato ti consiglio di guardare la documentazione di Parser, quella originale. Qui, http://legacy.cs.uu.nl/daan/download...c.html#chainl1 trovi proprio l'esempio dell'espressione che serve a te. Prova a darci una occhiata e farmmi sapere se ti ci trovi.
__________________
One of the conclusions that we reached was that the "object" need not be a primitive notion in a programming language; one can build objects and their behaviour from little more than assignable value cells and good old lambda expressions. —Guy Steele |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 06:32.



















