\section{Introduction}
\begin{frame}
\frametitle{\textsc{SPL}}
- \framesubtitle{Acronym for: \textsc{SPL}: Parser and Lexer}
\begin{block}{Features}
\begin{itemize}
\item Implementation language:
\usepackage{clean}
\title[cc1516: Lexing \& Parsing]{SPL}
-\subtitle{SPL: Lexical Analysis and Parsing}
+\subtitle{\texttt{<spl> ::= <spl> <lexer> 'and' <parser>}}
\author[P. Jager, M. Lubbers]{Pim Jager\inst{1}\and Mart Lubbers\inst{1}}
\institute[Radboud University]{%
\inst{1}%
+<Prog> ::= <VarDecl>* <FunDecl>+
+
<FunDecl> ::= <id> '(' <Type>* ')' ['::' <FunType] '{' <VarDecl>* <Stmt>+ '}'
<FunType> ::= <Type>+ '->' <VoidType>
| <VoidType>
from Data.Either import :: Either
from yard import :: Error
-
from AST import :: Pos
:: Token :== (Pos, TokenValue)
| LesserToken // <
| BiggerToken // >
| ExclamationToken // !
- //Special tokens
- | EndOfFileToken // EOF
:: LexerOutput :== Either Error [Token]
import StdList
import StdChar
import StdString
+import StdTuple
import yard
-from AST import :: Pos(..)
+import AST
:: LexItem
= LexToken Int TokenValue
| LexItemError String
lexer :: [Char] -> LexerOutput
-lexer r = case runParser (lexProgram 1 1) r of
- (Right p, _) = Right p
- (Left e, _) = Left e
+lexer r = fst $ runParser (lexProgram 1 1) r
lexProgram :: Int Int -> Parser Char [Token]
lexProgram line column = lexToken >>= \t->case t of
from Data.Maybe import :: Maybe
from StdString import class toString
-import lex
+from lex import :: LexerOutput, :: Error, :: Token, :: TokenValue, :: Pos
from AST import :: AST
:: ParserOutput :== Either Error AST
parseBlock = parseBCBraces (many parseStmt)
parseOneLine :: Parser Token [Stmt]
- //first pure makes singleton list from the statement
parseOneLine = pure <$> parseStmt
parseFunType :: Parser Token FunType
parseOpL :: (Parser Token Op2) (Parser Token Expr) -> Parser Token Expr
parseOpL ops prev = peekPos >>= \pos-> prev >>= \e1->many (
- ops >>= \op->prev >>= \e->pure (op, e)
- ) >>= \moe->foldM (\e->(\(op,e2)->pure $ Op2Expr pos e op e2)) e1 moe
+ ops >>= \op->prev >>= \e->pure (op, e))
+ >>= \moe->foldM (\e->(\(op,e2)->pure $ Op2Expr pos e op e2)) e1 moe
parseBasicExpr :: Parser Token Expr
parseBasicExpr = peekPos >>= \pos ->
(VarExpr pos <$> parseVarDef)
parseFunCall :: Parser Token FunCall
-parseFunCall = FunCall <$> parseIdent <*> (parseBBraces $ parseSepList CommaToken parseExpr)
+parseFunCall = FunCall <$> parseIdent <*>
+ (parseBBraces $ parseSepList CommaToken parseExpr)
parseVarDef :: Parser Token VarDef
parseVarDef = liftM2 VarDef
printTokens :: [Token] -> String
printTokens ts = concat $ flatten $ map pt ts
where
- pt ({line,col},token) = [toString line, ":",
+ pt ({line,col},token) = [toString line, ":",
toString col, ": ", printToString token, "\n"]
parseArgs :: *World -> (Opts, *World)