Module Tools.YYgen
This program creates a parser from the result of running byacc -v on a yacc grammar. It reads the resulting files "y.tab.c" and "y.output" in the current directory and uses the information therein to construct a frege program.
Each grammar rule is associated with a reduction function of type
item1 -> ... -> item2 -> result
where the item types are either tokens (provided as input to the parser) or results of other grammar rules. All rules of a nonterminal grammar symbol will have the same result type. Example:
%{
package Calculator
numberFrom :: Token -> Double
%}
%token NUMBER PI
%start sum
%%
sum : product '+' product { \a\_\b -> a + b } // was: $$ = $1 + $3
| product // was: $$ = $1
;
product: term '*' term { \a\b\c -> a * b }
| term
;
term: NUMBER { numberFrom }
| PI { const 3.14159 }
| '-' term { \_\b -> negate b }
| '(' sum ')' { \_\b\_ -> b }
;
%%
data TokenID = NUMBER | PI | OPERATOR
derive Show TokenID
derive Ord TokenID
data Token = N Double | Pi | Op Char
derive Show Token
yyshowToken = Token.show
yyniceToken (N d) = show d
yyniceToken Pi = "pi"
yyniceToken (Op c) = show c
yytoken :: Token -> TokenID
yytoken (N _) = NUMBER
yytoken Pi = PI
yytoken Op = OPERATOR
yychar :: Token -> Char
yychar (Op c) = c
yychar _ = const 'ยง' // paragraph symbol not used elsewhere
yyfromId NUMBER = N 42.0
yyfromId PI = PI
// yyfromId OPERATOR = not needed, as the parser knows nothing of OPERATOR
yyfromCh :: Char -> Token
yyfromCh c = Op c
yyline = const 0 // no way to know in this example
So far, this is just a good old yacc grammar, except that the semantic actions are frege expressions, and the code before an after the "%%" markers is frege code.
Each semantic action is expected to be a function that takes as many arguments as there are items in its rule. Hence, in the first rule of nonterminal "sum" the left product will be bound to /a/, the "+" token is ignored and the right product to /b/. A missing action is equivalent to the identity function, therefore in the second rule of nonterminal "sum", the sum will be just the product.
The parser generated from this will have a function
yyparse :: [Token] -> Maybe Double
since Double is the result type of the rules associated with the start symbol and tokens are obviously of type Token (due to application of user supplied function numberFrom). The result will be Maybe.Nothing if the parser could not reduce the start symbol (i.e., when there were nonrecoverable syntax errors) and (Maybe.Just x) otherwise, where /x/ is the result of one of the semantic actions associated with the "%start" symbol.
The parser itself does not make any assumptions about what a token is. Instead, it relies on the following user supplied functions:
yytoken :: token -> tid
yyfromId :: tid -> token
where /token/ is the type of tokens and /tid/ is the type of the token constants defined in the "%token" directive. The /yytoken/ function must be total, i.e. all possible token values must produce some token constant, yet this does not have to be one of the constants that is listed in the "%token" directive.
The /yyfromId/ function must produce an example token value for each token constant the parser knows of through the "%token" directive. Both functions have to be there only when a "%token" directive is present.
If the grammar uses character constants, the following functions must be present:
yychar :: token -> Char
yyfromCh :: Char -> token
The /yychar/ function must be total, all possible token values must be mapped to some character. Use a character value that is not used in the grammar for tokens that have no representation as a character value.
The /yyfromCh/ function maps back characters to token values. The /yyfrom../ functions are used in error recovery to supply tokens that are expected, but missing.
Here is an example of how the parser may determine what to do in state 42:
yyaction 42 tok = case yychar tok of
'*' -> Shift 57
_ -> case yytoken tok of
NUMBER -> Shift 96
_ -> Reduce 5
In addition, the parser needs the following functions:
yynice :: token -> String
yyshow :: token -> String
yyline :: token -> Show:a
yyerror :: Show:a -> String -> c
/yynice/ is used by the parser to construct error messages like this
"syntax error, expected ',', found " ++ yynice errortoken
"syntax error, expected IDENTIFIER, found " ++ yynice errortoken
"syntax error on " ++ yynice errortoken
/yyshow/ is used in trace output and is intended for a most detailed display of tokens as they are recognized by the parser.
/yyline/ is used to extract line number information from a token, it is thus a good idea to design the lexical analyzer so that a token is able to tell where it was found.
/yyerror/ is used to emit messages about syntax errors. The first argument will be either the string "EOF" or the result of applying /yyline/ to a token. The second argument is a string that describes the error. The result of yyerror is evaluated, but ignored.
Imports
Table of Content
-
-
data Action
-
-
data Item
-
-
data Prod
-
-
data Todo
-
-
data YYState
-
-
resource โท Maybe ๐ โ String
-
-
yygenpar โท Maybe a โ IO [String]
-
-
loadResource โท Maybe a โ MutableIO URLClassLoader โ IO [String]
-
-
loadUrl โท URL โ IO [String]
-
-
uncr โท String โ String
-
-
fileContent โท String โ IO [String]
-
-
scanlines โท [String] โ ([String], [(Int, [String])])
-
-
scanytablines โท [String] โ ([String], [String], [(Int, String)], [(String, String)], TreeMap String String)
-
-
compiletypes โท TreeMap String String โ [String] โ String
-
-
genshowsi โท [String] โ String
-
-
mkState โท (Int, [String]) โ YYState
-
-
genitems โท [Item] โ [String]
-
-
genitem โท Int โ Item โ String
-
-
niceitem โท Item โ String
-
-
genst โท YYState โ String
-
-
printstates โท [YYState] โ MutableIO PrintWriter โ IO ()
-
-
extrrule โท YYState โ [Prod]
-
-
extrrules โท [YYState] โ [Prod]
-
-
printpr โท Maybe ๐ โ [YYState] โ [(Int, String)] โ MutableIO PrintWriter โ IO ()
-
-
genacc โท Show ๐ โ ๐ โ Prod โ String
-
-
genrule โท Prod โ String
-
-
genprod โท Maybe ๐ โ Prod โ [(Int, String)] โ String
-
-
genstate โท Show ๐ โ ๐ โ [Prod] โ Action โ String
-
-
gengoto1 โท ๐ โ Action โ [Prod] โ [(๐, Int, Int)]
-
-
gengotos1 โท [Prod] โ YYState โ [(Int, Int, Int)]
-
-
gengoto2 โท Action โ [Prod] โ [(Int, Int)] โ [(Int, Int)]
-
-
gengotos2 โท [Prod] โ YYState โ (Int, [(Int, Int)])
-
-
sortfst โท Ord ๐ โ (๐, ๐) โ (๐, ๐) โ Bool
-
-
sortgos โท (Ord ๐, Ord ๐) โ (๐, ๐, ๐) โ (๐, ๐, ๐) โ Ordering
-
-
altgotos โท [Prod] โ [YYState] โ [(Int, Int, Int)] โ [(Int, Int, Int)]
-
-
altgotos2 โท [Prod] โ [YYState] โ [(Int, [(Int, Int)])]
-
-
listmax โท Int
-
-
pracase โท Show a โ String โ [(Int, a)] โ MutableIO PrintWriter โ IO ()
-
-
collecteactions โท YYState โ Bool
-
-
collectacceptactions โท YYState โ Bool
-
-
actions โท [YYState] โ [(Int, String)]
-
-
recoveries โท TreeMap String String โ [YYState] โ [(Int, String)]
-
-
eactions โท [YYState] โ [(Int, String)]
-
-
numbertypes โท Ord ๐ โ Int โ [๐] โ TreeMap ๐ Int โ TreeMap ๐ Int
-
-
numprod โท Prod โ (Int, String)
-
-
listred โท Show ๐ โ (๐, String) โ String
-
-
printreds โท Show ๐ โ [(๐, String)] โ MutableIO PrintWriter โ IO ()
-
-
gengos โท (Ord ๐, ListSource ๐, Show ๐, Show ๐, Show ๐, Ord ๐, Ord ๐) โ ๐ (๐, ๐, ๐) โ String
-
-
printgos โท [(Int, [(Int, Int)])] โ MutableIO PrintWriter โ IO ()
-
-
format1 โท String โ Int โ String
-
-
main โท [String] โ IO ()
-
-
cantwrite โท String โ FileNotFoundException โ IO ()
-
-
mainIO โท Maybe String โ String โ IO ()
-
-
-
++ โท ListSemigroup ฮฑ โ ฮฑ ฮฒ โ ฮฑ ฮฒ โ ฮฑ ฮฒ
-
-
filter โท (๐โBool) โ [๐] โ [๐]
-
-
-
instance Eq Item
-
-
instance Eq Prod
-
-
instance Ord Item
-
-
instance Ord Prod
-
-
instance Show Action
-
-
instance Show Item
-
-
instance Show Prod
-
-
instance Show Todo
-
-
instance Show YYState
-
-
-
resource โท Maybe ๐ โ String
-
-
yygenpar โท Maybe a โ IO [String]
-
-
loadResource โท Maybe a โ MutableIO URLClassLoader โ IO [String]
-
-
loadUrl โท URL โ IO [String]
-
-
uncr โท String โ String
-
remove carriage returns from strings -
-
fileContent โท String โ IO [String]
-
give back file content as list of lines -
-
scanlines โท [String] โ ([String], [(Int, [String])])
-
-
scanytablines โท [String] โ ([String], [String], [(Int, String)], [(String, String)], TreeMap String String)
-
-
compiletypes โท TreeMap String String โ [String] โ String
-
-
genshowsi โท [String] โ String
-
-
data Item
-
Constructors
-
Acc
-
-
Def
-
-
End
-
-
Lit (String)
-
-
NT (String)
-
-
T (String)
-
-
data Prod
-
Constructors
-
Prod Int Item [Item]
-
-
data Todo
-
Constructors
-
Accept
-
-
Error
-
-
Goto
-
-
Reduce
-
-
Shift
-
-
data Action
-
Constructors
-
A Todo Item Int
-
-
data YYState
-
Constructors
-
St Int [Prod] [Action] [Action]
-
-
mkState โท (Int, [String]) โ YYState
-
-
genitems โท [Item] โ [String]
-
-
genitem โท Int โ Item โ String
-
-
niceitem โท Item โ String
-
-
genst โท YYState โ String
-
-
printstates โท [YYState] โ MutableIO PrintWriter โ IO ()
-
-
extrrule โท YYState โ [Prod]
-
-
extrrules โท [YYState] โ [Prod]
-
-
printpr โท Maybe ๐ โ [YYState] โ [(Int, String)] โ MutableIO PrintWriter โ IO ()
-
-
genacc โท Show ๐ โ ๐ โ Prod โ String
-
-
genrule โท Prod โ String
-
-
genprod โท Maybe ๐ โ Prod โ [(Int, String)] โ String
-
-
genstate โท Show ๐ โ ๐ โ [Prod] โ Action โ String
-
-
gengoto1 โท ๐ โ Action โ [Prod] โ [(๐, Int, Int)]
-
-
gengotos1 โท [Prod] โ YYState โ [(Int, Int, Int)]
-
-
gengoto2 โท Action โ [Prod] โ [(Int, Int)] โ [(Int, Int)]
-
-
gengotos2 โท [Prod] โ YYState โ (Int, [(Int, Int)])
-
-
sortfst โท Ord ๐ โ (๐, ๐) โ (๐, ๐) โ Bool
-
-
sortgos โท (Ord ๐, Ord ๐) โ (๐, ๐, ๐) โ (๐, ๐, ๐) โ Ordering
-
-
altgotos โท [Prod] โ [YYState] โ [(Int, Int, Int)] โ [(Int, Int, Int)]
-
-
altgotos2 โท [Prod] โ [YYState] โ [(Int, [(Int, Int)])]
-
-
listmax โท Int
-
-
pracase โท Show a โ String โ [(Int, a)] โ MutableIO PrintWriter โ IO ()
-
-
collecteactions โท YYState โ Bool
-
-
collectacceptactions โท YYState โ Bool
-
-
actions โท [YYState] โ [(Int, String)]
-
-
recoveries โท TreeMap String String โ [YYState] โ [(Int, String)]
-
-
eactions โท [YYState] โ [(Int, String)]
-
-
numbertypes โท Ord ๐ โ Int โ [๐] โ TreeMap ๐ Int โ TreeMap ๐ Int
-
-
numprod โท Prod โ (Int, String)
-
-
listred โท Show ๐ โ (๐, String) โ String
-
-
printreds โท Show ๐ โ [(๐, String)] โ MutableIO PrintWriter โ IO ()
-
-
gengos โท (Ord ๐, ListSource ๐, Show ๐, Show ๐, Show ๐, Ord ๐, Ord ๐) โ ๐ (๐, ๐, ๐) โ String
-
-
printgos โท [(Int, [(Int, Int)])] โ MutableIO PrintWriter โ IO ()
-
create frege code for go tabs
-
format1 โท String โ Int โ String
pure native java.lang.String.format
-
-
main โท [String] โ IO ()
-
-
cantwrite โท String โ FileNotFoundException โ IO ()
-
-
mainIO โท Maybe String โ String โ IO ()
-
-
instance Eq Item
-
Member Functions
-
!= โท Item โ Item โ Bool
infix 7
-
inherited from Eq.!=
-
== โท Item โ Item โ Bool
infix 7
-
Function generated for derived instance.
-
hashCode โท Item โ Int
-
Function generated for derived instance.
-
instance Eq Prod
-
Member Functions
-
!= โท Prod โ Prod โ Bool
infix 7
-
inherited from Eq.!=
-
== โท Prod โ Prod โ Bool
infix 7
-
Function generated for derived instance.
-
hashCode โท Prod โ Int
-
Function generated for derived instance.
-
instance Ord Item
-
Member Functions
-
< โท Item โ Item โ Bool
infix 9
-
inherited from Ord.<
-
<= โท Item โ Item โ Bool
infix 9
-
inherited from Ord.<=
-
<=> โท Item โ Item โ Ordering
infix 8
-
Function generated for derived instance.
-
> โท Item โ Item โ Bool
infix 9
-
inherited from Ord.>
-
>= โท Item โ Item โ Bool
infix 9
-
inherited from Ord.>=
-
compare โท Item โ Item โ Ordering
infix 8
-
inherited from Ord.compare
-
max โท Item โ Item โ Item
-
inherited from Ord.max
-
min โท Item โ Item โ Item
-
inherited from Ord.min
-
instance Ord Prod
-
Member Functions
-
< โท Prod โ Prod โ Bool
infix 9
-
inherited from Ord.<
-
<= โท Prod โ Prod โ Bool
infix 9
-
inherited from Ord.<=
-
<=> โท Prod โ Prod โ Ordering
infix 8
-
-
> โท Prod โ Prod โ Bool
infix 9
-
inherited from Ord.>
-
>= โท Prod โ Prod โ Bool
infix 9
-
inherited from Ord.>=
-
compare โท Prod โ Prod โ Ordering
infix 8
-
inherited from Ord.compare
-
max โท Prod โ Prod โ Prod
-
inherited from Ord.max
-
min โท Prod โ Prod โ Prod
-
inherited from Ord.min
-
instance Show Action
-
Member Functions
-
display โท Action โ String
-
inherited from Show.display
-
show โท Action โ String
-
Function generated for derived instance.
-
showChars โท Action โ [Char]
-
inherited from Show.showChars
-
showList โท [Action] โ String โ String
-
inherited from Show.showList
-
showsPrec โท Int โ Action โ String โ String
-
inherited from Show.showsPrec
-
showsub โท Action โ String
-
Function generated for derived instance.
-
instance Show Item
-
Member Functions
-
display โท Item โ String
-
inherited from Show.display
-
show โท Item โ String
-
Function generated for derived instance.
-
showChars โท Item โ [Char]
-
inherited from Show.showChars
-
showList โท [Item] โ String โ String
-
inherited from Show.showList
-
showsPrec โท Int โ Item โ String โ String
-
inherited from Show.showsPrec
-
showsub โท Item โ String
-
Function generated for derived instance.
-
instance Show Prod
-
Member Functions
-
display โท Prod โ String
-
inherited from Show.display
-
show โท Prod โ String
-
Function generated for derived instance.
-
showChars โท Prod โ [Char]
-
inherited from Show.showChars
-
showList โท [Prod] โ String โ String
-
inherited from Show.showList
-
showsPrec โท Int โ Prod โ String โ String
-
inherited from Show.showsPrec
-
showsub โท Prod โ String
-
Function generated for derived instance.
-
instance Show Todo
-
Member Functions
-
display โท Todo โ String
-
inherited from Show.display
-
show โท Todo โ String
-
Function generated for derived instance.
-
showChars โท Todo โ [Char]
-
inherited from Show.showChars
-
showList โท [Todo] โ String โ String
-
inherited from Show.showList
-
showsPrec โท Int โ Todo โ String โ String
-
inherited from Show.showsPrec
-
showsub โท Todo โ String
-
Function generated for derived instance.
-
instance Show YYState
-
Member Functions
-
display โท YYState โ String
-
inherited from Show.display
-
show โท YYState โ String
-
Function generated for derived instance.
-
showChars โท YYState โ [Char]
-
inherited from Show.showChars
-
showList โท [YYState] โ String โ String
-
inherited from Show.showList
-
showsPrec โท Int โ YYState โ String โ String
-
inherited from Show.showsPrec
-
showsub โท YYState โ String
-
Function generated for derived instance.
-
TreeMap String String โ [String] โ String
-
compiletypes
-
TreeMap String String โ [YYState] โ [(Int, String)]
-
recoveries
-
(Int, [String]) โ YYState
-
mkState
-
Maybe String โ String โ IO ()
-
mainIO
-
String โ FileNotFoundException โ IO ()
-
cantwrite
-
String โ Int โ String
-
format1
-
String โ IO [String]
-
fileContent
-
String โ String
-
uncr
-
String โ Item
-
Item.NT, Item.Lit, Item.T
-
[(Int, [(Int, Int)])] โ MutableIO PrintWriter โ IO ()
-
printgos
-
[String] โ ([String], [String], [(Int, String)], [(String, String)], TreeMap String String)
-
scanytablines
-
[String] โ ([String], [(Int, [String])])
-
scanlines
-
[String] โ IO ()
-
main
-
[String] โ String
-
genshowsi
-
[Action] โ String โ String
-
Show_Action.showList
-
[Item] โ String โ String
-
Show_Item.showList
-
[Item] โ [String]
-
genitems
-
[Prod] โ String โ String
-
Show_Prod.showList
-
[Prod] โ [YYState] โ [(Int, Int, Int)] โ [(Int, Int, Int)]
-
altgotos
-
[Prod] โ [YYState] โ [(Int, [(Int, Int)])]
-
altgotos2
-
[Prod] โ YYState โ (Int, [(Int, Int)])
-
gengotos2
-
[Prod] โ YYState โ [(Int, Int, Int)]
-
gengotos1
-
[Todo] โ String โ String
-
Show_Todo.showList
-
[YYState] โ MutableIO PrintWriter โ IO ()
-
printstates
-
[YYState] โ String โ String
-
Show_YYState.showList
-
[YYState] โ [(Int, String)]
-
actions, eactions
-
[YYState] โ [Prod]
-
extrrules
-
URL โ IO [String]
-
loadUrl
-
Int โ [Prod] โ [Action] โ [Action] โ YYState
-
YYState.St
-
Int โ Action โ String โ String
-
Show_Action.showsPrec
-
Int โ Item โ String โ String
-
Show_Item.showsPrec
-
Int โ Item โ [Item] โ Prod
-
Prod.Prod
-
Int โ Item โ String
-
genitem
-
Int โ Prod โ String โ String
-
Show_Prod.showsPrec
-
Int โ Todo โ String โ String
-
Show_Todo.showsPrec
-
Int โ YYState โ String โ String
-
Show_YYState.showsPrec
-
Action โ [Prod] โ [(Int, Int)] โ [(Int, Int)]
-
gengoto2
-
Action โ String
-
Show_Action.showsub, Show_Action.display, Show_Action.show
-
Action โ [Char]
-
Show_Action.showChars
-
Item โ Item โ Bool
-
Eq_Item.!=, Eq_Item.==, Ord_Item.>=, Ord_Item.<, Ord_Item.<=, Ord_Item.>
-
Item โ Item โ Ordering
-
Ord_Item.compare, Ord_Item.<=>
-
Item โ Item โ Item
-
Ord_Item.min, Ord_Item.max
-
Item โ String
-
niceitem, Show_Item.showsub, Show_Item.display, Show_Item.show
-
Item โ [Char]
-
Show_Item.showChars
-
Item โ Int
-
Eq_Item.hashCode
-
Prod โ Prod โ Bool
-
Eq_Prod.!=, Eq_Prod.==, Ord_Prod.>=, Ord_Prod.<, Ord_Prod.<=, Ord_Prod.>
-
Prod โ Prod โ Ordering
-
Ord_Prod.compare, Ord_Prod.<=>
-
Prod โ Prod โ Prod
-
Ord_Prod.min, Ord_Prod.max
-
Prod โ (Int, String)
-
numprod
-
Prod โ String
-
genrule, Show_Prod.showsub, Show_Prod.display, Show_Prod.show
-
Prod โ [Char]
-
Show_Prod.showChars
-
Prod โ Int
-
Eq_Prod.hashCode
-
Todo โ Item โ Int โ Action
-
Action.A
-
Todo โ String
-
Show_Todo.showsub, Show_Todo.display, Show_Todo.show
-
Todo โ [Char]
-
Show_Todo.showChars
-
YYState โ String
-
genst, Show_YYState.showsub, Show_YYState.display, Show_YYState.show
-
YYState โ [Char]
-
Show_YYState.showChars
-
YYState โ [Prod]
-
extrrule
-
YYState โ Bool
-
collectacceptactions, collecteactions
-
Int
-
listmax
-
Item
-
Item.End, Item.Acc, Item.Def
-
Todo
-
Todo.Reduce, Todo.Error, Todo.Accept, Todo.Goto, Todo.Shift
-
Maybe a โ MutableIO URLClassLoader โ IO [String]
-
loadResource
-
Maybe a โ IO [String]
-
yygenpar
-
Maybe ๐ โ [YYState] โ [(Int, String)] โ MutableIO PrintWriter โ IO ()
-
printpr
-
Maybe ๐ โ Prod โ [(Int, String)] โ String
-
genprod
-
Maybe ๐ โ String
-
resource
-
๐ โ Action โ [Prod] โ [(๐, Int, Int)]
-
gengoto1
-
Ord ๐ โ Int โ [๐] โ TreeMap ๐ Int โ TreeMap ๐ Int
-
numbertypes
-
Show a โ String โ [(Int, a)] โ MutableIO PrintWriter โ IO ()
-
pracase
-
Show ๐ โ (๐, String) โ String
-
listred
-
Show ๐ โ [(๐, String)] โ MutableIO PrintWriter โ IO ()
-
printreds
-
Show ๐ โ ๐ โ [Prod] โ Action โ String
-
genstate
-
Show ๐ โ ๐ โ Prod โ String
-
genacc
-
Ord ๐ โ (๐, ๐) โ (๐, ๐) โ Bool
-
sortfst
-
(Ord ๐, ListSource ๐, Show ๐, Show ๐, Show ๐, Ord ๐, Ord ๐) โ ๐ (๐, ๐, ๐) โ String
-
gengos
-
(Ord ๐, Ord ๐) โ (๐, ๐, ๐) โ (๐, ๐, ๐) โ Ordering
-
sortgos
