I am new in solidity and General with coding.
i use this : Truffle v5.5.21 (core: 5.5.21) ,Ganache v7.2.0,Solidity v0.5.16 (solc-js)
Node v12.22.12,Web3.js v1.7.4.
I took this error and i cant fix it. I checked more interesting information's about my project but the problem is problem...I would be grateful if I could have some help
(in terminal)
CompileError: test:/contracts/EmailRegex.sol:49:34: ParserError: Expected ',' but got identifier
cur = state(cur).func(uint c);
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.22 <0.9.0;
library EmailRegex {
struct State {
bool accepts;
function (byte) internal pure returns (uint) func;
}
function state(uint id) internal pure returns (State memory) {
if (id == 1) {
return State(false, s1);
}
if (id == 2) {
return State(false, s2);
}
if (id == 3) {
return State(false, s3);
}
if (id == 4) {
return State(false, s4);
}
if (id == 5) {
return State(false, s5);
}
if (id == 6) {
return State(false, s6);
}
if (id == 7) {
return State(false, s7);
}
if (id == 8) {
return State(false, s8);
}
if (id == 9) {
return State(true, s9);
}
if (id == 10) {
return State(true, s10);
}
}
function matches(string memory input) public pure returns (bool) {
uint cur = 1;
for (uint i = 0; i < bytes(input).length; i++) {
uint8 c = uint8(bytes(input)[i]);
cur = state(cur).func(uint c);
if (cur == 0) {
return false;
}
}
return state(cur).accepts;
}
function s1(uint8 c) internal pure returns (uint) {
if (c == 37 || c == 43 || c == 45 || c == 46 || c == 57 || c >= 65 && c <= 90 || c == 95 || c >= 97 && c <= 122) {
return 2;
}
return 0;
}
function s2(uint8 c) internal pure returns (uint) {
if (c == 37 || c == 43 || c == 45 || c == 46 || c >= 48 && c <= 57 || c >= 65 && c <= 90 || c == 95 || c >= 97 && c <= 122) {
return 3;
}
if (c == 64) {
return 4;
}
return 0;
}
function s3(uint8 c) internal pure returns (uint) {
if (c == 37 || c == 43 || c == 45 || c == 46 || c >= 48 && c <= 57 || c >= 65 && c <= 90 || c == 95 || c >= 97 && c <= 122) {
return 3;
}
if (c == 64) {
return 4;
}
return 0;
}
function s4(uint8 c) internal pure returns (uint) {
if (c >= 46 && c <= 47 || c >= 48 && c <= 57 || c >= 58 && c <= 64 || c >= 65 && c <= 90 || c >= 91 && c <= 95 || c >= 97 && c <= 122) {
return 5;
}
return 0;
}
function s5(uint8 c) internal pure returns (uint) {
if (c == 46) {
return 6;
}
if (c == 47 || c >= 48 && c <= 57 || c >= 58 && c <= 64 || c >= 65 && c <= 90 || c >= 91 && c <= 95 || c >= 97 && c <= 122) {
return 7;
}
return 0;
}
function s6(uint8 c) internal pure returns (uint) {
if (c == 46) {
return 6;
}
if (c == 47 || c >= 48 && c <= 57 || c >= 58 && c <= 64 || c >= 91 && c <= 95) {
return 7;
}
if (c >= 65 && c <= 90 || c >= 97 && c <= 122) {
return 8;
}
return 0;
}
function s7(uint8 c) internal pure returns (uint) {
if (c == 46) {
return 6;
}
if (c == 47 || c >= 48 && c <= 57 || c >= 58 && c <= 64 || c >= 65 && c <= 90 || c >= 91 && c <= 95 || c >= 97 && c <= 122) {
return 7;
}
return 0;
}
function s8(uint8 c) internal pure returns (uint) {
if (c == 46) {
return 6;
}
if (c == 47 || c >= 48 && c <= 57 || c >= 58 && c <= 64 || c >= 91 && c <= 95) {
return 7;
}
if (c >= 65 && c <= 90 || c >= 97 && c <= 122) {
return 9;
}
return 0;
}
function s9(uint8 c) internal pure returns (uint) {
if (c == 46) {
return 6;
}
if (c == 47 || c >= 48 && c <= 57 || c >= 58 && c <= 64 || c >= 91 && c <= 95) {
return 7;
}
if (c >= 65 && c <= 90 || c >= 97 && c <= 122) {
return 10;
}
return 0;
}
function s10(uint8 c) internal pure returns (uint) {
if (c == 46) {
return 6;
}
if (c == 47 || c >= 48 && c <= 57 || c >= 58 && c <= 64 || c >= 91 && c <= 95) {
return 7;
}
if (c >= 65 && c <= 90 || c >= 97 && c <= 122) {
return 10;
}
return 0;
}
}
Lets see your code:
cur = state(cur).func(uint c);
You are defining the input variable inside the function parameter, you can't do that, try the following:
byte c; // remember to set the value you see fit.
cur = state(cur).func(c);
I suggest you also check the rest of your code, since for some reason, you're also defining c in this loop, and variables are only defined once, yet you can reassign its value later on.
for (uint i = 0; i < bytes(input).length; i++) {
uint8 c = uint8(bytes(input)[i]);
if (cur == 0) {
return false;
}
}
Also, the interface for the function you specified on your struct's interface has a byte type input.
struct State {
bool accepts;
function (byte) internal pure returns (uint) func;
}
But, your sX functions has a uint8 input type instead. This will also cause error's
Can't see any other issues so far, yet I haven't tried compiling it.
Related
This is the code for Bulls and Cows game, simply it just we have 2 array a[] and b[] with the same length, if a[i] == b[i] then Bulls += 1, if a[i] in b && a[i] != b[i] then Cows += 1.
I have written the Bulls and Cows function, but the method BullCows have some problem when calculate it, it make my assert fail.
`
function bullspec(s:seq<nat>, u:seq<nat>): nat
requires |s| > 0
requires |u| > 0
requires |s| == |u|
{
var index:=0;
if |s| == 1 then (
if s[0]==u[0]
then 1 else 0
) else (
if s[index] != u[index]
then bullspec(s[index+1..],u[index+1..])
else 1+bullspec(s[index+1..],u[index+1..])
)
}
function cowspec(s:seq<nat>, u:seq<nat>): nat
requires |s| > 0
requires |u| > 0
requires |s| <= |u|
{
var extra:= |u|-|s|;
var index:=0;
if |s| == 1 then (
if s[0] in u
then 1 else 0
) else(
if s[index] in u && s[index]!=u[extra]
then (1+ cowspec(s[index+1..],u))
else cowspec(s[index+1..],u)
)
}
method BullsCows (s:seq<nat>, u:seq<nat>) returns (b:nat, c:nat)
requires |s|>0 && |u|>0 &&|s|==|u|
// No duplicates in array
requires forall i, j | 0 <= i < |s| && 0 <= j < |s| && i != j :: s[i] != s[j]
requires forall i, j | 0 <= i < |u| && 0 <= j < |u| && i != j :: u[i] != u[j]
ensures forall k :: 0 <= k < |s| && s[k] !in u ==> b == c == 0
ensures forall k :: 0 <= k < |s| && s[k] in u ==> (c + b) > 0
{
var index := 0;
b := 0;
c := 0;
while(index<|s|)
invariant index <= |s|
invariant forall k :: 0 <= k < index && s[k] in u ==> (b + c) > 0
{
if s[index] in u {
if s[index] == u[index]{
b:=b+1;
} else {
c:=c+1;
}
}
index:=index + 1;
}
}
method NotMain()
{
var sys:seq<nat> := [4,2,9,3,1];
var usr:seq<nat> := [1,2,3,4,5];
assert bullspec(sys, usr) == 1; //True
assert cowspec(sys, usr) == 3; //True
var b:nat, c:nat := BullsCows(sys, usr);
assert b == 1; //Not true
assert c == 3; //Not true
}
`
The method NotMain said that assert b == 1; and assert c==3; are not true, this is Dafny language, please could someone help me with this logical, I'm banging my head.
I try put on many ensures in the BullsCows method but there's nothing happen
The problem is that the postcondition on BullsCows() is not strong enough to prove the final two assertions. In particular, there is no connection between the operation of BullsCows() and the specifications bullspec() and cowspec(). You need to connect these things together.
To illustrate I'm going to use a simpler example which is easier to follow. As for your example above, the final assertion fails in the following:
function sumspec(xs: seq<nat>, i: nat) : nat
requires i <= |xs| {
if |xs| == 0 || i == 0 then 0
else sumspec(xs,i-1) + xs[i-1]
}
method sum(s:seq<nat>) returns (r:nat) {
r := 0;
for i := 0 to |s| {
r := r + s[i];
}
return r;
}
method main() {
assert sumspec([1,2,3],3) == 6;
var r := sum([1,2,3]);
assert r == 6;
}
Whilst sumspec() is a valid specification for sum() we have not connected these two things. As such, Dafny assumes that sum() can return any value for r!
To connect the specification (sumspec) with its implementation (sum) we need a stronger postcondition:
method sum(s:seq<nat>) returns (r:nat)
ensures r == sumspec(s,|s|) {
r := 0;
for i := 0 to |s|
invariant r == sumspec(s,i) {
r := r + s[i];
}
return r;
}
Here, r == sumspec(s,|s|) connects the specification with the result of our implementation. We also added a loop invariant to help Dafny show this postcondition holds.
"value" can only either be 0, 60, 120, 180, 240 or 300.
When I try to count each change that "value" has using iterations I'm not getting values of +1. It gives values which are stepping by 10000. Also I do not wish to iterate when a value is reached, I only wish to iterate when it has changed one of the 6 values.
#include <stdio.h>
int angles;
int counter[6] = {0, 60, 120, 180, 240, 300};
if (a == 1 && b == 1 && c == 0)
{
value = 0;
}
if (a == 1 && b == 0 && c == 0)
{
value = 60;
}
if (a == 1 && b == 0 && c == 1)
{
value = 120;
}
if (a == 0 && b == 0 && c == 1)
{
value = 180;
}
if (a== 0 && b == 1 && c == 1)
{
value= 240;
}
if (a == 0 && b == 1 && c == 0)
{
value = 300;
}
2**3 == 8
You are missing conditions for some of the possible combinations of values.
I would put the conditions into a function and return the desired value as soon as it has been determined because that allows to sort out the conditions. Doing that, you will inevitably notice that you do not know what to return in some cases.
Other than that, it´s not clear to me what you are trying to achieve.
Something like this:
#define ERROR -1
int get_value(int a, int b, int c)
{
if(a)
{
if(b)
{
return c ? ERROR : 0;
}
else
{
return c ? 60 : 120;
}
}
else
{
if(b)
{
return c ? 240 : 300;
}
else
{
return c ? 180 : ERROR;
}
}
return ERROR;
}
I would probably avoid all this, consider each of the three values as one bit and use bit operators to get to the desired value.
How to change 12345 to 54321?
With a string, you can change the string to a rune, and reverse it, but you cannot do the same for an integer. I have searched and found no one talking about this. Examples
131415 >>> 514131
1357 >>> 7531
123a >>> ERROR
-EDIT-
I was thinking, why not create a slice and index that?
Then I realized that you can't index int
(http://play.golang.org/p/SUSg04tZsc)
MY NEW QUESTION IS
How do you index an int?
OR
How do you reverse a int?
Here is a solution that does not use indexing an int
package main
import (
"fmt"
)
func reverse_int(n int) int {
new_int := 0
for n > 0 {
remainder := n % 10
new_int *= 10
new_int += remainder
n /= 10
}
return new_int
}
func main() {
fmt.Println(reverse_int(123456))
fmt.Println(reverse_int(100))
fmt.Println(reverse_int(1001))
fmt.Println(reverse_int(131415))
fmt.Println(reverse_int(1357))
}
Result:
654321
1
1001
514131
7531
Go playground
I converted the integer to a string, reverse the string, and convert the result back to a string.
package main
import (
"fmt"
"strconv"
)
func main() {
fmt.Println(reverse_int(123456))
fmt.Println(reverse_int(100))
fmt.Println(reverse_int(1001))
fmt.Println(reverse_int(131415))
fmt.Println(reverse_int(1357))
}
func reverse_int(value int) int {
intString := strconv.Itoa(value)
newString := ""
for x := len(intString); x > 0; x-- {
newString += string(intString[x - 1])
}
newInt, err := strconv.Atoi(newString)
if(err != nil){
fmt.Println("Error converting string to int")
}
return newInt
}
Very similar to the first answer but this checks to make sure you don't go out of bounds on the type.
func reverse(x int) int {
rev := 0
for x != 0 {
pop := x % 10
x /= 10
if rev > math.MaxInt32/10 || (rev == math.MaxInt32 /10 && pop > 7) {
return 0
}
if rev < math.MinInt32/10 || (rev == math.MinInt32/10 && pop < -8) {
return 0
}
rev = rev * 10 + pop
}
return rev
}
Also flips negative numbers int
func Abs(x int) int {
if x < 0 {
return -x
}
return x
}
func reverse_int(n int) int {
newInt := 0
sign := 1
if n < 0 {
sign = -1
}
n = Abs(n)
for n > 0 {
remainder := n % 10
newInt = newInt*10 + remainder
n /= 10
}
return newInt * sign
}
func main() {
fmt.Println(reverse_int(-100))
fmt.Println(reverse_int(-1001))
fmt.Println(reverse_int(131415))
fmt.Println(reverse_int(1357))
}
Similar to Fokiruna's answer but also checks for a 32bit overflow
func reverse(x int) int {
result, sign := 0, 1
if(x < 0) {
sign = -1
x = -x
}
for x > 0 {
remainder := x % 10;
result = result * 10 + remainder
x = x/10
}
var checkInt int = int(int32(result))
if checkInt != result {
return 0
}
return result * sign
}
I have the following zaMap (see full code here: http://rise4fun.com/Dafny/LCaM):
class zaMap {
var busybits :array<bool>;
var keys : array<int>;
var values : array<int>;
predicate wellformed ()
reads busybits, keys, values, this
{
busybits != null && keys != null && values != null &&
keys != values &&
busybits.Length == keys.Length &&
keys.Length == values.Length
}
// ... more predicates and methods follow
method put(k : int, v : int) returns (success : bool)
requires wellformed()
modifies busybits, keys, values
ensures !success ==> full()
ensures success ==> mapsto(k, v)
{
var i := findEmpty();
if (i < 0)
{
success := false;
return;
}
assert !busybits[i];
busybits[i] := true;
keys[i] := k;
values[i] := v;
success := true;
}
//...
Now I want to add more specifications to the put method. For example, I want to ensure, that if the return value is success == true, then a map was !full() before the function call, or equivalently if a map not full(), it is guaranteed to put there.
The problem is that, in the precondition "requires" I don't know yet what it will return, and in the postcondition "ensures" I don't have an original map anymore. What people do about that?
You can use the old keyword. Let's take a look at an example. The following method sets to zero all positions of an array containing the element x and leaving the rest as they are. Here's the code:
method setToZero(a: array<int>, x : int )
requires a != null;
modifies a;
ensures forall i :: 0 <= i < a.Length && old(a[i]) == x ==> a[i] == 0;
ensures forall i :: 0 <= i < a.Length && old(a[i]) != x ==> a[i] == old(a[i]);
{
var j := 0;
while (j < a.Length)
invariant 0 <= j <= a.Length;
invariant forall i :: 0 <= i < j && old(a[i]) == x ==> a[i] == 0;
invariant forall i :: 0 <= i < j && old(a[i]) != x ==> a[i] == old(a[i]);
invariant forall i :: j <= i < a.Length ==> a[i] == old(a[i]);
{
if (a[j] == x) {
a[j] := 0;
}
j := j + 1;
}
}
void __fastcall TForm1::Step(int _Sum, int _Num, int _Val)
{
if (_Sum <= 0 || _Num <= 0 || _Sum < _Val) return;
if (_Num == 1)
{
data[theK - 1] = _Sum;
Memo1->Lines->Add("{");
for (int i = 0; i < theK; ++i)
Memo1->Lines->Add(data[i]);
sc++;
Memo1->Lines->Add("}");
Memo1->Lines->Add("\n");
Label4->Caption = sc;
return;
}
for (int i = _Val; i < _Sum; ++i)
{
data[theK - _Num] = i;
Step(_Sum - i, _Num - 1, i);
}
}
I have output like this:
1 2 3 4 5 6 7 8 9 10
11 12
But I need output like:
1,2,3,4 5,6,7,8 9,10,11,12
How to do it in C++ Builder? I have Memo1 or ListBox. I need formatting output.
I usually build a UnicodeString for each line of output like so:
UnicodeString Str;
for (int I =0; I<5;i++)
{
Str += IntToStr(I)+","
};
Str.SetLength(Str.Length -1); // trim off last comma
Memo1->Add(Str);