semicolon single expression in a for loop - syntax

Why is there a semicolon at the end of Proc.num_stack_slots.(i) <- 0 in the following code?
I thought semicolons are separators in OCaml. Can we always put an optional semicolon for the last expression of a block?
for i = 0 to Proc.num_register_classes - 1 do
Proc.num_stack_slots.(i) <- 0;
done;
See https://github.com/def-lkb/ocaml-tyr/blob/master/asmcomp/coloring.ml line 273 for the complete example.

There is no need for a semicolon after this expression, but as a syntactic courtesy, it is allowed here. In the example, you referenced, there is a semicolon, because after it a second expression follows.
Essentially, you can view a semicolon as a binary operator, that takes two-unit expressions, executes them from left to right, and returns a unit.
val (;): unit -> unit -> unit
then the following example will be more understandable:
for i = 1 to 5 do
printf "Hello, ";
printf "world\n"
done
here ; works just a glue. It is allowed to put a ; after the second expression, but only as the syntactic sugar, nothing more than a courtesy from compiler developers.
If you open a parser definition of the OCaml compiler you will see, that an expression inside a seq_expr can be ended by a semicolumn:
seq_expr:
| expr %prec below_SEMI { $1 }
| expr SEMI { reloc_exp $1 }
| expr SEMI seq_expr { mkexp(Pexp_sequence($1, $3)) }
That means that you can even write such strange code:
let x = 2 in x; let y = 3 in y; 25

Related

[] reduce with anonymous function in Perl 6

We can use reduce with a sub with two arguments, putting it in double brackets:
> sub mysum { $^a + $^b }
> [[&mysum]] 1,3,5
9
But what if we want to use an anonymous function instead?
Both following variants produce a compile error:
> [[&{ $^a + $^b }]] 1,3,5
> [[{ $^a + $^b }]] 1,3,5
You are not allowed to have any spaces in that form of reduce.
> [[&({$^a+$^b})]] 1, 3, 5
9
This is so that it is more obvious that it is a reduce, and not an array declaration.
> [ { $^a + $^b }, { $^a * $^b } ].pick.(3,5)
8 | 15
The double [[…]] is just an extension of allowing any function to be used as an infix operator.
Note that you must use &(…) in this feature, when not talking about a named function &foo, or an already existing infix operator.
> 3 [&( { $^a + $^b } )] 5
8
This is sort-of an extension of using […] for bracketing meta operators like Z and =
> #a [Z[[+]=]] 1..5
> #a Z[[+]=] 1..5
> #a Z[+=] 1..5
> #a Z+= 1..5
Not sure why that doesn't work. But there's always:
say reduce { $^a + $^b }, 1,3,5 # 9
I'm guessing you knew that but it's all I've got for tonight. :)
I've now moved my comment here and expanded it a bit before I go to sleep.
The TTIAR error means it fails to parse the reduce as a reduce. So I decided to take a quick gander at the Perl 6 grammar.
I searched for "reduce" and quickly deduced that it must be failing to match this regex.
While that regex might be only 20 or so lines long, and I recognize most of the constructs, it's clearly not trivial. I imagine there's a way to use Grammar::Debugger and/or some other grammar debugging tool with the Perl 6 grammar but I don't know it. In the meantime, you must be a bit of a regex whiz by now, so you tell me: why doesn't it match? :)
Update
With Brad's answer to your question as our guide, the answer to my question is instantly obvious. The first line of the regex proper (after the two variable declarations) directly corresponds to the "no spaces" rule that Brad revealed:
<?before '['\S+']'>
This is an assertion that the regex engine is positioned immediately before a string that's of the form [...] where the ... is one or more non-space characters. (\s means space, \S means non-space.)
(Of course, I'd have been utterly baffled why this non-space rule was there without Brad's answer.)

What is the empty statement in Golang?

In Python we can use pass clause as an placeholder.
What is the equivalent clause in Golang?
An ; or something else?
The Go Programming Language Specification
Empty statements
The empty statement does nothing.
EmptyStmt = .
Notation
The syntax is specified using Extended Backus-Naur Form (EBNF):
Production = production_name "=" [ Expression ] "." .
Expression = Alternative { "|" Alternative } .
Alternative = Term { Term } .
Term = production_name | token [ "…" token ] | Group | Option | Repetition .
Group = "(" Expression ")" .
Option = "[" Expression "]" .
Repetition = "{" Expression "}" .
Productions are expressions constructed from terms and the following
operators, in increasing precedence:
| alternation
() grouping
[] option (0 or 1 times)
{} repetition (0 to n times)
Lower-case production names are used to identify lexical tokens.
Non-terminals are in CamelCase. Lexical tokens are enclosed in double
quotes "" or back quotes ``.
The form a … b represents the set of characters from a through b as
alternatives. The horizontal ellipsis … is also used elsewhere in the
spec to informally denote various enumerations or code snippets that
are not further specified. The character … (as opposed to the three
characters ...) is not a token of the Go language.
The empty statement is empty. In EBNF (Extended Backus–Naur Form) form: EmptyStmt = . or an empty string.
For example,
for {
}
var no
if true {
} else {
no = true
}

Conflicts in ocamlyacc

I am trying to write a parser for a simple language that recognizes integer and float expressions using ocamlyacc. However I want to introduce the possiblity of having variables. So i defined the token VAR in my lexer.mll file which allows it to be any alphanumneric string starting with a capital letter.
expr:
| INT { $1 }
| VAR { /*Some action */}
| expr PLUS expr { $1 + $3 }
| expr MINUS expr { $1 - $3 }
/* and similar rules below for real expressions differently */
Now i have a similar definition for real numbers. However when i run this file, I get 2 reduce/reduce conflict because if i just enter a random string(identified as token VAR). The parser would not know if its a real or an integer type of variable as the keyword VAR is present in defining both int and real expressions in my grammar.
Var + 12 /*means that Var has to be an integer variable*/
Var /*Is a valid expression according to my grammar but can be of any type*/
How do I eliminate this reduce/reduce conflict without losing the generality of variable declaration and mainting the 2 data types available to me.

If statement not working - what is wrong with this syntax?

I get an "Expected Identifier" message against the if line. Any ideas why?
if ([inputA.text isEqualToString:#""]) && ([inputB.text <> isEqualToString:#""]) {
c = 1;
}
I'm trying to say it both inputs are empty...
I presume there isn't an easier way to say if the text is null in Obj C++?
An if statement requires that its condition expression be enclosed in parentheses. You have a compound expression. You've used parentheses around the subexpressions of the logical AND operation (&&), but you haven't surrounded the entire expression in parentheses. (The subexpressions don't actually require parentheses in this case, but they don't hurt.)
Next, you have a random <> in the second subexpression. What is that doing in there? In some languages that is the "not equal" operator, but a) it's not an operator in C or Objective-C, b) it wouldn't go inside a message-send expression like that, and c) you claim you were trying to check that both inputs are empty, so I wouldn't expect you to try to negate the test for equality with the empty string.
So, fixing just those problems yields:
if ([inputA.text isEqualToString:#""] && [inputB.text isEqualToString:#""]) {
c = 1;
}
That said, pie's answer is good, too. It also works if either of the inputs has a nil text property, too.
if ([inputA.text length]==0 && [inputB.text length]==0)
{
c = 1;
}

Remove whitespace, but do so last?

I am attempting to parse Lua, which depends on whitespace in some cases due to the fact that it doesn't use braces for scope. I figure that by throwing out whitespace only if another rule doesn't match is the best way, but i have no clue how to do that. Can someone help me?
Looking at Lua's documentation, I see no need to take spaces into account.
The parser rule ifStatement:
ifStatement
: 'if' exp 'then' block ('elseif' exp 'then' block 'else' block)? 'end'
;
exp
: /* todo */
;
block
: /* todo */
;
should match both:
if j==10 then print ("j equals 10") end
and:
if j<10 then
print ("j < 10")
elseif j>100 then
print ("j > 100")
else
print ("j >= 10 && j <= 100")
end
No need to take spaces into account, AFAIK. So you can just add:
Space
: (' ' | '\t' | '\r' | '\n'){$channel=HIDDEN;}
;
in your grammar.
EDIT
It seems there is a Lua grammar on the ANTLR wiki: http://www.antlr.org/grammar/1178608849736/Lua.g
And it seems I my suggestion for an if statement slightly differs from the grammar above:
'if' exp 'then' block ('elseif' exp 'then' block)* ('else' block)? 'end'
which is the correct one, as you can see.

Resources