Created
January 24, 2018 16:04
-
-
Save TakashiHarada/6b5875d29a15a8f4bfa909fd70e7ba61 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Parser | |
import BNF2Props | |
import System.IO | |
import System.Environment | |
import Data.Maybe | |
import Data.List | |
main = | |
putStr "Input a grammar file: " >> -- grammar.txt | |
getLine >>= \filename -> | |
openFile filename ReadMode >>= \handle -> | |
hGetContents handle >>= \file -> | |
let bnf = (fst . fromJust . parse syntax') file ; cs = (fst . fromJust . parse chars) file in | |
-- mapM_ print bnf >> | |
-- mapM_ print cs >> | |
mapM_ print (nub $ concatMap (\rule -> rule2clauses' rule cs) bnf) >> | |
-- print (length $ nub $ concatMap (\rule -> rule2clauses' rule cs) bnf) >> | |
hClose handle | |
rule2clauses' :: (String,[String]) -> [Char] -> [Clause] | |
rule2clauses' (_,[]) _ = [] | |
rule2clauses' (s,[t]) cs = rule2clauses s t cs | |
rule2clauses' (s,(t:ts)) cs = rule2clauses s t cs ++ rule2clauses' (s,ts) cs | |
{- grammar.txt | |
<expression> ::= <term> | <expression> '+' <term> | <expression> '-' <term> | |
<term> ::= <factor> | '-' <factor> | <term> '*' <factor> | <term> '/' <factor> | |
<factor> ::= <variable> | <constant> | '(' <expression> ')' | |
<variable> ::= <letter> | <variable> <letter> | <variable> <digit> | |
<letter> ::= 'a' | 'b' | 'c' | |
<constant> ::= <digit> | <constant> <digit> | |
<digit> ::= '0' | '1' | |
-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment