Skip to content

Commit

Permalink
Update Flex ll file
Browse files Browse the repository at this point in the history
  • Loading branch information
hangingman committed Oct 29, 2021
1 parent f02affc commit f5a5576
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions source/src/BNFC/Backend/C/CFtoFlexC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ cf2flex mode cf = (, env) $ unlines
[ prelude stringLiterals mode
, cMacros cf
, lexSymbols mode env1
, restOfFlex (parserPackage mode) cf env
, restOfFlex mode cf env
, footer -- mode
]
where
Expand Down Expand Up @@ -271,31 +271,44 @@ lexSymbols mode ss = concatMap transSym ss
s' = escapeChars s
prefix = if (beyondAnsi mode) then "token::" else ""

restOfFlex :: InPackage -> CF -> SymMap -> String
restOfFlex _inPackage cf env = unlines $ concat
restOfFlex :: ParserMode -> CF -> SymMap -> String
restOfFlex mode cf env = unlines $ concat
[ [ render $ lexComments $ comments cf
, ""
]
, userDefTokens
, ifC catString $ lexStrings "yylval" "_STRING_" "_ERROR_"
, ifC catChar $ lexChars "yylval" "_CHAR_"
, ifC catDouble [ "<INITIAL>{DIGIT}+\".\"{DIGIT}+(\"e\"(\\-)?{DIGIT}+)? \t yylval->_double = atof(yytext); return _DOUBLE_;" ]
, ifC catInteger [ "<INITIAL>{DIGIT}+ \t yylval->_int = atoi(yytext); return _INTEGER_;" ]
, ifC catIdent [ "<INITIAL>{LETTER}{IDENT}* \t yylval->_string = strdup(yytext); return _IDENT_;" ]
, ifC catString $ lexStrings "yylval" (prefix++"_STRING_") (prefix++"_ERROR_")
, ifC catChar $ lexChars "yylval" (prefix++"_CHAR_")
, ifC catDouble [ "<INITIAL>{DIGIT}+\".\"{DIGIT}+(\"e\"(\\-)?{DIGIT}+)? \t " ++ (yylvalCopy mode "double" "yytext") ++ " return " ++prefix++ "_DOUBLE_;" ]
, ifC catInteger [ "<INITIAL>{DIGIT}+ \t " ++ (yylvalCopy mode "int" "yytext") ++ " return " ++prefix++ "_INTEGER_;" ]
, ifC catIdent [ "<INITIAL>{LETTER}{IDENT}* \t " ++ (yylvalCopy mode "string" "yytext") ++ " return " ++prefix++ "_IDENT_;" ]
, [ "<INITIAL>[ \\t\\r\\n\\f] \t /* ignore white space. */;"
, "<INITIAL>. \t return _ERROR_;"
, "<INITIAL>. \t return " ++prefix++ "_ERROR_;"
, ""
, "%% /* Initialization code. */"
]
]
where
ifC cat s = if isUsedCat cf (TokenCat cat) then s else []
userDefTokens =
[ "<INITIAL>" ++ printRegFlex exp ++
" \t yylval->_string = strdup(yytext); return " ++ sName name ++ ";"
| (name, exp) <- tokenPragmas cf
]
where sName n = fromMaybe n $ Map.lookup (Tokentype n) env
_inPackage = parserPackage mode
prefix = if (beyondAnsi mode) then "token::" else ""
ifC cat s = if isUsedCat cf (TokenCat cat) then s else []
userDefTokens =
[ "<INITIAL>" ++ printRegFlex exp ++
" \t " ++ (yylvalCopy mode "string" "yytext") ++ " return " ++ prefix ++ (sName name) ++ ";"
| (name, exp) <- tokenPragmas cf
]
where sName n = fromMaybe n $ Map.lookup (Tokentype n) env

-- | switch yylval->emplace<T> and yylval->_x = conv(yytext)
yylvalCopy :: ParserMode -> String -> String -> String
yylvalCopy mode typeStr arg =
case (beyondAnsi mode, typeStr) of
(True , "string") -> "yylval->emplace<std::string>(" ++arg++ ");"
(True , _ ) -> "yylval->emplace<" ++typeStr++ ">(" ++arg++ ");"
(False, "int" ) -> "yylval->_int = atoi(" ++arg++ ");"
(False, "string") -> "yylval->_string = strdup(" ++arg++ ");"
(False, "double") -> "yylval->_double = atof(" ++arg++ ");"
(False, _ ) -> ""

footer :: String
footer = unlines
Expand Down

0 comments on commit f5a5576

Please sign in to comment.