Permalink
Browse files

Redid parsing; need to add Return as a keyword to avoid ambiguous parse

  • Loading branch information...
Daniel Tahara
Daniel Tahara committed May 1, 2012
1 parent 95688ca commit f2ddada92a004705a1436bcc7d60c1162b8609a2
Showing with 82 additions and 65 deletions.
  1. +9 −9 DTKV_Conversion_Test.hs
  2. +0 −15 DTKV_Implementation.hs
  3. +1 −1 DTKV_Test.hs
  4. +35 −29 Recognition.hs
  5. +37 −11 Recognition_Test.hs
View
@@ -5,13 +5,13 @@ import Data.Maybe
import DTKV_Conversion
import Recognition_Test
-convertedProgs = map toProg $ map fromJust parseTrees
+convertedProgs = map toProg parseTrees
-converted_assignment = toProg $ fromJust parseTree_assignment -- 15
-converted_fnCall = toProg $ fromJust parseTree_fnCall -- 35
-converted_nestedCalls = toProg $ fromJust parseTree_nestedCalls -- 36
-converted_arithFnDecl = toProg $ fromJust parseTree_arithFnDecl -- 36
-converted_forLoop = toProg $ fromJust parseTree_forLoop -- 55
-converted_whileLoop = toProg $ fromJust parseTree_whileLoop -- -160
-converted_forLoopInFn = toProg $ fromJust parseTree_forLoopInFn -- -9
-converted_stringUserFn = toProg $ fromJust parseTree_stringUserFn -- hi
+converted_assignment = toProg parseTree_assignment -- 15
+converted_fnCall = toProg parseTree_fnCall -- 35
+converted_nestedCalls = toProg parseTree_nestedCalls -- 36
+converted_arithFnDecl = toProg parseTree_arithFnDecl -- 36
+converted_forLoop = toProg parseTree_forLoop -- 55
+converted_whileLoop = toProg parseTree_whileLoop -- -160
+converted_forLoopInFn = toProg parseTree_forLoopInFn -- -9
+converted_stringUserFn = toProg parseTree_stringUserFn -- hi
View
@@ -36,21 +36,6 @@ data Loop = For Expression Expression [Statement]
data Conditional = IfElse Expression [Statement] [Statement] deriving Show
-- if second list is empty, no else
-
--------------------------------------------------------------------------------
--- Conversion code from Tree Structure to Grammar
-
-{-
-toProgram :: Tree Word Category -> Program
-
-toStatement :: Tree Word Category -> Statement
-
-toExpression :: Tree Word Category -> Expression
-
-toAtom :: Tree Word Category -> Atom
-
-toFunction :: Tree Word Category -> Function
--}
-------------------------------------------------------------------------------
-- Functions to actually evaluate the statements
View
@@ -15,7 +15,7 @@ expected_outputs = [IntAtom 15,
IntAtom 55,
IntAtom (-160),
IntAtom (-9),
- StrAtom "hi"]
+ StrAtom "shouldbeleftafterfnhi"]
test_results = map (\x -> fst x == snd x) (zip outputs expected_outputs)
View
@@ -11,7 +11,8 @@ import DTKV_Grammar
data Tree a b = Leaf a | Node b [Tree a b] deriving (Show, Eq)
--------------------------------------------------------------------------
-type RecogItem = (Category, Int, Int, Word)
+type RecogItem = (Category, Int, Int, ChildData)
+type ChildData = (Int, String, String)
getBounds :: RecogItem -> (Int, Int)
getBounds (_,min,max,_) = (min, max)
@@ -22,21 +23,30 @@ getUpperBound = snd . getBounds
getCategory :: RecogItem -> Category
getCategory (c,_,_,_) = c
-getWord :: RecogItem -> Word
-getWord (_,_,_,w) = w
+getChildData :: RecogItem -> ChildData
+getChildData (_,_,_,d) = d
+
+getMidIndex :: ChildData -> Int
+getMidIndex (x,c1,c2) = x
+
+getCat1 :: ChildData -> String
+getCat1 (x,c1,c2) = c1
+
+getCat2 :: ChildData -> String
+getCat2 (x,c1,c2) = c2
preprocess :: Grammar -> [String] -> [RecogItem]
preprocess g strs = concatMap (getTermCategory g) (zip [0..] strs)
getTermCategory :: Grammar -> (Int, Word) -> [RecogItem]
getTermCategory (grs,c) (index, w1)
- | all isDigit w1 = [("INTEGER", index, index + 1, w1)]
+ | all isDigit w1 = [("INTEGER", index, index + 1, (index + 1,w1,""))]
-- | head w1 == '-' && all isDigit $ tail w1
-- = [("INTEGER", index, index + 1)]
-- negative numbers?
| otherwise =
- if cats == [] then [("STRING", index, index + 1, w1)] else cats
- where cats = [(c, index, index + 1, w1) |
+ if cats == [] then [("STRING", index, index + 1, (index + 1,w1,""))] else cats
+ where cats = [(c, index, index + 1, (index + 1,w1,"")) |
TerminatingRule c w2 <- grs, w1 == w2]
-- if all isDigit string, then "Integer"
@@ -50,7 +60,8 @@ convertToBinRule :: Category -> Category -> Category
-> (RecogItem -> RecogItem -> [RecogItem])
convertToBinRule c1 c2 c3 = \r1 r2 -> if getCategory r1 == c2
&& getCategory r2 == c3 && boundsMatch r1 r2
- then [(c1, fst (getBounds r1), snd (getBounds r2), getWord r1 ++ " " ++ getWord r2)] else []
+ then [(c1, fst (getBounds r1), snd (getBounds r2),
+ (snd $ getBounds r1, getCategory r1, getCategory r2))] else []
convertToBinaryGrammarRule :: RecogItem -> RecogItem -> RecogItem -> GrammarRule
convertToBinaryGrammarRule r1 r2 r3 = NonTerminatingBinaryRule (getCategory r1) (getCategory r2) (getCategory r3)
@@ -62,7 +73,8 @@ unaryRules (grs, c) =
convertToUnaryRule :: Category -> Category -> (RecogItem -> [RecogItem])
convertToUnaryRule c1 c2 =
\r1 -> if getCategory r1 == c2
- then [(c1, fst (getBounds r1), snd (getBounds r1), getWord r1)] else []
+ then [(c1, fst $ getBounds r1, snd $ getBounds r1,
+ (snd $ getBounds r1, getCat1 . getChildData $ r1, getCat2 . getChildData $ r1))] else []
convertToUnaryGrammarRule :: RecogItem -> RecogItem -> GrammarRule
convertToUnaryGrammarRule r1 r2 = NonTerminatingUnaryRule (getCategory r1) (getCategory r2)
@@ -85,27 +97,20 @@ recognize g@(grs, cat) s = not . null $ validParses g s
-- TopDownParse Tree
-- Idea: Take all items, and then recreate:
-getParseTree :: Grammar -> RecogItem -> [RecogItem] -> Maybe (Tree Word Category)
-getParseTree g ri@(c, lb, ub, w) ris
- | isTerminatingCategory g c = Just $ Node c [Leaf w]
- | null matches = Nothing
- | otherwise = Just $ Node c $ process $ map getSubtrees $ matches
- -- generate list of pairs and filter out ones with nothing
- where matches = matchingConstituents g ri ris -- [[Recog Item]]
- process = head . map (map fromJust) . filter (all isJust) --
- -- should only be left with one
- getSubtrees = (map (\r -> getParseTree g r ris)) -- [[Maybe Tree]]
-
-matchingConstituents :: Grammar -> RecogItem -> [RecogItem] -> [[RecogItem]]
-matchingConstituents g r rls =
- [[x,y] | x <- pool, y <- pool, boundsMatch x y, convertToBinaryGrammarRule r x y `elem` fst g] ++
- [[x] | x <- pool, convertToUnaryGrammarRule r x `elem` fst g, getBounds x == getBounds r]
- where pool = constituentsMeetingBounds g r rls
-
-constituentsMeetingBounds :: Grammar -> RecogItem -> [RecogItem] -> [RecogItem]
-constituentsMeetingBounds g r rls = [x | x <- rls, lowerBoundsMatch r x || upperBoundsMatch r x, r /= x]
- --filter (\r -> r `elem` unaryRules g || r `elem` binaryRules g) correctBounds
- --where correctBounds = [x | x <- rls, lowerBoundsMatch r x || upperBoundsMatch r x && not (r == x)]
+getParseTree g ri ris = head $ getParseTrees g ri ris
+
+getParseTrees :: Grammar -> RecogItem -> [RecogItem] -> [Tree Word Category]
+getParseTrees g@(grs, re) ri@(cat, lb, ub, (i, c1, c2)) ris
+ | isTerminatingCategory g cat = [Node cat [Leaf c1]]
+ | ub == i = map (Node cat) [getParseTrees g r ris | r@(c, l, u, d) <- ris,
+ (lb, ub) == (l,u), NonTerminatingUnaryRule cat c `elem` grs]-- unary rule
+ | otherwise = map (Node cat) $ concatMap crossProduct pairs
+ --[[m,n] | m <- getParseTrees g x ris, n <- getParseTrees g y ris | [x,y] <- pairs]
+ where pairs = [(getParseTrees g r1 ris, getParseTrees g r2 ris) | r1@(c1,l1,u1,d1) <- ris, r2@(c2,l2,u2,d2) <- ris,
+ (l1,u1) == (lb,i), (l2,u2) == (i,ub), NonTerminatingBinaryRule cat c1 c2 `elem` grs] -- binary rules (lb, i), (i, ub)
+
+-- crossProduct :: ([a],[a]) -> [[a,a]]
+crossProduct (x,y) = [[m,n] | m <- x, n <- y]
lowerBoundsMatch :: RecogItem -> RecogItem -> Bool
lowerBoundsMatch r1 r2 = getLowerBound r1 == getLowerBound r2
@@ -127,6 +132,7 @@ isTerminatingCategory g cat = not . null $ getRulesOfCat_Terminating g cat
getRulesOfCat_Terminating :: Grammar -> Category -> [GrammarRule]
getRulesOfCat_Terminating g cat = [r | r@(TerminatingRule c w) <- fst g, c == cat]
+
{-
-- if it's a terminating category, just find it with right bounds and done
View
@@ -105,8 +105,18 @@ test_noStms_items = getAllItems dtkvGrammar (words testCode_noStms)
-- Test Building Parse Tree
-- Test helper functions
-bounds = constituentsMeetingBounds dtkvGrammar recogItem_fnCall test_fnCall_items
-matches = matchingConstituents dtkvGrammar recogItem_fnCall test_fnCall_items
+--bounds = constituentsMeetingBounds dtkvGrammar recogItem_fnCall test_fnCall_items
+--matches = matchingConstituents dtkvGrammar recogItem_fnCall test_fnCall_items
+
+-- Valid Parses
+validParsesList = [validParses_assignment,
+ validParses_fnCall,
+ validParses_nestedCalls,
+ validParses_arithFnDecl,
+ validParses_forLoop,
+ validParses_whileLoop,
+ validParses_forLoopInFn,
+ validParses_stringUserFn]
-- Resulting parseTrees
parseTrees = [parseTree_assignment,
@@ -118,28 +128,44 @@ parseTrees = [parseTree_assignment,
parseTree_forLoopInFn,
parseTree_stringUserFn]
-recogItem_assignment = head $ validParses dtkvGrammar testCode_assignment
+validParses_assignment = validParses dtkvGrammar testCode_assignment
+recogItem_assignment = head validParses_assignment
parseTree_assignment = getParseTree dtkvGrammar recogItem_assignment test_assignment_items
+--filter (checkTree dtkvGrammar "PROG") $
+parseTrees_assignment = getParseTrees dtkvGrammar recogItem_assignment test_assignment_items
-recogItem_fnCall = head $ validParses dtkvGrammar testCode_fnCall
+validParses_fnCall = validParses dtkvGrammar testCode_fnCall
+recogItem_fnCall = head validParses_fnCall
parseTree_fnCall = getParseTree dtkvGrammar recogItem_fnCall test_fnCall_items
+parseTrees_fnCall = getParseTrees dtkvGrammar recogItem_fnCall test_fnCall_items
-recogItem_nestedCalls = head $ validParses dtkvGrammar testCode_nestedCalls
+validParses_nestedCalls = validParses dtkvGrammar testCode_nestedCalls
+recogItem_nestedCalls = head validParses_nestedCalls
parseTree_nestedCalls = getParseTree dtkvGrammar recogItem_nestedCalls test_nestedCalls_items
+parseTrees_nestedCalls = getParseTrees dtkvGrammar recogItem_nestedCalls test_nestedCalls_items
-recogItem_arithFnDecl = head $ validParses dtkvGrammar testCode_arithFnDecl
+validParses_arithFnDecl = validParses dtkvGrammar testCode_arithFnDecl
+recogItem_arithFnDecl = head validParses_arithFnDecl
parseTree_arithFnDecl = getParseTree dtkvGrammar recogItem_arithFnDecl test_arithFnDecl_items
+parseTrees_arithFnDecl = getParseTrees dtkvGrammar recogItem_arithFnDecl test_arithFnDecl_items
-recogItem_forLoop = head $ validParses dtkvGrammar testCode_forLoop
+validParses_forLoop = validParses dtkvGrammar testCode_forLoop
+recogItem_forLoop = head validParses_forLoop
parseTree_forLoop = getParseTree dtkvGrammar recogItem_forLoop test_forLoop_items
+parseTrees_forLoop = getParseTrees dtkvGrammar recogItem_forLoop test_forLoop_items
-recogItem_whileLoop = head $ validParses dtkvGrammar testCode_whileLoop
+validParses_whileLoop = validParses dtkvGrammar testCode_whileLoop
+recogItem_whileLoop = head validParses_whileLoop
parseTree_whileLoop = getParseTree dtkvGrammar recogItem_whileLoop test_whileLoop_items
+parseTrees_whileLoop = getParseTrees dtkvGrammar recogItem_whileLoop test_whileLoop_items
-recogItem_forLoopInFn = head $ validParses dtkvGrammar testCode_forLoopInFn
+validParses_forLoopInFn = validParses dtkvGrammar testCode_forLoopInFn
+recogItem_forLoopInFn = head validParses_forLoopInFn
parseTree_forLoopInFn = getParseTree dtkvGrammar recogItem_forLoopInFn test_forLoopInFn_items
+parseTrees_forLoopInFn = getParseTrees dtkvGrammar recogItem_forLoopInFn test_forLoopInFn_items
-recogItem_stringUserFn = head $ validParses dtkvGrammar testCode_stringUserFn
+validParses_stringUserFn = validParses dtkvGrammar testCode_stringUserFn
+recogItem_stringUserFn = head validParses_stringUserFn
parseTree_stringUserFn = getParseTree dtkvGrammar recogItem_stringUserFn test_stringUserFn_items
-
+parseTrees_stringUserFn = getParseTrees dtkvGrammar recogItem_stringUserFn test_stringUserFn_items

0 comments on commit f2ddada

Please sign in to comment.