Skip to content

Commit a359f47

Browse files
committed
Updated parser for lexical analysis
1 parent 023ee6c commit a359f47

File tree

1 file changed

+87
-14
lines changed

1 file changed

+87
-14
lines changed

Compiler/myparser.hs

Lines changed: 87 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,54 @@
33
import Text.Regex.Posix
44
import Data.Typeable
55
import Data.Data
6+
import Data.String.Utils as Utils
67

78
-- Basic Functions
89

9-
--Custom Split Function, for blank space
10-
split :: String -> [String]
11-
split "" = []
12-
split str = (firstWord str):(split (nextWords str))
10+
--Custom split Function, for blank space
11+
isBlankSpace :: Char -> Bool
12+
isBlankSpace ch
13+
| ch == ' ' = True
14+
| ch == '\n' = True
15+
| ch == '\t' = True
16+
| otherwise = False
17+
18+
splitthis :: String -> [String]
19+
splitthis "" = []
20+
splitthis str = (firstWord str):(splitthis (nextWords str))
1321

1422
firstWord :: String -> String
1523
firstWord "" = ""
1624
firstWord str
17-
| head str == ' ' = ""
25+
| isBlankSpace $ head str = ""
1826
| otherwise = (head str):(firstWord (tail str))
1927

2028
nextWords :: String -> String
2129
nextWords "" = ""
2230
nextWords str
23-
| head str == ' ' = skipAllSpaces (tail str)
31+
| isBlankSpace $ head str = skipAllSpaces (tail str)
2432
| otherwise = (nextWords (tail str))
2533

2634
skipAllSpaces :: String -> String
2735
skipAllSpaces "" = ""
2836
skipAllSpaces str
29-
| (head str) == ' ' = skipAllSpaces (tail str)
37+
| isBlankSpace $ head str = skipAllSpaces (tail str)
3038
| otherwise = str
3139

3240
--Allowed Char
3341

3442
data Token =
3543
Invalid {str :: String}
3644
| DInt {val :: Integer}
45+
| DChar {valc :: Char}
46+
| Dopa {op :: String}
47+
| Dopb {op :: String}
48+
| Dopr {op :: String}
49+
| DReserved {word :: String}
50+
| DType {datatype :: String}
3751
| DIdentifier {name :: String}
3852
deriving (Show,Eq,Typeable,Data)
3953
data Tokens = Tokens {list :: [Token]} deriving (Show)
40-
--newtype Parser tokenType = Parser { parse :: String -> (Bool,[(tokenType,String)]) }
4154

4255
sameType x y = (toConstr x) == (toConstr y)
4356
diffType x y = (toConstr x) /= (toConstr y)
@@ -48,25 +61,51 @@ isValidToken x = diffType x (Invalid "")
4861
isInvalidToken :: Token -> Bool
4962
isInvalidToken x = sameType x (Invalid "")
5063

51-
--Parser DInt
64+
checkReserved :: String -> Token
65+
checkReserved input
66+
| input =~ "^((:=)|(skip)|;|(if)|(then)|(else)|(while))$" :: Bool = DReserved input
67+
| otherwise = Invalid input
68+
69+
checkDataType :: String -> Token
70+
checkDataType input
71+
| input =~ "^((char)|(int))$" :: Bool = DType input
72+
| otherwise = Invalid input
73+
5274
checkInt :: String -> Token
5375
checkInt input
5476
| input =~ "^[0-9]+$" :: Bool = DInt (read input :: Integer)
5577
| otherwise = Invalid input
5678

79+
checkChar :: String -> Token
80+
checkChar input
81+
| input =~ "^'.'$" :: Bool = DChar (input !! 1)
82+
| otherwise = Invalid input
83+
84+
checkOperator :: String -> Token
85+
checkOperator input
86+
| input =~ "^((\\+)|(\\-)|(\\*)|(\\/))$" :: Bool = Dopa input
87+
| input =~ "^((and)|(or)|(not))$" :: Bool = Dopb input
88+
| input =~ "^(([<>]=?)|(==)|(!=))$" :: Bool = Dopr input
89+
| otherwise = Invalid input
90+
5791
checkIdentifier :: String -> Token
5892
checkIdentifier input
5993
| input =~ "^[A-Za-z_][A-Za-z_0-9]*$" :: Bool = DIdentifier input
6094
| otherwise = Invalid input
6195

6296
getToken :: String -> Token
6397
getToken x
98+
| isValidToken (checkReserved x) = checkReserved x
99+
| isValidToken (checkOperator x) = checkOperator x
100+
| isValidToken (checkDataType x) = checkDataType x
64101
| isValidToken (checkInt x) = checkInt x
102+
| isValidToken (checkChar x) = checkChar x
65103
| isValidToken (checkIdentifier x) = checkIdentifier x
66104
| otherwise = Invalid x
67105

106+
68107
makeExpression :: String -> Tokens
69-
makeExpression input = Tokens (map getToken (split input))
108+
makeExpression input = Tokens (map getToken (splitthis input))
70109

71110
getTokenList :: Tokens -> [Token]
72111
getTokenList (Tokens list) = list
@@ -87,11 +126,45 @@ printTokens tokens = do
87126
error "Invalid Token Found!!"
88127
return 0;
89128

129+
-- In bytes
130+
getSize :: Token -> Integer
131+
getSize x
132+
| x == checkDataType("int") = 4
133+
| x == checkDataType("char") = 1
134+
| otherwise = error "Invalid DataType"
135+
136+
-- Symbol Table
137+
data SymbolRow =
138+
SymbolRow {ident :: Token, itype :: Token, size :: Integer}
139+
140+
instance Show SymbolRow where
141+
show (SymbolRow (DIdentifier ident) (DType itype) size) = "{ "++ident ++", "++ itype ++", "++ show(size)++"}\n"
142+
143+
144+
cleanInput :: String -> String
145+
cleanInput str = Utils.replace ";" " ; " str
146+
147+
-- DataType, rest of Token, out is SymbolTable
148+
makeSymbolTable :: Token -> [Token] -> SymbolRow
149+
makeSymbolTable token tokens
150+
| (sameType (head tokens) (DIdentifier "x")) && ((tokens !! 1) == checkReserved ";" ) = SymbolRow (head tokens) (token) (getSize token)
151+
| otherwise = error "Error! On Variable Declaration"
152+
153+
getSymbolTable :: [Token] -> [SymbolRow]
154+
getSymbolTable [] = []
155+
getSymbolTable tokens
156+
| sameType (head tokens) (DType " ") = (makeSymbolTable (head tokens) (tail tokens)) : (getSymbolTable (tail tokens))
157+
| otherwise = getSymbolTable (tail tokens)
158+
90159
main :: IO ()
91160
main = do
92-
putStr "Enter an expression\n"
93-
a <- getLine
94-
let tokens_collection = (makeExpression a)
161+
putStr "Enter filename\n"
162+
filename <- getLine
163+
a <- readFile filename
164+
let tokens_collection = (makeExpression $ cleanInput a)
95165
let invalid_tokens = getInvalidTokens tokens_collection
96166
printTokens (getTokenList invalid_tokens)
97-
putStr $ (show(tokens_collection) ++ "\n")
167+
putStr $ (show(tokens_collection) ++ "\n")
168+
putStr $ "\nSymbol Table\n===========================\n\n"
169+
putStr $ show $ getSymbolTable $ getTokenList tokens_collection
170+
putStr $ "\n===========================\n\n"

0 commit comments

Comments
 (0)