I must write a function "to_string" wich receives this datatype
datatype prop = Atom of string | Not of prop | And of prop*prop | Or of prop*prop;
and returns a string.
Example
show
And(Atom("saturday"),Atom("night")) =
"(saturday & night)"
My function is working but I have 2 problems.
the interpreter tells me -> Warning: match nonexhaustive
I think i can write the function with locals functions for all the types (Not, And, Or) and avoid duplicate code but I don't know how.
there is my code
datatype prop = Atom of string | Not of prop | And of prop*prop | Or of prop*prop;
fun show(Atom(alpha)) = alpha
| show(Not(Atom(alpha))) = "(- "^alpha^" )"
| show(Or(Atom(alpha),Atom(beta))) = "( "^alpha^" | "^beta^" )"
| show(Not(Or(Atom(alpha),Atom(beta)))) = "(- ( "^alpha^" | "^beta^" ))"
| show(Or(Not(Atom(alpha)),Atom(beta))) = "( (-"^alpha^") | "^beta^" )"
| show(Or(Atom(alpha),Not(Atom(beta)))) = "( "^alpha^" | (-"^beta^") )"
| show(Or(Not(Atom(alpha)),Not(Atom(beta)))) = "( (-"^alpha^") | (-"^beta^") )"
| show(And(Atom(alpha),Atom(beta))) = "( "^alpha^" & "^beta^" )"
| show(Not(And(Atom(alpha),Atom(beta)))) = "(- ( "^alpha^" & "^beta^" ))"
| show(And(Not(Atom(alpha)),Atom(beta))) = "( (-"^alpha^") & "^beta^" )"
| show(And(Atom(alpha),Not(Atom(beta)))) = "( "^alpha^" & (-"^beta^") )"
| show(And(Not(Atom(alpha)),Not(Atom(beta)))) = "( (-"^alpha^") & (-"^beta^") )";
Thanks a lot for your help.
The general rule is as follows: if you have a recursive data type, you should use a recursive function to transform it.
Your match expression is not exhaustive because there are a lot of variants you can't handle - i.e. And(And(Atom("a"), Atom("b")), Atom("c")).
You should rewrite the function with recursive calls to itself - i.e. replace Not(Atom(alpha)) match with Not(expr):
show(Not(expr)) = "(- " ^ show(expr) ^ " )"
I'm sure you can figure out the rest (you'll have two recursive calls for and/or).
Related
I am trying to define a simple grammar for C which I am using in Lark. The problem is, I defined the closing parenthesis ("}" or ")") as a terminal in the grammar but it is throwing an error as "No terminal matches ')' in the current parser context". In many example grammar rules that I saw, the closing parenthesis was defined as terminals. How do I resolve this issue?
Here is the code:
from lark import Lark
g=r'''start : header_files
preprocessor_commands : "#include" | definition
definition : def string (header_files)* program
def : "#define" | "#undef" | "#ifdef" | "#ifndef" | "#if" | "#else" | "#elif" | "#endif" | "#error" | "#pragma"
header_files : preprocessor_commands file_names (header_files)* program
file_names : "<stdio.h>" | "<math.h>" | "<conio.h>" | "<stdlib.h>" | "<string.h>" | "<ctype.h>" | "<time.h>" | "<float.h>" | "<limits.h>" | "<wctype.h>"
program : data_type func "(" var ")" "{" (codeblock) close_par | data_type func "()" "{" (codeblock) close_par
data_type : "void" | "int" | "float" | "double" | "long" | "char" | "string" | "long long" | "unsigned_int"
func : "main" | string
var : string
codeblock : "return" var term | "return" const term | "return" func term | "return" "(" expressions ")" term | declarations | expressions | statements | call | print
declarations : data_type var assign const ";" (declarations)* codeblock | data_type var assign var ";" (declarations)* codeblock | data_type var ("," var)* ";" (declarations)* codeblock
expressions : arithmetic | bitwise | assignment
arithmetic : add | sub | mul | div | mod | unary
add : const "+" (arithmetic)* | var "+" (arithmetic)*
sub : const "-" (arithmetic)* | var "-" (arithmetic)*
mul : const "*" (arithmetic)* | var "*" (arithmetic)*
div : const "/" (arithmetic)* | var "/" (arithmetic)*
mod : const "%" (arithmetic)* | var "%" (arithmetic)*
unary : inc | dec
inc : "++" var | var "++"
dec : "--" var | var "--"
bitwise : and | or | xor | boc | ls | rs
and : const "&" (bitwise)* | var "&" (bitwise)*
or : const "|" (bitwise)* | var "|" (bitwise)*
xor : const "^" (bitwise)* | var "^" (bitwise)*
boc : var assign "~" const | var assign "~" var
ls : const "<<" const | var "<<" const
rs : const ">>" const | var ">>" const
assignment : assign | "*=" | "/=" | "%=" | "+=" | "-=" | "<<=" | ">>=" | "&=" | "^=" | "|="
assign : "="
statements : if | switch | loop
if : ("if" "(" logical close_par codeblock)+ ("elseif" "(" logical close_par codeblock)* ["else" codeblock]
logical : land | lor | lnot | equ | gre | les | greq | leeq | neq
land : const "&&" (logical)* | var "&&" (logical)*
lor : const "||" (logical)* | var "||" (logical)*
lnot : "!" (logical)+ | "!" (arithmetic)+ | "!" var | "!" const
equ : const "==" (logical)* | var "==" (logical)*
gre : const ">" (logical)* | var ">" (logical)*
les : const "<" (logical)* | var "<" (logical)*
greq : const ">=" (logical)* | var ">=" (logical)*
leeq : const "<=" (logical)* | var "<=" (logical)*
neq : const "!=" (logical)* | var "!=" (logical)*
switch : "switch" ("(" expressions ")") "{" (switch_case)* ["default" ":" codeblock] close_par
switch_case : "case" const ":" codeblock (switch_case)*
loop : for | while | do_while
for : "for" "(" [[data_type] var assign const] ";" [logical] ";" [arithmetic] ")" "{" codeblock "}" | "for" "(" [[data_type] var assign const] ";" [logical] ";" [arithmetic] ")" codeblock
while : "while" "(" logical ")" "{" codeblock "}" | "while" "(" logical ")" codeblock
do_while : "do" "{" codeblock "}" "while" "(" logical ")"
call : func "(" var ("," var)* ")" term | var assign func "(" var ("," var)* ")" term | func "(" ")" term | var assign func "(" ")" term
print : "printf" "(" (dcstring)* ")" term
%import common.SIGNED_NUMBER
const : SIGNED_NUMBER
term : ";" codeblock
tpar : ")" | "}" | ")" codeblock
digit : "0".."9"
nz_dig : "1".."9"
integer : (digit)* (nz_dig)+ | "-" (digit)* (nz_dig)+
decimal : (digit)+ "." (digit)+ | "-" (digit)+ "." (digit)+
letter : "a".."z" | "A".."Z"
char : letter | SIGNED_NUMBER
string : /[a-zA-Z0-9_.-]{2,}/ | (char)*
dcstring : /"[^"]*"/
close_par : "}" | ")" | "]"
WHITESPACE: " " | "\t" | "\f" | "\n"
%ignore WHITESPACE+
COMMENT: "//" /[^\n]/* | "/*" /(\S|\s)*?/ "*/"
%ignore COMMENT
'''
parser=Lark(grammar=g,parser="earley")
code='''#include<stdio.h>
#define PI 3.14
void main()
{
int a,b; long c=1;
if(a==b || b==c)
return 2;
}
'''
print(parser.parse(code).pretty())
This is the error :
I'm trying to use jq to automate changing i18n string files from the format taken by one library to another.
I have a json file which has looks like this:
{
"some_label": {
"message": "a string in English with a $VARIABLE$",
"description": "directions to translators",
"placeholders": {
"VARIABLE": {
"content": "{variable}"
}
}
},
// more of the same...
}
And I need that to turn in to "some-label": "a string in English with a {variable}"
I am pretty close to getting it. Currently, I'm using
jq '[.
| to_entries
| .[]
| .key |= (gsub("_";"-"))
| .value.placeholders as $p
| .value.message |= (sub("\\$KEY_NAME\\$";$p.KEY_NAME.content))
| .value = .value.message
] | from_entries'
The next step is to use a capture group in the sub call so I can programmatically get variables with different names, but I'm not sure how to use the capture group to index into $p.
I've tried sub("\\$(?<id>VARIABLE)\\$";$p.(.id).content) which gave a compiler error, and I'm pretty much stuck on what to try next.
Here is one way of achieving the desired result. It could be simplified further too. At the top level it removes the usage of to_entries/from_entries by enclosing the whole filter under with_entries() and modifying the .value field as required
with_entries(
.key |= ( gsub("_";"-") ) |
.value.placeholders as $p |
.value.message as $m |
( $m | match(".*\\$(.*)\\$") | .captures[0].string ) as $c |
( $p | .[$c].content ) as $v |
( "\\$" + $c + "\\$" ) as $t |
.value = ( $m | sub($t; $v) )
)
My view of the key parts of the expression are
The part $m | match(".*\\$(.*)\\$") | .captures[0].string makes a regex match to extract the part within the $..$ in the .message
The part $p | .[$c].content does a generic object index fetch using the dynamic value of $c
Since the first argument of sub()/gsub() functions are a regex, the value captured $c needs to be created as \\$VARIABLE\\$
jqplay - Demo
Here's a basic JQ. Haven't tried with complex inputs, and haven't accommodated for $. I guess you can build on top of this -
to_entries | map(. as $kv | { "\($kv.key)": $kv.value.placeholders | to_entries | map(. as $p | $kv.value.message | sub("\\$\($p.key)\\$"; $p.value.content))[0]}) | add
output -
{
"some_label": "a string in English with a {variable}"
}
I'm using multiple IF AND statements in one cell and finding operator missing error 509. It worked with fewer variables just not sure the syntax is correct for calc here.
tried using nested statement with error 509 returned as well.
=IF(M5="Statement 1";L5;K5)IF(AND(M6="Tax";A5=A6); | L6;K6);IF(AND(M7="Discounts";A7=A6); | L7;K7);IF(AND(M8="Alternate";A8=A7); | L8;K8);IF(AND(M9="Other";A9=A8); | L9;K9);IF(AND(M10="Local";A10=A9); | L10;K10);IF(AND(M11="State";A11=A10); | L11;K11)
Desired outcome: Trying to get this output pending all values are true: L5 | L6 | L7 | L8 | L9 | L10 | L11
Current outcome: Error:509 which is Operator Missing error.
=IF(M5="Statement 1";L5;K5) missing& IF(AND(M6="Tax";A5=A6); missing & followed by quotes | missing quotes followed by & L6;K6) ; does not belong here should be &;IF(AND(M7="Discounts";A7=A6); | L7;K7);IF(AND(M8="Alternate";A8=A7); | L8;K8);IF(AND(M9="Other";A9=A8); | L9;K9);IF(AND(M10="Local";A10=A9); | L10;K10);IF(AND(M11="State";A11=A10); | L11;K11)
=if(M5="Statement 1";L5;K5)&if(AND(M6="Tax";A5=A6); "|" & L6;K6)&if(AND(M7="Discounts";A7=A6); "|" & L7;K7)&if(AND(M8="Alternate";A8=A7); "|" & L8;K8)&if(AND(M9="Other";A9=A8); "|" & L9;K9)&if(AND(M10="Local";A10=A9); "|" & L10;K10)&if(AND(M11="State";A11=A10); "|" & L11;K11)
Let's say you want to print out one of those great ASCII art images. How can you do that without cout each line individually?
Adjacent string literals are concatenated, so you can do this:
cout << " _______________________ _______ _ _______ _______ _______ _______ _ _______ \n"
"( ____ \__ __/ ___ ) ____ \ \ /\ ( ___ )\ /| ____ \ ____ )( ____ \ \ ( ___ )\ /|\n"
"| ( \/ ) ( | ( ) | ( \/ \ / / | ( ) | ) ( | ( \/ ( )|| ( \/ ( | ( ) | ) ( |\n"
"| (_____ | | | (___) | | | (_/ / | | | | | | | (__ | (____)|| (__ | | | | | | | _ | |\n"
"(_____ ) | | | ___ | | | _ ( | | | | ( ) ) __) | __)| __) | | | | | | |( )| |\n"
" ) | | | | ( ) | | | ( \ \ | | | |\ \_/ /| ( | (\ ( | ( | | | | | | || || |\n"
"/\____) | | | | ) ( | (____/\ / \ \ | (___) | \ / | (____/\ ) \ \__| ) | (____/\ (___) | () () |\n"
"\_______) )_( |/ \|_______/_/ \/ (_______) \_/ (_______// \__/|/ (_______/_______)_______)\n";
Or, more accurately, perhaps:
cout << " .::/- \n"
" .+++/ \n"
" `.::` /+++. \n"
" -////. :+++- \n"
" .////-` .+++/` \n"
" `:///:` `/++/. \n"
" ..` -////. -+++: \n"
" :+++:-` .////:` ./++/` \n"
" `-/+++++/-. `:////.`:++/. \n"
" `.:/++++/:.` -////..:--` \n"
" .-/+++++/-..::.` \n"
" `:::-..`` `.:/++++- \n"
" -++++++///:--.```.-/- \n"
" `.--:///++++++//::. \n"
"`--. ``..-::///+/``--- -+- ./oso- /++: \n"
"-oo+ -::::----....````... `ooo :s- /mo -dmmhy:`hmmo \n"
"-oo+ /+++++++++++++++++/. `ooo om: /mo ```` ``` ``` ``.`` ``` `.`` ommd`` `hmmo ``.`` ``` ``` ``` \n"
"-oo+ ...----::::////+++/` `ooo `/ssyss+:`.ohmyoo` .+ssyss+- -+syys+- /mo -o+. .ohdmmdho- -hdd/ `sdds` :shmmmdy/` .hddshdmmhoydmmmhy:`hmmo .+hdmmmds- .ddd/ .ddh- +ddh. \n"
"-oo+ ``````````````````` `ooo .yh-.``-/- .sm/.` `/o-```-sd+ .sd+-..-++` /mo .odo. :dmmy+/smmm: +mmh- /mmd- +mmh+:/smmy- .dmmdo/+s:`/ymmm++.`hmmo .dmmh++smmd+`ommd` `ymmmy .hmm+ \n"
"-oo+ +oooooooooooooooooo- `ooo -dy. om: -dy` +m/ /mo`+dy- `smmy` `smmy``smms`.hmm/ -dmd+---:hmmo`.dmm+ ommd `hmmo ommh. ommh..ymm+ +mmdmm/ ommy. \n"
"-oo+ /++++++++++++++++++. `ooo -oyhyyys/` om: `:osyyyyymy``sm- /myhyhd: `smms +mmh` `dmm/smms :dmmddddddddo`.dmm/ ommd `hmmo smmy` /mmd. :dmd+dmy-ymd+hmd: \n"
"-oo+ `ooo ``.+do om: /do. -dy``om: /md/``od+` `ommh. `ymmy` :dmmmmy. .hmd/`````.` .dmm/ ommd hmmo +mmh- smmy` `smmmmm- :dmmmmo \n"
"-oo+:::::::::::::::::::::::/ooo -+:.```.od+ +mo.` /do.```.omy` .sd/.``.//` /mo +dy. -ymmdysdmmh- +mmmh- :dmmyoosdd+` .dmm/ ommd ommmso.`ymmdyshmmh: .hmmm+ +mmmd` \n"
"-oooooooooooooooooooooooooooooo ./syyyyyo:` `:sys.`:syyyys+yo` `:syyyyo:` :h/ :ys` `:shddhs/` `ohy/ ./shddhy+- .shh: /hhy `:syhs. `:oyhdhs/. /hho` `shh/ \n"
More sensibly, use endl. This is subtly different from just "\n" after each line, because you'll also flush the output stream.
try something like:
cout << R"(place multiple lines
of text here
and it will display exactly
as you have it between the two brackets,
line feeds and all.)";
...the above code will also allow you to use characters like the backslash \ without needing two of them, it displays everything and doesn't recognize control codes, like \n etc. Very handy.
This is called a "string literal" and was added in C++11. You can find more information on the commands here, specifically refer to the prefix "R" which is for raw_characters: https://en.cppreference.com/w/cpp/language/string_literal
Others have already suggested using endl. While this isn't (necessarily) a bad thing, using endl flushes the stream's buffer along with writing a new-line. Contrary to the implication in one of the answers you've gotten, using endl does not help (at all) with translating the new-line to whatever character sequence the platform normally uses to signal the end of a line. Using newline is guaranteed to be precisely equivalent to stream << "\n" << flush;". Translating new-lines to "\r", or "\n" or "\r\n", or whatever the platform prefers, is done at a different level and newline has nothing to do with it.
The flush that it does, however, can (and often will) slow down your I/O, sometimes by quite a considerable margin. As long as you're only writing a few lines (e.g. a couple hundred characters) it's probably completely irrelevant. If you're writing a large file, however, using endl instead of "\n" can easily result in a 10x slowdown (in fact, I'd go so far as to say that much of the idea that iostreams are slow stems directly from using endl).
That's not to say there's never any reason to use endl. The flush assures that whatever has been written to the stream is immediately flushed out of the standard library's buffer, and sent to the OS. If you want to assure immediate display, endl can be useful. Likewise, if you're doing logging, and it's critical that your log always reflect the most recent known state of a program, endl can be (extremely) useful to assure that what you've written really gets logged, not lost in a buffer when/if the application crashes.
So, endl makes sense at times, but probably 95% of the time that it's used, it's really inappropriate (e.g., it's unlikely to accomplish anything useful in any of the answers to this question).
It's pretty simple thankfully. Just use endl whenever you want to start another line.
cout << stuff << endl
<< morestuff << endl
<< evenmorestuff << endl;
I would like to state that I much prefer using endl because it should work even if you are on a platform that requires "\r\n" instead of just "\n".
((https?|ftp)://|www.)(\S+[^.*])
I would like this expression to check for . in succession to each other. If it finds two or more periods back to back, the expression should fail. On the other hand, if it succeeds, I want it to match every character and/or symbol up until the first white space encountered.
In other words:
www.yahoo..com should fail
On a related note: I realize that this expression is very basic in terms of judging valid URL structure. I have another "more intelligent" regular expression in place that precedes the one above. The purpose of the posted one is meant to check the validity of the URL that is passed from the initial regular expression via preg_match_all.
You may awnt to check out FILTER_VALIDATE_URL with http://php.net/manual/en/book.filter.php instead of using Regex to validate your URLS.
Here's example usage:
$url = "http://www.example.com";
if(!filter_var($url, FILTER_VALIDATE_URL))
{
echo "URL is not valid";
}
else
{
echo "URL is valid";
}
You can do something like this:
((https?|ftp)\:\/\/|www.)((?:[\w\-]+\.)*[\w\-]+)
This will not yet check for valid URLs, even if you skip double dots. I'd advise not to use regex if the language you're using (PHP?) has other means of validating an URL.
The RFC states the following:
; URL schemeparts for ip based protocols:
ip-schemepart = "//" login [ "/" urlpath ]
login = [ user [ ":" password ] "#" ] hostport
hostport = host [ ":" port ]
host = hostname | hostnumber
hostname = *[ domainlabel "." ] toplabel
domainlabel = alphadigit | alphadigit *[ alphadigit | "-" ] alphadigit
toplabel = alpha | alpha *[ alphadigit | "-" ] alphadigit
alphadigit = alpha | digit
hostnumber = digits "." digits "." digits "." digits
port = digits
user = *[ uchar | ";" | "?" | "&" | "=" ]
password = *[ uchar | ";" | "?" | "&" | "=" ]
urlpath = *xchar ; depends on protocol see section 3.1
; HTTP
httpurl = "http://" hostport [ "/" hpath [ "?" search ]]
hpath = hsegment *[ "/" hsegment ]
hsegment = *[ uchar | ";" | ":" | "#" | "&" | "=" ]
search = *[ uchar | ";" | ":" | "#" | "&" | "=" ]
; Miscellaneous definitions
lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" |
"i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" |
"q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" |
"y" | "z"
hialpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" |
"J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" |
"S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"
alpha = lowalpha | hialpha
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
"8" | "9"
safe = "$" | "-" | "_" | "." | "+"
extra = "!" | "*" | "'" | "(" | ")" | ","
national = "{" | "}" | "|" | "\" | "^" | "~" | "[" | "]" | "`"
punctuation = "<" | ">" | "#" | "%" | <">
reserved = ";" | "/" | "?" | ":" | "#" | "&" | "="
hex = digit | "A" | "B" | "C" | "D" | "E" | "F" |
"a" | "b" | "c" | "d" | "e" | "f"
escape = "%" hex hex
unreserved = alpha | digit | safe | extra
uchar = unreserved | escape
xchar = unreserved | reserved | escape
digits = 1*digit
Using negative lookahead is an easy way if your engine supports it:
(?!.*\.\.)((https?|ftp)\:\/\/|www.)(\S+[^.*])
Otherwise, you have to be more specific:
^((https?|ftp)\:\/\/|www.)((\.[^.]|[^.\s])+[^.*])($|\s+)