Verilog HDL syntax error at practice.v(7) near text "or"; expecting ")" - syntax-error

I have changed the setting to the same name as the module. There are no other Verilog files in the folder where this file is stored. I don't know what is wrong with the grammar.
module test(A,B,F);
input A;
input [1:0]B;
output F;
reg F;
always #(*)
if({A,B}==3'b001 or {A,B}==3'b010 or {A,B}==3'b100 or {A,B}==3'b101)
F=1;
else F=0;
endmodule

or is a legal keyword in Verilog, but it can not be used in that context. It can be used as a built-in primitive OR gate, or in a sensitivity list.
You likely want it to behave as the logical OR operator, in which case you would use || instead:
if({A,B}==3'b001 || {A,B}==3'b010 || {A,B}==3'b100 || {A,B}==3'b101)
Alternately, you could enable SystemVerilog features in your tools and use the set membership inside operator:
module test(A,B,F);
input A;
input [1:0]B;
output F;
assign F = ({A,B} inside {3'b001, 3'b010, 3'b100, 3'b101});
endmodule
This code is simpler to understand, and it scales better since it is easier to add/remove a comparison value.

The logical OR operator in Verilog is ||, not or.

Related

Why is the order of evaluation of expressions used for concatenation undefined in Awk?

In GNU Awk User's Guide, I went through the section 6.2.2 String Concatenation and found interesting insights:
Because string concatenation does not have an explicit operator, it is often necessary to ensure that it happens at the right time by using parentheses to enclose the items to concatenate.
Then, I was quite surprised to read the following:
Parentheses should be used around concatenation in all but the most common contexts, such as on the righthand side of ‘=’. Be careful about the kinds of expressions used in string concatenation. In particular, the order of evaluation of expressions used for concatenation is undefined in the awk language. Consider this example:
BEGIN {
a = "don't"
print (a " " (a = "panic"))
}
It is not defined whether the second assignment to a happens before or after the value of a is retrieved for producing the concatenated value. The result could be either ‘don't panic’, or ‘panic panic’.
In particular, in my GNU Awk 5.0.0 it performs like this, doing the replacement before printing the value:
$ gawk 'BEGIN {a = "dont"; print (a " " (a = "panic"))}'
dont panic
However, I wonder: why isn't the order of evaluation of expressions defined? What are the benefits of having "undefined" outputs that may vary depending on the version of Awk you are running?
This particular example is about expressions with side-effects. Traditionally, in C and awk syntax (closely inspired by C), assignments are allowed inside expressions. How those expressions are then evaluated is up to the implementation.
Leaving something unspecified would make sure that people don't use potentially confusing or ambiguous language constructs. But that assumes they are aware of the lack of specification.

String interpolation in Perl6

I have difficulty figuring out why the statement
say "\c500";
produces the character 'Ǵ' on my screen as expected, while the following statements give me an error message at compile time ("Unrecognized \c character"):
my $i = 500;
say "\c$i";
even though
say "$i"; # or 'say $i.Str;' for that matter
produces "500" (with "$i".WHAT indicating type Str).
You'll have to use $i.chr, which is documented here. \c is handled specially within strings, and does not seem to admit anything that is not a literal.
The string literal parser in Perl 6 is a type of domain specific language.
Basically what you write gets compiled similarly to the rest of the language.
"abc$_"
&infix:«~»('abc',$_.Str)
In the case of \c500, you could view it as a compile-time constant.
"\c500"
(BEGIN 500.chr)
Actually it is more like:
(BEGIN 500.HOW.find_method_qualified(Int,500,'chr').(500))
Except that the compiler for string literals actually tries to compile it to an abstract syntax tree, but is unable to because there hasn't been code added to handle this case of \c.
Even if there was, \c is effectively compiled to run at BEGIN time, which is before $_ has a value.
Also \c is used for more than .chr
"\c9" eq "\c[TAB]" eq "\cI" eq "\t"
(Note that \cI represents the character you would get by typing Cntrl+Alt+i on a posix platform)
So which of these should \c$_ compile to?
$_.chr
$_.parse-names
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.index($_).succ.chr
If you want .chr you can write it as one of the following. (spaces added where they are allowed)
"abc$_.chr( )def"
"abc{ $_.chr }def"
"abc{ .chr }def"
'abc' ~ $_.chr ~ 'def'

Do all Perl 6 quoting constructs have term precedence?

The < > has term precedence. Here's the example from the docs:
say <a b c>[1];
I figured the same precedence would apply to all of the quoting operators. This works:
my $string = '5+8i';
my $number = <<$string>>;
say $number;
This interpolates $string and creates allomorphes (in this case a ComplexStr):
(5+8i)
But, if I try to index it like the example from the docs, it doesn't compile:
my $string = '5+8i';
my $number = <<$string>>[0];
say $number;
I'm not quite sure what Perl 6 thinks is happening here. Perhaps it's thinking it's a hyperoperator:
===SORRY!=== Error while compiling ...
Cannot use variable $number in declaration to initialize itself
at /Users/brian/Desktop/scratch.pl:6
------> say $⏏number;
expecting any of:
statement end
statement modifier
statement modifier loop
term
I can skip the variable:
my $string = '5+8i';
say <<$string>>[0];
But that's a different error that can't find the closing quotes:
===SORRY!=== Error while compiling ...
Unable to parse expression in shell-quote words; couldn't find final '>>'
at /Users/brian/Desktop/scratch.pl:8
------> <BOL>⏏<EOL>
expecting any of:
statement end
statement modifier
statement modifier loop
I think this warrants a rakudobug email. I think the parser gets confused trying to interpret it as a hyper (aka >>.method). The following workaround seems to corroborate this:
my $string = '5+8i';
my $number = <<$string >>[0]; # note space before >>
say $number;
To satisfy your OCD, you could probably also put a space before $string.
And yes, whitespace is not meaningless in Perl 6.
Jonathan has the answer in response to RT #131695.
The >>[] is a postfix operator to index a list, so it tries to use it as such. This is intended behavior. Fair enough, although I think the parser is being a bit too clever for regular code monkeys here.

Use variable in GAMS dollar

I have a GAMS code where I want an if statement. I've read about dollar condition and I thought it could help me. Reading GAMS user guide, it says:
The Dollar Condition
This section introduces the dollar operator , which is one of the most powerful features of GAMS. The dollar operator operates with a logical condition. The term $(condition) can be read as 'such that condition is valid' where condition is a logical condition.
Attention:
The dollar logical conditions cannot contain variables. Variable attributes (like .l and .m) are permitted however.
The dollar operator is used to model conditional assignments, expressions, and equations. The following subsection provides an example that will clarify its use. The next section will deal individually with the topic of using dollar conditions to model conditional assignments, expressions, and equations respectively.
I have tryed it in my code, but still I found always the same error:
*** Error 53 in C:\route\Filename.gms
Endogenous $ operation not allowed
This is my actual code:
ACUMULADO_FIN_GRUPOS(k,l,t)..
GA(k,l,t)$(GA(k,l,t) GE GT(k,l)) =E= 0 ;
(I want to change value of a variable to 0 if it is greater or equal to another variable). I have also tryed with .l attribute:
ACUMULADO_FIN_GRUPOS(k,l,t)..
GA(k,l,t)$(GA(k,l,t).l GE GT(k,l).l) =E= 0 ;
but then next error appears (just in the .l definition)
*** Error 8 in C:\route\Filename.gms
')' expected
Please, could anyone help?
Thanks in advance!!
I have seen that my previous code was not correct, the correct way is:
ACUMULADO_FIN_GRUPOS(k,l,t)..
GA(k,l,t)$(GA.l(k,l,t) GE GT.l(k,l)) =E= 0 ;
with the .l previous to set definition.

YACC or Bison Action Variables positional max value

In YACC and other Yacc like programs. There are action positional variables for the current parsed group of tokens. I might want to process some csv file input that the number of columns changes for unknown reasons. With my rules quoted_strings and numbers can be one or more instances found.
rule : DATE_TOKEN QUOTED_NUMBERS q_string numbers { printf(..... $1,$2....}
q_string
: QUOTED_STRING
| QUOTED_STRING q_string
;
numbers
: number numbers
| number
;
number
: INT_VALUE
| FLOAT_VALUE
;
Actions can be added to do things with what ever has been parsed as is
{ printf("%s %s %s \n",$<string>1, $<string>1, $<string>1); }
Is there a runtime macro, constuct or variable that tells me how many tokens have been read so that I can write a loop to print all token values?
What is $max
The $n variables in a bison action refer to right-hand side symbols, not to tokens. If the corresponding rhs object is a non-terminal, $n refers to that non-terminal's semantic value, which was set by assigning to $$ in the semantic action of that nonterminal.
So if there are five symbols on the right-hand side of a rule, then you can use $1 through $5. There is no variable notation which allows you to refer to the "nth" symbol.