Skip to content
Snippets Groups Projects
Commit 974e95a5 authored by ik1g19's avatar ik1g19
Browse files

add task 3

parent 41d93e19
No related branches found
No related tags found
No related merge requests found
Showing
with 4143 additions and 0 deletions
File moved
File moved
File moved
File moved
File moved
File moved
File moved
File moved
File moved
File moved
File moved
File moved
File moved
File moved
module Main where
import Tokens
import System.Environment
import Control.Exception
import System.IO
main :: IO ()
main = catch main' noLex
main' = do (fileName : _) <- getArgs
sourceText <- readFile fileName
putStrLn $ "Lexing: " ++ sourceText
let lexedProg = alexScanTokens sourceText
putStrLn $ "Lexed as: " ++ (show lexedProg)
noLex :: ErrorCall -> IO ()
noLex e = do let err = show e
hPutStrLn stderr ("Problem with lexing :" ++ err)
return ()
\ No newline at end of file
This diff is collapsed.
{
module Tokens where
}
%wrapper "posn"
$digit = 0-9
-- digits
$alpha = [a-zA-Z]
-- alphabetic characters
tokens :-
$white+ ;
"--".* ;
let { \p s -> TokenLet p}
in { \p s -> TokenIn p}
$digit+ { \p s -> TokenInt p (read s) }
\= { \p s -> TokenEq p}
\+ { \p s -> TokenPlus p}
\- { \p s -> TokenMinus p}
\* { \p s -> TokenTimes p}
\/ { \p s -> TokenDiv p}
\( { \p s -> TokenLParen p}
\) { \p s -> TokenRParen p}
\^ { \p s -> TokenExp p}
$alpha [$alpha $digit \_ \’]* { \p s -> TokenVar p s }
{
-- Each action has type :: AlexPosn -> String -> Token
-- The token type:
data Token =
TokenLet AlexPosn |
TokenIn AlexPosn |
TokenInt AlexPosn Int |
TokenVar AlexPosn String |
TokenEq AlexPosn |
TokenPlus AlexPosn |
TokenMinus AlexPosn |
TokenTimes AlexPosn |
TokenDiv AlexPosn |
TokenExp AlexPosn |
TokenLParen AlexPosn |
TokenRParen AlexPosn
deriving (Eq,Show)
tokenPosn :: Token -> String
tokenPosn (TokenLet (AlexPn _ l c)) = (show l) ++ " : " ++ (show c)
-- all other cases as well
}
\ No newline at end of file
let x = 34 in x + 17
\ No newline at end of file
{
module MDLTokens where
}
-- start with writing a grammar
-- E := Forward n | Rotate D | Check [1..9] | if E then E else E | E ; E | ( E )
-- D := L | R
%wrapper "posn"
$digit = 0-9
tokens :-
$white+ ;
"--".* ;
Forward {\p s -> TokenForward p}
Rotate {\p s -> TokenRotate p}
Check {\p s -> TokenCheck p}
if {\p s -> TokenIf p}
then {\p s -> TokenThen p}
else {\p s -> TokenElse p}
L {\p s -> TokenLeft p}
R {\p s -> TokenRight p}
\;
\(
\)
[1..9] {\p s -> TokenDigit p (read s)}
$digit $digit+ {\p s -> TokenInt p (read s)}
{
data MDLToken =
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment