datatype term = node of string*term list
| vnode of string
I have a value of type term. How do I print it in SML to the standard output?
You need to first create a string out of the term and then print that using print. To turn a term into a string, you could define a function like this:
fun termToString (node (str, terms)) =
"node(" ^ str ^ ", " ^ termListToString terms ^ ")"
| termToString (vnode str) =
"vnode(" ^ str ^ ")"
and termListToString terms =
"[" ^ String.concatWith ", " (map termToString terms) ^ "]"
Related
I have an issue to replace a special characters in string (from IIS Sharepoint log files) that contains a domain name with forward slash and names that starts with t, n, r that makes confusions with regular expressions. my code is as follow:
std::setlocale(LC_ALL, ".ACP"); //Sets the locale to the ANSI code page obtained from the operating system. FR characters
std::string subject("2018-08-26 11:38:20 172.20.1.148 GET /BaseDocumentaire/Documents+de+la+page+Notes+de+services/Rappel+du+dispositif+de+Sécurité+relatif+aux+Moyens+de+paiement+et+d’épargne+en+agence.pdf - 80 0#.w|domainname\tonzaro 10.12.105.24 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64;+rv:61.0)+Gecko/20100101+Firefox/61.0 200 0 0 29984");
std::string result;
std::string g1, g2, g5, g9, g10; //str groups in regex
try {
std::regex re("(\\d{4}-\\d{2}-\\d{2})( \\d{2}:\\d{2}:\\d{2})( 172.20.1.148)( GET | POST | HEAD )((/.*){1,4}/.*.(pdf|aspx))( -.*)(domainname.[a-zA-Z0-9]*)( \\d+.\\d+.\\d+.\\d+)");
std::sregex_iterator next(subject.begin(), subject.end(), re);
std::sregex_iterator end;
while (next != end) {
std::smatch match = *next;
std::cout << match.str() << "\n";
std::cout << "-------------------------------------------" << "\n";
g1 = match.str(1);
g2 = match.str(2);
g5 = match.str(5);
g9 = match.str(9);
g10 = match.str(10);
next++;
}
std::cout << "Date: " + g1 << "\n";
std::cout << "Time: " + g2 << "\n";
std::replace(g5.begin(), g5.end(), '+', ' ');
std::cout << "Link Document : " + g5 << "\n";
std::cout << "User: " + g9 << "\n";
std::cout << "IP: " + g10 << "\n";
}
catch (std::regex_error& e) {
std::cout << "Syntax error in the regular expression" << "\n";
}
My output for domain name is: domainname onzaro
Any help please for this problem with \, \t, \n or \r ?
I'd urge you to use raw string literals. This is solution designed for cases where the literal should not be processed in any way, such as yours.
The syntax is R "delimiter( raw_characters )delimiter", so in your case it could be:
std::string subject(R"raw(2018-08-26 11:38:20 172.20.1.148 GET /BaseDocumentaire/Documents+de+la+page+Notes+de+services/Rappel+du+dispositif+de+Sécurité+relatif+aux+Moyens+de+paiement+et+d’épargne+en+agence.pdf - 80 0#.w|domainname\tonzaro 10.12.105.24 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64;+rv:61.0)+Gecko/20100101+Firefox/61.0 200 0 0 29984)raw");
std::regex re( R"raw((\d{4}-\d{2}-\d{2})( \d{2}:\d{2}:\d{2})( 172.20.1.148)( GET | POST | HEAD )((/.*){1,4}/.*.(pdf|aspx))( -.*)(domainname.[a-zA-Z0-9]*)( \d+.\d+.\d+.\d+))raw");
(I might have missed some superfluous \ above). See it live.
Those special characters are called escape sequences are being processed in string literals at compilation level (in phase 5 to be precise). For raw string literals this transformation is suppressed.
You don't care about any special character handling. You just need to take care that ")delimiter" doesn't appear in your literal, which I imagine could happen in regex.
'\t' is one character, a horizontal tab. If you want the characters \ and t, you need to escape the backslash: "\\t".
I'm trying to write a template parser and need to pickup (3) distinct sets of sequences for string replacement.
// Each of These Expressions Work Perfect Separately!
// All Sequences start with | pipe. Followed by
boost::regex expr {"(\\|[0-9]{2})"}; // 2 Digits only.
boost::regex expr {"(\\|[A-Z]{1,2}+[0-9]{1,2})"}; // 1 or 2 Uppercase Chars and 1 or 2 Digits.
boost::regex expr {"(\\|[A-Z]{2})(?!\\d)"}; // 2 Uppercase Chars with no following digits.
However, once I try to combine them into a single statement, I get can't them to work properly to catch all sequences. I must be missing something. Can anyone shed some light on what I'm missing?
Here is what I have so far:
// Each sequence is separated with a | for or between parenthesis.
boost::regex expr {"(\\|[0-9]{2})|(\\|[A-Z]{1,2}+[0-9]{1,2})|(\\|[A-Z]{2})(?!\\d)"};
I'm using the follow string for testing, and probably little more then needed here is the code as well.
#include <boost/regex.hpp>
#include <string>
#include <iostream>
std::string str = "|MC01 |U1 |s |A22 |12 |04 |2 |EW |SSAADASD |15";
boost::regex expr {"(\\|[0-9]{2})|(\\|[A-Z]{1,2}+[0-9]{1,2})|(\\|[A-Z]{2})(?!\\d)"};
boost::smatch matches;
std::string::const_iterator start = str.begin(), end = str.end();
while(boost::regex_search(start, end, matches, expr))
{
std::cout << "Matched Sub '" << matches.str()
<< "' following ' " << matches.prefix().str()
<< "' preceeding ' " << matches.suffix().str()
<< std::endl;
start = matches[0].second;
for(size_t s = 1; s < matches.size(); ++s)
{
std::cout << "+ Matched Sub " << matches[s].str()
<< " at offset " << matches[s].first - str.begin()
<< " of length " << matches[s].length()
<< std::endl;
}
}
I believe this is what you want:
const boost::regex expr {"(\\|[0-9]{2})|(\\|[A-Z]{1,2}+[0-9]{1,2})|(\\|[A-Z]{2})"}; // basically, remove the constraint on the last sub
I also suggest being explicit in your flags for expr and passed to regex_search.
I also fond that by added an extra check for matches on matched, this removes half-matched patterns which was throwing me off.
for(size_t s = 1; s < matches.size(); ++s)
{
if (matches[s].matched) // Check for bool True/False
{
std::cout << "+ Matched Sub " << matches[s].str()
<< " at offset " << matches[s].first - str.begin()
<< " of length " << matches[s].length()
<< std::endl;
}
}
Without it, matches where showing with an offset at the end of the string showing length 0. So I hope this helps anyone else who runs into this.
Another Tip is, in the loop, checking s == 1, 2, 3 refers back to the match on the expressions. Since I have (3) expressions, if it matched on the first part of the expression, s will have a 1 value when matched is a true value, otherwise it will have 2 or 3. Pretty nice!
I want configuring my .vimrc for do somes auto syntax checking.
That is my problem, i want auto change somes syntax by another.
I deal with the specific caracter in computer programation like = ; , . ( { [ <.
An exemple it's better than words :
void bibi(int param1,char *words)
{
unsigned int locale=param;
cout<<words<<endl;
}
became :
void bibi( int param1,char* words)
{
unsigned int locale = param;
cout << words << endl;
}
Just formating with add or remove some whitespaces.
I write this :
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Formating of text in code
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! ChangeSpaces()
"" search and replace "= " or " =" or "= " to " = "
silent! %s/\s*[=]\s*/ = /g
endfunction
""autocmd CursorMovedI * call ChangeSpaces()
""autocmd BufWrite * call ChangeSpaces()
autocmd FileAppendPre * call ChangeSpaces()
But i have not the result, in this case, if i write " i=e" , they do nothing but if i write 'i= ', it's work, the regex doesn't run, they replace after the end of the "pattern".
By the way if you have a more "sexy way" to do what i want, let me know.
In fact, when i want add some other specific caracter the code became :
"function! ChangeSpaces()
"" search and replace "= " or " =" or "= " to " = "
"silent! %s/\s*[=]\s*/ = /g
""" search and replace "( " or " (" or "(" to " ( "
"" silent! %s/\s*[(]\s*/ ( /g
""" search and replace "[ " or " [" or "[" to " [ "
"" silent! %s/\s*[[]\s*/ [ /g
""" search and replace ", " or " ," or "," to " , "
"" silent! %s/\s*[,]\s*/ , /g
""" search and replace "== " or " ==" or "==" to " == "
"" silent! %s/\s*[==]\s*/ = /g
""" search and replace "> " or " >" or ">" to " > "
"" silent! %s/\s*[>]\s*/ > /g
""" search and replace ">= " or " >=" or ">=" to " >= "
" silent! %s/\s*[>=]\s*/ >= /g
""" search and replace "< " or " <" or "<" to " < "
"" silent! %s/\s*[<]\s*/ < /g
""" search and replace "<= " or " <=" or "<=" to " <= "
"" silent! %s/\s*[=]\s*/ <= /g
"" let repl=substitute(cline,\s*[= ]\s*," = ", "g")
"" call setline(".",repl)
"" let cline=line(".")
"" let ccol=col(".")
"" call cursor(cline, ccol)
"endfunction
""autocmd CursorMovedI * call ChangeSpaces()
""autocmd BufWrite * call ChangeSpaces()
"autocmd FileAppendPre * call ChangeSpaces()
Best regards.
PS: my bad, i want this kind of formating, for every language i use, not just C++.
What about filtering your file through an external C++ indenter? While GNU indent says it was not designed for C++ it works reasonably well. If it doesn't, you might try astyle. Then all you have to do is
map <F8> :w<CR>m':%!astyle<CR>`'
That way even folks using other editors can use the same indent style.
I have a regex I intend to use to "tokenize" a mathematical expression like:
a + b + 1 + 2
int main() {
string rxstrIdentifier = "\\b[a-zA-Z]\\w*\\b";
string rxstrConstant = "\\b\\d+\\b";
string rxstrRef = "(" + rxstrIdentifier + ")|(" + rxstrConstant + ")"; // identifier or constant
const regex rxExpr = regex("^(" + rxstrRef + ")(.*)$"); // {x} [{+} {y}]*
//const regex rxSubExpr = regex("^\\s*([+])\\s*(" + rxstrRef + ")(.*)$"); // {+} {x} [...]
string test = "b + a + 1";
cmatch res;
regex_search(test.c_str(), res, rxExpr);
cout << "operand: " << res[1] << endl;
cout << "res: " << res[2] << endl;
system("pause");
return 0;
}
Problem is operand and res gives just b in the example. I expected
operand: b
res: + a + 1
Used to work in another similar regex ...
const regex Parser::rxExpr = regex("^(\\w+)((\\s*([+])\\s*(\\w+))*)$"); // {x} [{+} {y}]*
const regex Parser::rxSubExpr = regex("^\\s*([+])\\s*(\\w+)(.*)$"); // {+} {x} [...]
Your regexes don't appear to allow for the whitespace in the string. \b matches word boundaries, but boundaries have zero width so nothing's consuming the spaces between the tokens.
Use (?:pattern) group:
string rxstrRef = "(?:" + rxstrIdentifier + ")|(?:" + rxstrConstant + ")"; // identifier or constant
This eliminates the impact on the search results
I am trying to perform a regex search, but instead of typing the pattern (e.g std::regex reg = "\.") I want to give regex a string input. Here is my code where I convert string to const char* and pass it to regex_search:
void trigger_regex(string name, string body)
{
map<string,string>::iterator it;
it=test.mydata.find(name);
if( it == test.mydata.end())
{
std::cout<<"REGEX not found"<<endl;
}
else
cout << "Found REGEX with name: " << it->first << " and rule: " << it->second << endl ;
string subject = body;
string rule = it -> second;
const char * pattern = rule.c_str();
regex reg = (pattern);
smatch match;
while (regex_search (subject,match,reg)) {
for (auto x:match) std::cout << x << " ";
std::cout << std::endl;
body = match.suffix().str();
}
}
I get error:
conversion from 'const char*' to non-scalar type 'std::regex {aka
std::basic_regex}' requested
regex reg1 = (rule1);
any help pls?
If you look at e.g. this std::regex constructor reference you will see that the constructor taking just a string (the second constructor in the list) is marked explicit. That means you must explicitly use that constructor, you can't just hope to convert a string to a std::regex object.
In other words, the solution is to define and initialize the object like this
std::regex reg1{pattern};