Skip to content
Snippets Groups Projects
Commit 7583ee9c authored by pm3g19's avatar pm3g19
Browse files

First problem works

parent 934a4993
No related branches found
No related tags found
No related merge requests found
...@@ -9,14 +9,14 @@ import Parser ...@@ -9,14 +9,14 @@ import Parser
import Debug.Trace import Debug.Trace
parse :: String -> Program parse :: String -> Program
parse = parseSource.alexScanTokens parse = parseSource.(\x -> let o = alexScanTokens x in traceShowId o)
--outline --outline
main = do main = do
args <- getArgs args <- getArgs
case args of case args of
(srcname:_) -> interpret srcname (srcname:_) -> interpret srcname
_ -> interpret "q1.txt" _ -> interpret "solutions/pr1.cql"
interpret :: FilePath -> IO () -- the main function, takes in file name, prints out result interpret :: FilePath -> IO () -- the main function, takes in file name, prints out result
......
...@@ -2,40 +2,60 @@ ...@@ -2,40 +2,60 @@
module Parser where module Parser where
import Lexer import Lexer
import Types import Types
import CSV
} }
%name parseSource %name parseSource
%tokentype {Token} %tokentype {Token}
%error {parseError} %error {parseError}
--\\map(\r -> r[1,2,3])
%token %token
filter { TokenFilter } filter { TokenFilter _ }
in { TokenInSet } in { TokenInSet _ }
out { TokenOutSet } out { TokenOutSet _ }
SetName { TokenSetName $$ } SetName { TokenSetName _ $$ }
Nat { TokenNat $$ } Nat { TokenNat _ $$ }
VarName { TokenVarName $$ } PosNat { TokenPosNat _ $$ }
true { TokenTrue } VarName { TokenVarName _ $$ }
false { TokenFalse } true { TokenTrue _ }
Str { TokenString $$ } false { TokenFalse _ }
'[' { TokenLeftSqBracket } Str { TokenString _ $$ }
']' { TokenRightSqBracket } '[' { TokenLeftSqBracket _ }
"->" { TokenArrow } ']' { TokenRightSqBracket _ }
"==" { TokenisEqual } "->" { TokenArrow _ }
"/=" { TokenisNotEqual } "==" { TokenisEqual _ }
'(' { TokenLeftBracket } "/=" { TokenisNotEqual _ }
')' { TokenRightBracket } '(' { TokenLeftBracket _ }
';' { TokenSemiCol } ')' { TokenRightBracket _ }
'\\' { TokenLambda } ';' { TokenSemiCol _ }
',' { TokenComma } ':' { TokenCol _ }
'.' { TokenFullStop } '\\' { TokenLambda _ }
x { TokenXProduct } ',' { TokenComma _ }
'.' { TokenFullStop _ }
'+' { TokenPlus _ }
x { TokenXProduct _ }
map { TokenMap _ }
xx { TokenXXProduct _ }
-- mapr { TokenMapr _ }
-- '!' { TokenNot _ }
-- zip { TokenZip _ }
contains { TokenContains _ }
-- isSubstring { TokenIsSubstring _ }
isEmpty { TokenIsEmpty _ }
%right "->" %right "->"
%left "/=" "==" ';' %left "/=" "==" ';'
%right '+'
%% %%
Prog : in SetNames out SetFuncCalls {($2,$4)} Prog : in SetDecls out SetFuncCalls {($2,$4)}
SetDecl : SetName':'PosNat {$1}
SetDecls : SetDecl {[$1]}
| SetDecls','SetDecl {$3:$1}
SetNames : SetName {[$1]} SetNames : SetName {[$1]}
| SetName ',' SetNames { $1:$3 } | SetName ',' SetNames { $1:$3 }
...@@ -44,12 +64,13 @@ SetNames : SetName {[$1]} ...@@ -44,12 +64,13 @@ SetNames : SetName {[$1]}
VarNames : VarName {[$1]} VarNames : VarName {[$1]}
| VarName ',' VarNames {$1:$3} | VarName ',' VarNames {$1:$3}
SetFuncCalls : SetFuncCall {[$1]} SetFuncCalls : SetFuncCall';' {[$1]}
| SetFuncCall';' SetFuncCalls {$1:$3} | SetFuncCall';' SetFuncCalls {$1:$3}
SetFuncCall : filter '['SetName']' '('Func')' {FuncCall (PredefFunc Filter) [Var $3] [$6]} SetFuncCall : filter '['SetName']' '('Func')' {FuncCall (PredefFunc Filter) [Var $3] [$6]}
| filter '('Func')' {FuncCall (PredefFunc Filter) [] [$3]} | filter '('Func')' {FuncCall (PredefFunc Filter) [] [$3]}
--| map '['SetName']' '('Func')' {FuncCall (PredefFunc Map) [Var $3] [$6]}
| map '('Func')' {FuncCall (PredefFunc Map) [] [$3]}
| SetName x SetName {FuncCall (PredefFunc XProduct) (map Var [$1, $3]) []} | SetName x SetName {FuncCall (PredefFunc XProduct) (map Var [$1, $3]) []}
Func : '\\' '(' VarNames ')' "->" Expr { FuncDef [] $3 $6 } Func : '\\' '(' VarNames ')' "->" Expr { FuncDef [] $3 $6 }
...@@ -57,18 +78,39 @@ Func : '\\' '(' VarNames ')' "->" Expr {FuncDef [] $3 $6} ...@@ -57,18 +78,39 @@ Func : '\\' '(' VarNames ')' "->" Expr {FuncDef [] $3 $6}
Expr : Expr "==" Expr {FuncCall (PredefFunc IsEqual) [] [$1, $3]} Expr : Expr "==" Expr {FuncCall (PredefFunc IsEqual) [] [$1, $3]}
| Expr'['Nat']' {FuncCall (PredefFunc RecordIndex) [] [$1, Types.Int $3]} | Expr'['Nat']' {FuncCall (PredefFunc RecordIndex) [] [$1, Types.Int $3]}
| Str {Types.String $1} | Expr'['Nat','Nats']' {FuncCall (PredefFunc RecordSelect) [] ($1:(map Types.Int ($3:$5))) }
| Function'('Exprs')' {FuncCall $1 [] $3}
| Str {Types.String $ stripWhitespace $1}
| VarName {Var $1} | VarName {Var $1}
| Record {$1} | Record {$1}
| true {Boolean True} | true {Boolean True}
| false {Boolean False} | false {Boolean False}
--TODO brackets
Function : PredefFunc {PredefFunc $1}
| VarName { Var $1}
PredefFunc : isEmpty {IsEmpty}
-- | isSubstring {IsSubstring}
| contains {Contains}
-- | zip {Zip}
-- | Map
-- | Mapr
Record : '['Exprs']' {Record $2} Record : '['Exprs']' {Record $2}
Exprs : Expr {[$1]} Exprs : Expr {[$1]}
| Expr','Exprs {$1:$3} | Expr','Exprs {$1:$3}
Nats : Nat {[$1]}
| Nat ',' Nats {$1:$3}
{ {
parseError :: [Token] -> a parseError :: [Token] -> a
parseError _ = error "Parse error" parseError tokens = error $ "Parse error: " ++ (show.pos.head) tokens
tokenPosn :: Token -> String
tokenPosn t = let (AlexPn _ line col) = pos t in (show line) ++ ':' : (show col)
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment