mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-19 01:39:32 -06:00
modernized parser in EmbedAPI
This commit is contained in:
8
examples/tutorial/embedded/LexMath.gf
Normal file
8
examples/tutorial/embedded/LexMath.gf
Normal file
@@ -0,0 +1,8 @@
|
||||
interface LexMath = open Syntax in {
|
||||
|
||||
oper
|
||||
even_A : A ;
|
||||
odd_A : A ;
|
||||
prime_A : A ;
|
||||
|
||||
}
|
||||
8
examples/tutorial/embedded/LexMathEng.gf
Normal file
8
examples/tutorial/embedded/LexMathEng.gf
Normal file
@@ -0,0 +1,8 @@
|
||||
instance LexMathEng of LexMath = open SyntaxEng, ParadigmsEng in {
|
||||
|
||||
oper
|
||||
even_A = mkA "even" ;
|
||||
odd_A = mkA "odd" ;
|
||||
prime_A = mkA "prime" ;
|
||||
|
||||
}
|
||||
8
examples/tutorial/embedded/LexMathFre.gf
Normal file
8
examples/tutorial/embedded/LexMathFre.gf
Normal file
@@ -0,0 +1,8 @@
|
||||
instance LexMathFre of LexMath = open SyntaxFre, ParadigmsFre in {
|
||||
|
||||
oper
|
||||
even_A = mkA "pair" ;
|
||||
odd_A = mkA "impair" ;
|
||||
prime_A = mkA "premier" ;
|
||||
|
||||
}
|
||||
18
examples/tutorial/embedded/Makefile
Normal file
18
examples/tutorial/embedded/Makefile
Normal file
@@ -0,0 +1,18 @@
|
||||
all: gf hs run
|
||||
|
||||
gf:
|
||||
echo "pm | wf math.gfcm" | gf MathEng.gf MathFre.gf
|
||||
|
||||
hs: gf
|
||||
echo "pg -printer=haskell | wf haskell/GSyntax.hs" | gf math.gfcm
|
||||
|
||||
run: hs
|
||||
ghc --make -o ./math -ihaskell haskell/Run.hs
|
||||
strip math
|
||||
|
||||
clean:
|
||||
rm -f *.gfc *.gfr haskell/*.o haskell/*.hi
|
||||
|
||||
distclean:
|
||||
rm -f GSyntax.hs math math.gfcm *.gfc *.gfr haskell/*.o haskell/*.hi
|
||||
|
||||
14
examples/tutorial/embedded/Math.gf
Normal file
14
examples/tutorial/embedded/Math.gf
Normal file
@@ -0,0 +1,14 @@
|
||||
abstract Math = {
|
||||
|
||||
cat Answer ; Question ; Object ;
|
||||
|
||||
fun
|
||||
Even : Object -> Question ;
|
||||
Odd : Object -> Question ;
|
||||
Prime : Object -> Question ;
|
||||
Number : Int -> Object ;
|
||||
|
||||
Yes : Answer ;
|
||||
No : Answer ;
|
||||
|
||||
}
|
||||
6
examples/tutorial/embedded/MathEng.gf
Normal file
6
examples/tutorial/embedded/MathEng.gf
Normal file
@@ -0,0 +1,6 @@
|
||||
--# -path=.:present:prelude:mathematical
|
||||
|
||||
concrete MathEng of Math = MathI with
|
||||
(Syntax = SyntaxEng),
|
||||
(Symbol = SymbolEng),
|
||||
(LexMath = LexMathEng) ;
|
||||
6
examples/tutorial/embedded/MathFre.gf
Normal file
6
examples/tutorial/embedded/MathFre.gf
Normal file
@@ -0,0 +1,6 @@
|
||||
--# -path=.:present:prelude:mathematical
|
||||
|
||||
concrete MathFre of Math = MathI with
|
||||
(Syntax = SyntaxFre),
|
||||
(Symbol = SymbolFre),
|
||||
(LexMath = LexMathFre) ;
|
||||
23
examples/tutorial/embedded/MathI.gf
Normal file
23
examples/tutorial/embedded/MathI.gf
Normal file
@@ -0,0 +1,23 @@
|
||||
incomplete concrete MathI of Math =
|
||||
open Syntax, Symbol, LexMath in {
|
||||
|
||||
flags startcat = Question ; lexer = textlit ; unlexer = text ;
|
||||
|
||||
lincat
|
||||
Answer = Text ;
|
||||
Question = Text ;
|
||||
Object = NP ;
|
||||
|
||||
lin
|
||||
Even = questAdj even_A ;
|
||||
Odd = questAdj odd_A ;
|
||||
Prime = questAdj prime_A ;
|
||||
Number n = mkNP (IntPN n) ;
|
||||
|
||||
Yes = mkText yes_Phr ;
|
||||
No = mkText no_Phr ;
|
||||
|
||||
oper
|
||||
questAdj : A -> NP -> Text = \adj,x -> mkText (mkQS (mkCl x adj)) ;
|
||||
|
||||
}
|
||||
41
examples/tutorial/embedded/TransferLoop.hs
Normal file
41
examples/tutorial/embedded/TransferLoop.hs
Normal file
@@ -0,0 +1,41 @@
|
||||
module Main where
|
||||
|
||||
import GF.Embed.EmbedAPI
|
||||
import GSyntax
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
gr <- file2grammar "math.gfcm"
|
||||
loop (translate answerTree gr)
|
||||
|
||||
loop :: (String -> String) -> IO ()
|
||||
loop trans = do
|
||||
s <- getLine
|
||||
if s == "quit" then putStrLn "bye" else do
|
||||
putStrLn $ trans s
|
||||
loop trans
|
||||
|
||||
translate :: (Tree -> Tree) -> MultiGrammar -> String -> String
|
||||
translate tr gr = unlines . map transLine . lines where
|
||||
transLine s = case parseAllLang gr (startCat gr) s of
|
||||
(lg,t:_):_ -> linearize gr lg (tr t)
|
||||
_ -> "NO PARSE"
|
||||
|
||||
answerTree :: Tree -> Tree
|
||||
answerTree = gf . answer . fg
|
||||
|
||||
answer :: GQuestion -> GAnswer
|
||||
answer p = case p of
|
||||
GOdd x -> test odd x
|
||||
GEven x -> test even x
|
||||
GPrime x -> test prime x
|
||||
|
||||
value :: GObject -> Int
|
||||
value e = case e of
|
||||
GNumber (GInt i) -> fromInteger i
|
||||
|
||||
test :: (Int -> Bool) -> GObject -> GAnswer
|
||||
test f x = if f (value x) then GYes else GNo
|
||||
|
||||
prime :: Int -> Bool
|
||||
prime = (< 8) ----
|
||||
16
examples/tutorial/embedded/Translator.hs
Normal file
16
examples/tutorial/embedded/Translator.hs
Normal file
@@ -0,0 +1,16 @@
|
||||
module Main where
|
||||
|
||||
import GF.Embed.EmbedAPI
|
||||
import System (getArgs)
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
file:_ <- getArgs
|
||||
gr <- file2grammar file
|
||||
interact (translate gr)
|
||||
|
||||
translate :: MultiGrammar -> String -> String
|
||||
translate gr = unlines . map transLine . lines where
|
||||
transLine s =
|
||||
let (lang,tree:_):_ = parseAllLang gr (startCat gr) s
|
||||
in unlines [linearize gr lg tree | lg <- languages gr, lg /= lang]
|
||||
23
examples/tutorial/embedded/TranslatorLoop.hs
Normal file
23
examples/tutorial/embedded/TranslatorLoop.hs
Normal file
@@ -0,0 +1,23 @@
|
||||
module Main where
|
||||
|
||||
import GF.Embed.EmbedAPI
|
||||
import System (getArgs)
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
file:_ <- getArgs
|
||||
gr <- file2grammar file
|
||||
loop (translate gr)
|
||||
|
||||
loop :: (String -> String) -> IO ()
|
||||
loop trans = do
|
||||
s <- getLine
|
||||
if s == "quit" then putStrLn "bye" else do
|
||||
putStrLn $ trans s
|
||||
loop trans
|
||||
|
||||
translate :: MultiGrammar -> String -> String
|
||||
translate gr = unlines . map transLine . lines where
|
||||
transLine s = case parseAllLang gr (startCat gr) s of
|
||||
(lg,t:_):_ -> unlines [linearize gr l t | l <- languages gr, l /= lg]
|
||||
_ -> "NO PARSE"
|
||||
100
examples/tutorial/embedded/haskell/GSyntax.hs
Normal file
100
examples/tutorial/embedded/haskell/GSyntax.hs
Normal file
@@ -0,0 +1,100 @@
|
||||
module GSyntax where
|
||||
|
||||
import GF.Infra.Ident
|
||||
import GF.Grammar.Grammar
|
||||
import GF.Grammar.PrGrammar
|
||||
import GF.Grammar.Macros
|
||||
import GF.Data.Operations
|
||||
----------------------------------------------------
|
||||
-- automatic translation from GF to Haskell
|
||||
----------------------------------------------------
|
||||
|
||||
class Gf a where gf :: a -> Trm
|
||||
class Fg a where fg :: Trm -> a
|
||||
|
||||
newtype GString = GString String deriving Show
|
||||
|
||||
instance Gf GString where
|
||||
gf (GString s) = K s
|
||||
|
||||
instance Fg GString where
|
||||
fg t =
|
||||
case termForm t of
|
||||
Ok ([], K s ,[]) -> GString s
|
||||
_ -> error ("no GString " ++ prt t)
|
||||
|
||||
newtype GInt = GInt Integer deriving Show
|
||||
|
||||
instance Gf GInt where
|
||||
gf (GInt s) = EInt s
|
||||
|
||||
instance Fg GInt where
|
||||
fg t =
|
||||
case termForm t of
|
||||
Ok ([], EInt s ,[]) -> GInt s
|
||||
_ -> error ("no GInt " ++ prt t)
|
||||
|
||||
newtype GFloat = GFloat Double deriving Show
|
||||
|
||||
instance Gf GFloat where
|
||||
gf (GFloat s) = EFloat s
|
||||
|
||||
instance Fg GFloat where
|
||||
fg t =
|
||||
case termForm t of
|
||||
Ok ([], EFloat s ,[]) -> GFloat s
|
||||
_ -> error ("no GFloat " ++ prt t)
|
||||
|
||||
----------------------------------------------------
|
||||
-- below this line machine-generated
|
||||
----------------------------------------------------
|
||||
|
||||
data GAnswer =
|
||||
GYes
|
||||
| GNo
|
||||
deriving Show
|
||||
|
||||
data GObject = GNumber GInt
|
||||
deriving Show
|
||||
|
||||
data GQuestion =
|
||||
GPrime GObject
|
||||
| GOdd GObject
|
||||
| GEven GObject
|
||||
deriving Show
|
||||
|
||||
|
||||
instance Gf GAnswer where
|
||||
gf GYes = appqc "Math" "Yes" []
|
||||
gf GNo = appqc "Math" "No" []
|
||||
|
||||
instance Gf GObject where gf (GNumber x1) = appqc "Math" "Number" [gf x1]
|
||||
|
||||
instance Gf GQuestion where
|
||||
gf (GPrime x1) = appqc "Math" "Prime" [gf x1]
|
||||
gf (GOdd x1) = appqc "Math" "Odd" [gf x1]
|
||||
gf (GEven x1) = appqc "Math" "Even" [gf x1]
|
||||
|
||||
|
||||
instance Fg GAnswer where
|
||||
fg t =
|
||||
case termForm t of
|
||||
Ok ([], Q (IC "Math") (IC "Yes"),[]) -> GYes
|
||||
Ok ([], Q (IC "Math") (IC "No"),[]) -> GNo
|
||||
_ -> error ("no Answer " ++ prt t)
|
||||
|
||||
instance Fg GObject where
|
||||
fg t =
|
||||
case termForm t of
|
||||
Ok ([], Q (IC "Math") (IC "Number"),[x1]) -> GNumber (fg x1)
|
||||
_ -> error ("no Object " ++ prt t)
|
||||
|
||||
instance Fg GQuestion where
|
||||
fg t =
|
||||
case termForm t of
|
||||
Ok ([], Q (IC "Math") (IC "Prime"),[x1]) -> GPrime (fg x1)
|
||||
Ok ([], Q (IC "Math") (IC "Odd"),[x1]) -> GOdd (fg x1)
|
||||
Ok ([], Q (IC "Math") (IC "Even"),[x1]) -> GEven (fg x1)
|
||||
_ -> error ("no Question " ++ prt t)
|
||||
|
||||
|
||||
38
examples/tutorial/embedded/haskell/Run.hs
Normal file
38
examples/tutorial/embedded/haskell/Run.hs
Normal file
@@ -0,0 +1,38 @@
|
||||
module Main where
|
||||
|
||||
import GSyntax
|
||||
import GF.Embed.EmbedAPI
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
gr <- file2grammar "math.gfcm"
|
||||
loop gr
|
||||
|
||||
loop :: MultiGrammar -> IO ()
|
||||
loop gr = do
|
||||
s <- getLine
|
||||
interpret gr s
|
||||
loop gr
|
||||
|
||||
interpret :: MultiGrammar -> String -> IO ()
|
||||
interpret gr s = do
|
||||
let ltss = parseAllLang gr "Question" s
|
||||
case ltss of
|
||||
[] -> putStrLn "no parse"
|
||||
(l,t:_):_ -> putStrLn $ linearize gr l $ gf $ answer $ fg t
|
||||
|
||||
answer :: GQuestion -> GAnswer
|
||||
answer p = case p of
|
||||
GOdd x -> test odd x
|
||||
GEven x -> test even x
|
||||
GPrime x -> test prime x
|
||||
|
||||
value :: GObject -> Int
|
||||
value e = case e of
|
||||
GNumber (GInt i) -> fromInteger i
|
||||
|
||||
test :: (Int -> Bool) -> GObject -> GAnswer
|
||||
test f x = if f (value x) then GYes else GNo
|
||||
|
||||
prime :: Int -> Bool
|
||||
prime = (< 8) ----
|
||||
Reference in New Issue
Block a user