Commit bd9a889d authored by Tadej Borovšak's avatar Tadej Borovšak
Browse files

Start of homework

parent 9376e0e6
module Imp_AbsSyntax where
-- Haskell datatypes representing abstract syntax of expressions and commands
-- arithmetic expressions
data AExp =
Loc String |
......@@ -23,5 +23,3 @@ data Com =
Skip |
While (BExp, Com)
deriving Show
module Imp_Evaluator where
import Imp_AbsSyntax
import Imp_State
......
......@@ -4,11 +4,11 @@ import Imp_Parse
import Imp_State
import Imp_Evaluator
-- runImp "file.imp" s
-- runImp "file.imp" s
--
-- lexes and parses the Imp program text in file.imp and then
-- executes it starting in state s
runImp filename s = do
progtext <- readFile filename
let lexed = imp_lex progtext
......
-- vim: set syntax=alex autoindent
{module Imp_Lex where }
......@@ -6,33 +7,37 @@
-- character classes
$digit = [0-9]
$lower = [a-z]
$upper = [A-Z]
$alpha = [A-Za-z]
:-
-- lexical classes
--
-- [[IMPLEMENT YOUR LEXICAL CLASSES HERE]]
--
$white+ ; -- whitespace
\( | \) {\s -> SCOPE s} -- scope
\; {\s -> DELIM} -- command delimiter
\+ | \- | \* {\s -> OP s} -- arithmetic ops
\=\= | \< {\s -> OP s} -- boolean ops
\:\= {\s -> ASSIGN} -- assignment
if | then | else {\s -> KEYWORD s} -- if command
while | do {\s -> KEYWORD s} -- while command
skip {\s -> KEYWORD s} -- skip command
True | False {\s -> BOOL $ read s} -- boolean literals
$digit+ {\s -> NUM $ read s} -- numbers
$alpha [$alpha $digit \_]* {\s -> LOC s} -- locations
{
-- The Token type - the action for each lexical class has type String -> Token
data Token =
--
-- [[IMPLEMENT YOUR Token TYPE HERE]]
--
deriving Show
-- The lexer implements a function
--
-- imp_lex :: String -> [Token]
--
-- which converts a string containing Imp program text into a list of tokens
data Token = SCOPE String
| DELIM
| OP String
| ASSIGN
| KEYWORD String
| BOOL Bool
| NUM Integer
| LOC String
| INVALID
deriving Show
imp_lex = alexScanTokens
......
-- vim: set syntax=happy
{module Imp_Parse where
import Imp_Lex
import Imp_AbsSyntax
......@@ -9,7 +11,7 @@ import Imp_AbsSyntax
--
-- that converts a list of tokens to the abstract syntax tree of a command
%name imp_parse Com
%name imp_parse Com
%tokentype { Token }
%error { parseError }
......@@ -26,7 +28,7 @@ import Imp_AbsSyntax
--
-- [[MAKE PRECEDENCE AND ASSOCIATIVITY DECLARATIONS HERE]]
--
%%
-- the grammar
......@@ -34,9 +36,9 @@ import Imp_AbsSyntax
--
-- [[DEFINE YOUR GRAMMAR AND ITS ACTIONS HERE]]
--
{
parseError :: [Token] -> a
parseError _ = error "Imp parse error"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment